zmoog / dolores

Apache License 2.0
0 stars 0 forks source link

Schedule Trello card listing #3

Open zmoog opened 1 year ago

zmoog commented 1 year ago

I want to schedule the listing of the cards in a Trello list.

For example, I want to receive from Dolores:

zmoog commented 1 year ago

Extensions

discord.py has a nice extension mechanism to dynamically load and plug in components. Here are a couple of examples:

Trying a couple of variants to see what works better:

async def main():
    async with bot:
        # for filename in os.listdir("./dolores/cogs"):
        #     if filename.endswith(".py"):
        #         # cut off the .py from the file name
        #         await bot.load_extension(f"dolores. cogs.{filename[:-3]}")
        for extension in ["dolores.cogs.trello"]:
            await bot.load_extension(extension)

        await bot.start(os.getenv("DISCORD_TOKEN"))
zmoog commented 1 year ago

The extension also works for commands. Here's a cog that has a task and a command:

import datetime
from discord.ext import commands, tasks

utc = datetime.timezone.utc

# If no tzinfo is given then UTC is assumed.
times = [
    # datetime.time(hour=8, tzinfo=utc),
    datetime.time(hour=15, minute=39, tzinfo=utc),
    # datetime.time(hour=16, minute=40, second=30, tzinfo=utc)
]

class MyCog(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
        self.my_task.start()
        print("My cog has been loaded!")

    def cog_unload(self):
        self.my_task.cancel()
        print("My cog has been unloaded!")

    @tasks.loop(time=times)
    async def my_task(self):
        print("My task is running!")

    @commands.command(name="hey")
    async def hey(self, ctx):
        print("hey")
        await ctx.send("hey")

async def setup(bot):
    await bot.add_cog(MyCog(bot))
zmoog commented 1 year ago

I should change TrelloKit to unmarshal the JSON from Trello into actual datetime objects instead of str objects requiring further downstream handling.

Checking https://docs.pydantic.dev/latest/usage/types/datetime/