From 9da71373131b23a5a9eab710cfeabeccdb555add Mon Sep 17 00:00:00 2001 From: vypr Date: Thu, 9 Apr 2020 03:45:02 -0400 Subject: [PATCH] Fix auto-daily, other bugfixes. --- .vscode/settings.json | 3 ++ src/bot.py | 36 ++++++++++---------- src/handlers/command_logic/command_bridge.py | 2 +- src/handlers/command_logic/embed_builders.py | 25 +++----------- 4 files changed, 27 insertions(+), 39 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..84a192c --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.pythonPath": "venv\\Scripts\\python.exe" +} \ No newline at end of file diff --git a/src/bot.py b/src/bot.py index b3d68ef..e8cd6ae 100644 --- a/src/bot.py +++ b/src/bot.py @@ -23,6 +23,8 @@ import os import discord +from discord.ext import tasks + import central from handlers.command_logic import embed_builders from handlers.commands import CommandHandler @@ -36,7 +38,7 @@ config.read(dir_path + "/config.ini") class OrthoBot(discord.AutoShardedClient): def __init__(self, *args, loop=None, **kwargs): super().__init__(*args, loop=loop, **kwargs) - self.bg_task = self.loop.create_task(self.run_dailies()) + self.run_dailies.start() self.current_page = None self.total_pages = None @@ -54,30 +56,28 @@ class OrthoBot(discord.AutoShardedClient): central.log_message("info", shard_id + 1, "global", "global", "connected") + @tasks.loop(seconds=60.0) async def run_dailies(self): await self.wait_until_ready() - while not self.is_closed(): - # noinspection PyBroadException - try: - # a nice list comprehension for getting all the servers with daily stuff set - results = [x for x in central.guildDB.all() if "channel" in x and "time" in x] + # a nice list comprehension for getting all the servers with daily stuff set + results = [x for x in central.guildDB.all() if "channel" in x and "time" in x] - for item in results: - if "channel" in item and "time" in item: - channel = self.get_channel(int(item["channel"])) - daily_time = item["time"] + for item in results: + if "channel" in item and "time" in item: + channel = self.get_channel(int(item["channel"])) + daily_time = item["time"] - current_time = datetime.datetime.utcnow().strftime("%H:%M") + current_time = datetime.datetime.utcnow().strftime("%H:%M") - if daily_time == current_time: - embed = embed_builders.create_daily_embed() - await channel.send("Here is today's daily readings and saints/feasts:") - await channel.send(embed=embed) - except Exception: - pass + if daily_time == current_time: + embed = embed_builders.create_daily_embed() - await asyncio.sleep(60) + try: + await channel.send("Here is today's daily readings and saints/feasts:") + await channel.send(embed=embed) + except discord.errors.Forbidden: + pass async def on_message(self, raw): await self.wait_until_ready() diff --git a/src/handlers/command_logic/command_bridge.py b/src/handlers/command_logic/command_bridge.py index f3249a6..9658e0e 100644 --- a/src/handlers/command_logic/command_bridge.py +++ b/src/handlers/command_logic/command_bridge.py @@ -129,7 +129,7 @@ def run_command(command, args, user, guild, channel): "message": pages[0] } elif command == "daily": - return_embed = embed_builders.create_daily_embed(args[0]) + return_embed = embed_builders.create_daily_embed(args[0] if args else None) return { "level": "info", diff --git a/src/handlers/command_logic/embed_builders.py b/src/handlers/command_logic/embed_builders.py index 7bf84f5..ab6e500 100644 --- a/src/handlers/command_logic/embed_builders.py +++ b/src/handlers/command_logic/embed_builders.py @@ -46,16 +46,14 @@ def create_daily_embed(day="today"): elif day in dates: date_formatted = f"{dates[day].month}/{dates[day].day}/{dates[day].year}" daily = Daily(date_formatted) - else: + elif day is not None: date_format = re.compile("[0-9]*\/[0-9]*\/[0-9]*") if date_format.match(day): day = day[1:] if day.startswith("0") else day daily = Daily(day) - else: - day = "random" - date_formatted = f"{dates[day].month}/{dates[day].day}/{dates[day].year}" - daily = Daily(date_formatted) + else: + daily = Daily() daily.get_data() @@ -95,13 +93,6 @@ def create_daily_embed(day="today"): else: embed.add_field(name=f"{saint.title} ({saint.id})", value="No information provided.", inline=False) - name = "For more about today's saints and feasts" - - if saints_to_display < len(daily.saints): - name += f" (including the remaining {str(len(daily.saints) - saints_to_display)})" - - embed.add_field(name=f"{name}:", value=daily.public_url) - for reading in daily.readings: title = f"{reading.type_bb.title()} Reading ({reading.id}, {reading.event})" title = title.replace("Mg ", "Matins Gospel ") @@ -148,22 +139,19 @@ def create_saint_embed(_id): _type += f" ({reading.id}, {saint.id})" - embed.add_field(name=_type, value=reading.short_title, inline=True) + embed.add_field(name=_type, value=reading.short_title, inline=False) for hymn in saint.hymns: embed.add_field(name=f"{hymn.short_title} ({hymn.tone})", value=f"© {hymn.translation.copyright} // {hymn.translation.body}", inline=False) - name = "For more about this saint/feast" - embed.add_field(name=f"{name}:", value=saint.public_url, inline=False) - embed.set_footer(icon_url=central.icon, text=f"{central.version} | Greek Orthodox Archdiocese of America") return embed def create_lectionary_embed(_type, _id, event, _date=None): - lectionary = Lectionary(_type, _id, event, date=_date) + lectionary = Lectionary(_type.upper(), _id, event, date=_date) lectionary.get_data() embed = discord.Embed() @@ -179,9 +167,6 @@ def create_lectionary_embed(_type, _id, event, _date=None): reading_excerpt = textwrap.shorten(translation.body, width=600, placeholder="...") embed.add_field(name=translation.short_title, value=reading_excerpt, inline=False) - name = "For more about this lectionary/reading" - embed.add_field(name=f"{name}:", value=lectionary.public_url, inline=False) - embed.set_footer(icon_url=central.icon, text=f"{central.version} | Greek Orthodox Archdiocese of America") return embed