From 5d0aaf53a809223deea2c264d033b50aba06fbe6 Mon Sep 17 00:00:00 2001 From: imcompiling Date: Sat, 15 Apr 2023 22:26:46 -0400 Subject: [PATCH 1/3] Create loop, setup docker file for testing. --- Dockerfile | 19 +++++++++++++++++++ cogs/loops/__init__.py | 15 +++++++++++++++ cogs/loops/daily_loop.py | 23 +++++++++++++++++++++++ config.py | 1 - bot.py => dakiny_bot.py | 11 +---------- requirements.txt | 1 + 6 files changed, 59 insertions(+), 11 deletions(-) create mode 100644 Dockerfile create mode 100644 cogs/loops/__init__.py create mode 100644 cogs/loops/daily_loop.py rename bot.py => dakiny_bot.py (76%) diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..d590e9a --- /dev/null +++ b/Dockerfile @@ -0,0 +1,19 @@ +FROM python:3.8.10-slim + +# install dependencies for mysqlclient +# RUN apt-get update && apt-get -y upgrade && apt-get -y install python3-dev default-libmysqlclient-dev build-essential + +RUN pip install --upgrade pip + +WORKDIR /home + +COPY cogs cogs/ +COPY dakiny_bot.py dakiny_bot.py +COPY requirements.txt requirements.txt + +RUN pip install -r requirements.txt + +# remove build deps that aren't needed to run +# RUN apt-get -y remove python3-dev build-essential && apt-get -y autoremove + +CMD ["python3", "./dakiny_bot.py"] \ No newline at end of file diff --git a/cogs/loops/__init__.py b/cogs/loops/__init__.py new file mode 100644 index 0000000..20564de --- /dev/null +++ b/cogs/loops/__init__.py @@ -0,0 +1,15 @@ +""" +Initializing code for all loops. +""" + +from nextcord.ext import commands + +from .daily_loop import DailyLoop + + +def setup(bot: commands.Bot): + """ + Adds functions to bot. + """ + + bot.add_cog(DailyLoop(bot)) diff --git a/cogs/loops/daily_loop.py b/cogs/loops/daily_loop.py new file mode 100644 index 0000000..f12b5f9 --- /dev/null +++ b/cogs/loops/daily_loop.py @@ -0,0 +1,23 @@ +""" +_summary_ +""" + +import nextcord +from nextcord.ext import commands, tasks +from datetime import time +from pytz import timezone + + +class DailyLoop(commands.Cog, name="Daily Loop"): + """ + Class for performing daily functions. + """ + + def __init__(self, bot: commands.Bot) -> None: + self.bot = bot + + @tasks.loop(time=time(hour=0, tzinfo=timezone("GMT"))) + async def do_thing(self): + """ """ + imcompiling: nextcord.User = await self.bot.fetch_user(206602713511886849) + await imcompiling.send("its reset in runescape") diff --git a/config.py b/config.py index 21eb1e4..2383790 100644 --- a/config.py +++ b/config.py @@ -6,4 +6,3 @@ load_dotenv() # Discord config DISCORD_TOKEN = os.getenv("DISCORD_TOKEN", "") -BOT_PREFIX = "?" diff --git a/bot.py b/dakiny_bot.py similarity index 76% rename from bot.py rename to dakiny_bot.py index 9f5d293..91d5ec3 100644 --- a/bot.py +++ b/dakiny_bot.py @@ -20,16 +20,7 @@ def main(): # To enable the members priveleged intent: # intents.members = True - # Set custom status to "Listening to ?help" - activity = nextcord.Activity( - type=nextcord.ActivityType.listening, name=f"{config.BOT_PREFIX}help" - ) - - bot = commands.Bot( - commands.when_mentioned_or(config.BOT_PREFIX), - intents=intents, - activity=activity, - ) + bot = commands.Bot(intents=intents) # Get the modules of all cogs whose directory structure is ./cogs/ for folder in os.listdir("cogs"): diff --git a/requirements.txt b/requirements.txt index 2212c02..97a0b72 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ nextcord==2.4.2 python-dotenv==1.0.0 +pytz==2022.7.1 \ No newline at end of file From d21e2182b8e6faf65c93ee800c38e1a52ca324f4 Mon Sep 17 00:00:00 2001 From: imcompiling Date: Sat, 15 Apr 2023 22:34:42 -0400 Subject: [PATCH 2/3] Need to start the loop. --- cogs/loops/daily_loop.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/cogs/loops/daily_loop.py b/cogs/loops/daily_loop.py index f12b5f9..2bec4ce 100644 --- a/cogs/loops/daily_loop.py +++ b/cogs/loops/daily_loop.py @@ -16,6 +16,15 @@ class DailyLoop(commands.Cog, name="Daily Loop"): def __init__(self, bot: commands.Bot) -> None: self.bot = bot + @commands.Cog.listener() + async def on_ready(self): + """ + Starts the loop when bot is ready. + """ + await self.bot.wait_until_ready() + # pylint doesn't recognize that periodic_update_guild_data is subclassed from tasks.loop. + await self.do_thing.start() # pylint: disable=no-member + @tasks.loop(time=time(hour=0, tzinfo=timezone("GMT"))) async def do_thing(self): """ """ From 22d2d04b3b27d81646a0d1b86df248d1123ad2cb Mon Sep 17 00:00:00 2001 From: imcompiling Date: Sat, 15 Apr 2023 22:46:51 -0400 Subject: [PATCH 3/3] Correct dockerfile. --- Dockerfile | 1 + 1 file changed, 1 insertion(+) diff --git a/Dockerfile b/Dockerfile index d590e9a..bb01a46 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,6 +10,7 @@ WORKDIR /home COPY cogs cogs/ COPY dakiny_bot.py dakiny_bot.py COPY requirements.txt requirements.txt +COPY config.py config.py RUN pip install -r requirements.txt