From f649d15890839342169a7e01693a3f04f3227939 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 23 Dec 2019 13:29:04 -0500 Subject: [PATCH 1/7] modify playing status so it changes based on time of day Fixes #57 --- main.py | 23 +++++++++++++++++++++-- requirements.txt | 1 + templates/data/options.py | 12 ++++++++++-- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/main.py b/main.py index 832d0ac..a490d57 100644 --- a/main.py +++ b/main.py @@ -8,12 +8,14 @@ This file is part of discord-qrm2 and is released under the terms of the GNU General Public License, version 2. """ +from datetime import time, datetime + import discord from discord.ext import commands, tasks +import pytz import common as cmn import info - import data.options as opt import data.keys as keys @@ -129,7 +131,24 @@ async def on_message(message): @tasks.loop(minutes=5) async def _ensure_activity(): - await bot.change_presence(activity=discord.Game(name=opt.game)) + status = opt.status_default + + try: + tz = pytz.timezone(opt.status_tz) + except pytz.exceptions.UnknownTimeZoneError: + print(f'[!!] UnknownTimeZoneError: {opt.status_tz}') + await bot.change_presence(activity=discord.Game(name=status)) + return + + now = datetime.now(tz=tz).time() + + for sts in opt.statuses: + start_time = time(hour=sts[1][0], minute=sts[1][1], tzinfo=tz) + end_time = time(hour=sts[2][0], minute=sts[2][1], tzinfo=tz) + if start_time < now <= end_time: + status = sts[0] + + await bot.change_presence(activity=discord.Game(name=status)) @_ensure_activity.before_loop diff --git a/requirements.txt b/requirements.txt index a31e785..21f069a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,4 @@ discord.py ctyparser beautifulsoup4 lxml +pytz diff --git a/templates/data/options.py b/templates/data/options.py index 6fe15b7..768126c 100644 --- a/templates/data/options.py +++ b/templates/data/options.py @@ -29,8 +29,16 @@ owners_uids = (200102491231092736,) # The extensions to load when running the bot. exts = ['ae7q', 'base', 'fun', 'grid', 'ham', 'image', 'lookup', 'morse', 'qrz', 'study', 'weather'] -# The text to put in the "playing" status. -game = 'with lids on 7.200' +# Timezone for the status (string) +status_tz = 'US/Eastern' +# The text to put in the "playing" status, with start and stop times +statuses = [('with lids on 3.840', (00,00), (6,00)), + ('with lids on 7.200', (6,00), (10,00)), + ('with lids on 14.313', (10,00), (18,00)), + ('with lids on 7.200', (18,00), (20,00)), + ('with lids on 3.840', (20,00), (23,59))] +# The text to put in the "playing" status otherwise +status_default = 'with lids on the air' # Emoji IDs and keywords for emoji reactions # Use the format {emoji_id (int): ('tuple', 'of', 'lowercase', 'keywords')} From 2ccb225a5f271458113ae210af76b6bfb40a641a Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 23 Dec 2019 13:33:19 -0500 Subject: [PATCH 2/7] hhhhhhhh whitespace --- templates/data/options.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/templates/data/options.py b/templates/data/options.py index 768126c..99fa7b8 100644 --- a/templates/data/options.py +++ b/templates/data/options.py @@ -32,11 +32,11 @@ exts = ['ae7q', 'base', 'fun', 'grid', 'ham', 'image', 'lookup', 'morse', 'qrz', # Timezone for the status (string) status_tz = 'US/Eastern' # The text to put in the "playing" status, with start and stop times -statuses = [('with lids on 3.840', (00,00), (6,00)), - ('with lids on 7.200', (6,00), (10,00)), - ('with lids on 14.313', (10,00), (18,00)), - ('with lids on 7.200', (18,00), (20,00)), - ('with lids on 3.840', (20,00), (23,59))] +statuses = [('with lids on 3.840', (00, 00), (6, 00)), + ('with lids on 7.200', (6, 00), (10, 00)), + ('with lids on 14.313', (10, 00), (18, 00)), + ('with lids on 7.200', (18, 00), (20, 00)), + ('with lids on 3.840', (20, 00), (23, 59))] # The text to put in the "playing" status otherwise status_default = 'with lids on the air' From 9e79eececcf4557de1dae5ef65763bf0fc5b8e9c Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 23 Dec 2019 13:34:56 -0500 Subject: [PATCH 3/7] remove print statement --- main.py | 1 - 1 file changed, 1 deletion(-) diff --git a/main.py b/main.py index a490d57..405fe8e 100644 --- a/main.py +++ b/main.py @@ -136,7 +136,6 @@ async def _ensure_activity(): try: tz = pytz.timezone(opt.status_tz) except pytz.exceptions.UnknownTimeZoneError: - print(f'[!!] UnknownTimeZoneError: {opt.status_tz}') await bot.change_presence(activity=discord.Game(name=status)) return From b25b5a95ffd473c75fbaa3fee16c863dd59fed34 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 23 Dec 2019 14:06:42 -0500 Subject: [PATCH 4/7] refactor to allow for fixed, random, or by time statuses --- main.py | 28 ++++++++++++++++++++-------- templates/data/options.py | 18 +++++++++++------- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/main.py b/main.py index 405fe8e..ed8103f 100644 --- a/main.py +++ b/main.py @@ -9,6 +9,7 @@ General Public License, version 2. """ from datetime import time, datetime +import random import discord from discord.ext import commands, tasks @@ -115,6 +116,12 @@ async def _extctl_unload(ctx: commands.Context, extension: str): async def on_ready(): print(f"Logged in as: {bot.user} - {bot.user.id}") print("------") + if opt.status_mode == "time": + _ensure_activity_time.start() + elif opt.status_mode == "random": + _ensure_activity_random.start() + else: + _ensure_activity_fixed.start() @bot.event @@ -130,8 +137,8 @@ async def on_message(message): # --- Tasks --- @tasks.loop(minutes=5) -async def _ensure_activity(): - status = opt.status_default +async def _ensure_activity_time(): + status = opt.statuses[0] try: tz = pytz.timezone(opt.status_tz) @@ -141,7 +148,7 @@ async def _ensure_activity(): now = datetime.now(tz=tz).time() - for sts in opt.statuses: + for sts in opt.time_statuses: start_time = time(hour=sts[1][0], minute=sts[1][1], tzinfo=tz) end_time = time(hour=sts[2][0], minute=sts[2][1], tzinfo=tz) if start_time < now <= end_time: @@ -149,10 +156,17 @@ async def _ensure_activity(): await bot.change_presence(activity=discord.Game(name=status)) +@tasks.loop(minutes=5) +async def _ensure_activity_random(): + status = random.choice(opt.statuses) -@_ensure_activity.before_loop -async def _before_ensure_activity(): - await bot.wait_until_ready() + await bot.change_presence(activity=discord.Game(name=status)) + +@tasks.loop(minutes=5) +async def _ensure_activity_fixed(): + status = opt.statuses[0] + + await bot.change_presence(activity=discord.Game(name=status)) # --- Run --- @@ -160,8 +174,6 @@ async def _before_ensure_activity(): for ext in opt.exts: bot.load_extension(ext_dir + '.' + ext) -_ensure_activity.start() - try: bot.run(keys.discord_token) diff --git a/templates/data/options.py b/templates/data/options.py index 99fa7b8..225f329 100644 --- a/templates/data/options.py +++ b/templates/data/options.py @@ -29,16 +29,20 @@ owners_uids = (200102491231092736,) # The extensions to load when running the bot. exts = ['ae7q', 'base', 'fun', 'grid', 'ham', 'image', 'lookup', 'morse', 'qrz', 'study', 'weather'] +# Either "time", "random", or "fixed" (first item in statuses) +status_mode = "fixed" + +# Random statuses pool +statuses = ["with lids on the air", "with fire"] + # Timezone for the status (string) status_tz = 'US/Eastern' # The text to put in the "playing" status, with start and stop times -statuses = [('with lids on 3.840', (00, 00), (6, 00)), - ('with lids on 7.200', (6, 00), (10, 00)), - ('with lids on 14.313', (10, 00), (18, 00)), - ('with lids on 7.200', (18, 00), (20, 00)), - ('with lids on 3.840', (20, 00), (23, 59))] -# The text to put in the "playing" status otherwise -status_default = 'with lids on the air' +time_statuses = [('with lids on 3.840', (00, 00), (6, 00)), + ('with lids on 7.200', (6, 00), (10, 00)), + ('with lids on 14.313', (10, 00), (18, 00)), + ('with lids on 7.200', (18, 00), (20, 00)), + ('with lids on 3.840', (20, 00), (23, 59))] # Emoji IDs and keywords for emoji reactions # Use the format {emoji_id (int): ('tuple', 'of', 'lowercase', 'keywords')} From 4d763c2fc11927d3b20cb25e5febba36eb6a366b Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 23 Dec 2019 14:08:05 -0500 Subject: [PATCH 5/7] sigh --- main.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/main.py b/main.py index ed8103f..019e21e 100644 --- a/main.py +++ b/main.py @@ -156,12 +156,14 @@ async def _ensure_activity_time(): await bot.change_presence(activity=discord.Game(name=status)) + @tasks.loop(minutes=5) async def _ensure_activity_random(): status = random.choice(opt.statuses) await bot.change_presence(activity=discord.Game(name=status)) + @tasks.loop(minutes=5) async def _ensure_activity_fixed(): status = opt.statuses[0] From 951dc38992d319a9083655a16464fc457b35b27d Mon Sep 17 00:00:00 2001 From: Abigail Gold <5366828+classabbyamp@users.noreply.github.com> Date: Mon, 23 Dec 2019 15:56:25 -0500 Subject: [PATCH 6/7] Update main.py Co-Authored-By: 0x5c <0x5c.dev@gmail.com> --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index 019e21e..da19add 100644 --- a/main.py +++ b/main.py @@ -143,7 +143,7 @@ async def _ensure_activity_time(): try: tz = pytz.timezone(opt.status_tz) except pytz.exceptions.UnknownTimeZoneError: - await bot.change_presence(activity=discord.Game(name=status)) + await bot.change_presence(activity=discord.Game(name="with invalid timezones.")) return now = datetime.now(tz=tz).time() From 28eb6d45c1353f0cefbd067e54f52f7b3bac5051 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 23 Dec 2019 15:57:57 -0500 Subject: [PATCH 7/7] updates from review --- main.py | 3 ++- templates/data/options.py | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index da19add..a7e7b7b 100644 --- a/main.py +++ b/main.py @@ -11,9 +11,10 @@ General Public License, version 2. from datetime import time, datetime import random +import pytz + import discord from discord.ext import commands, tasks -import pytz import common as cmn import info diff --git a/templates/data/options.py b/templates/data/options.py index 225f329..194b7ad 100644 --- a/templates/data/options.py +++ b/templates/data/options.py @@ -36,6 +36,7 @@ status_mode = "fixed" statuses = ["with lids on the air", "with fire"] # Timezone for the status (string) +# See https://pythonhosted.org/pytz/ for more info status_tz = 'US/Eastern' # The text to put in the "playing" status, with start and stop times time_statuses = [('with lids on 3.840', (00, 00), (6, 00)),