diff --git a/robocop_ng/__init__.py b/robocop_ng/__init__.py index a97059a..f201189 100755 --- a/robocop_ng/__init__.py +++ b/robocop_ng/__init__.py @@ -236,4 +236,4 @@ for wanted_json in wanted_jsons: with open(wanted_json, "w") as f: f.write("{}") -bot.run(config.token, bot=True, reconnect=True) +bot.run(config.token, reconnect=True) diff --git a/robocop_ng/cogs/admin.py b/robocop_ng/cogs/admin.py index 0f991b0..3e7c097 100644 --- a/robocop_ng/cogs/admin.py +++ b/robocop_ng/cogs/admin.py @@ -172,5 +172,5 @@ class Admin(Cog): await ctx.send(f":white_check_mark: `{ext}` successfully reloaded.") -def setup(bot): - bot.add_cog(Admin(bot)) +async def setup(bot): + await bot.add_cog(Admin(bot)) diff --git a/robocop_ng/cogs/basic.py b/robocop_ng/cogs/basic.py index 306b171..48e6874 100644 --- a/robocop_ng/cogs/basic.py +++ b/robocop_ng/cogs/basic.py @@ -63,5 +63,5 @@ class Basic(Cog): await tmp.edit(content=message_text) -def setup(bot): - bot.add_cog(Basic(bot)) +async def setup(bot): + await bot.add_cog(Basic(bot)) diff --git a/robocop_ng/cogs/basic_reswitched.py b/robocop_ng/cogs/basic_reswitched.py index bf2b6ee..be3b3de 100644 --- a/robocop_ng/cogs/basic_reswitched.py +++ b/robocop_ng/cogs/basic_reswitched.py @@ -26,5 +26,5 @@ class BasicReswitched(Cog): ) -def setup(bot): - bot.add_cog(BasicReswitched(bot)) +async def setup(bot): + await bot.add_cog(BasicReswitched(bot)) diff --git a/robocop_ng/cogs/common.py b/robocop_ng/cogs/common.py index af9d094..66f40ec 100644 --- a/robocop_ng/cogs/common.py +++ b/robocop_ng/cogs/common.py @@ -191,5 +191,5 @@ class Common(Cog): return "No output." -def setup(bot): - bot.add_cog(Common(bot)) +async def setup(bot): + await bot.add_cog(Common(bot)) diff --git a/robocop_ng/cogs/err.py b/robocop_ng/cogs/err.py index a6c3bbe..f8470db 100644 --- a/robocop_ng/cogs/err.py +++ b/robocop_ng/cogs/err.py @@ -194,5 +194,5 @@ class Err(Cog): await ctx.send("This doesn't look like typical hex!") -def setup(bot): - bot.add_cog(Err(bot)) +async def setup(bot): + await bot.add_cog(Err(bot)) diff --git a/robocop_ng/cogs/imagemanip.py b/robocop_ng/cogs/imagemanip.py index f63545f..1b15082 100644 --- a/robocop_ng/cogs/imagemanip.py +++ b/robocop_ng/cogs/imagemanip.py @@ -83,5 +83,5 @@ class ImageManip(Cog): await ctx.send(content=f"{mention}: Enjoy.", file=discord.File(out_filename)) -def setup(bot): - bot.add_cog(ImageManip(bot)) +async def setup(bot): + await bot.add_cog(ImageManip(bot)) diff --git a/robocop_ng/cogs/invites.py b/robocop_ng/cogs/invites.py index 8c60a31..881ef6a 100644 --- a/robocop_ng/cogs/invites.py +++ b/robocop_ng/cogs/invites.py @@ -43,5 +43,5 @@ class Invites(Cog): ) -def setup(bot): - bot.add_cog(Invites(bot)) +async def setup(bot): + await bot.add_cog(Invites(bot)) diff --git a/robocop_ng/cogs/legacy.py b/robocop_ng/cogs/legacy.py index 7a0b566..fe85d99 100644 --- a/robocop_ng/cogs/legacy.py +++ b/robocop_ng/cogs/legacy.py @@ -33,5 +33,5 @@ class Legacy(Cog): ) -def setup(bot): - bot.add_cog(Legacy(bot)) +async def setup(bot): + await bot.add_cog(Legacy(bot)) diff --git a/robocop_ng/cogs/links.py b/robocop_ng/cogs/links.py index 79e174e..7341fc3 100644 --- a/robocop_ng/cogs/links.py +++ b/robocop_ng/cogs/links.py @@ -76,5 +76,5 @@ class Links(Cog): ) -def setup(bot): - bot.add_cog(Links(bot)) +async def setup(bot): + await bot.add_cog(Links(bot)) diff --git a/robocop_ng/cogs/lists.py b/robocop_ng/cogs/lists.py index 3444e07..bb3867a 100644 --- a/robocop_ng/cogs/lists.py +++ b/robocop_ng/cogs/lists.py @@ -389,5 +389,5 @@ class Lists(Cog): ) -def setup(bot): - bot.add_cog(Lists(bot)) +async def setup(bot): + await bot.add_cog(Lists(bot)) diff --git a/robocop_ng/cogs/lockdown.py b/robocop_ng/cogs/lockdown.py index 4128f0b..c9c6796 100644 --- a/robocop_ng/cogs/lockdown.py +++ b/robocop_ng/cogs/lockdown.py @@ -101,5 +101,5 @@ class Lockdown(Cog): await log_channel.send(msg) -def setup(bot): - bot.add_cog(Lockdown(bot)) +async def setup(bot): + await bot.add_cog(Lockdown(bot)) diff --git a/robocop_ng/cogs/logs.py b/robocop_ng/cogs/logs.py index 72599f7..efeb024 100644 --- a/robocop_ng/cogs/logs.py +++ b/robocop_ng/cogs/logs.py @@ -381,5 +381,5 @@ class Logs(Cog): await log_channel.send(msg) -def setup(bot): - bot.add_cog(Logs(bot)) +async def setup(bot): + await bot.add_cog(Logs(bot)) diff --git a/robocop_ng/cogs/meme.py b/robocop_ng/cogs/meme.py index 07b0753..a26f327 100644 --- a/robocop_ng/cogs/meme.py +++ b/robocop_ng/cogs/meme.py @@ -174,5 +174,5 @@ class Meme(Cog): ) -def setup(bot): - bot.add_cog(Meme(bot)) +async def setup(bot): + await bot.add_cog(Meme(bot)) diff --git a/robocop_ng/cogs/mod.py b/robocop_ng/cogs/mod.py index a4d3f9d..dd97547 100644 --- a/robocop_ng/cogs/mod.py +++ b/robocop_ng/cogs/mod.py @@ -697,5 +697,5 @@ class Mod(Cog): await ctx.send("Successfully set bot nickname.") -def setup(bot): - bot.add_cog(Mod(bot)) +async def setup(bot): + await bot.add_cog(Mod(bot)) diff --git a/robocop_ng/cogs/mod_note.py b/robocop_ng/cogs/mod_note.py index a7e4499..e2aac25 100644 --- a/robocop_ng/cogs/mod_note.py +++ b/robocop_ng/cogs/mod_note.py @@ -26,5 +26,5 @@ class ModNote(Cog): await ctx.send(f"{ctx.author.mention}: noted!") -def setup(bot): - bot.add_cog(ModNote(bot)) +async def setup(bot): + await bot.add_cog(ModNote(bot)) diff --git a/robocop_ng/cogs/mod_reacts.py b/robocop_ng/cogs/mod_reacts.py index 36e692d..f48a413 100644 --- a/robocop_ng/cogs/mod_reacts.py +++ b/robocop_ng/cogs/mod_reacts.py @@ -118,5 +118,5 @@ class ModReact(Cog): await msg.edit(content=f"{msg_text} Done!") -def setup(bot): - bot.add_cog(ModReact(bot)) +async def setup(bot): + await bot.add_cog(ModReact(bot)) diff --git a/robocop_ng/cogs/mod_reswitched.py b/robocop_ng/cogs/mod_reswitched.py index 95f3feb..4966ec4 100644 --- a/robocop_ng/cogs/mod_reswitched.py +++ b/robocop_ng/cogs/mod_reswitched.py @@ -41,5 +41,5 @@ class ModReswitched(Cog): await ctx.send(f"{ctx.author.mention}: Gave you mod role.") -def setup(bot): - bot.add_cog(ModReswitched(bot)) +async def setup(bot): + await bot.add_cog(ModReswitched(bot)) diff --git a/robocop_ng/cogs/mod_timed.py b/robocop_ng/cogs/mod_timed.py index 6a4416f..812b053 100644 --- a/robocop_ng/cogs/mod_timed.py +++ b/robocop_ng/cogs/mod_timed.py @@ -156,5 +156,5 @@ class ModTimed(Cog): add_restriction(target.id, config.mute_role) -def setup(bot): - bot.add_cog(ModTimed(bot)) +async def setup(bot): + await bot.add_cog(ModTimed(bot)) diff --git a/robocop_ng/cogs/mod_userlog.py b/robocop_ng/cogs/mod_userlog.py index e586d58..9a8ea61 100644 --- a/robocop_ng/cogs/mod_userlog.py +++ b/robocop_ng/cogs/mod_userlog.py @@ -244,5 +244,5 @@ class ModUserlog(Cog): ) -def setup(bot): - bot.add_cog(ModUserlog(bot)) +async def setup(bot): + await bot.add_cog(ModUserlog(bot)) diff --git a/robocop_ng/cogs/mod_watch.py b/robocop_ng/cogs/mod_watch.py index eb783e2..d04d656 100644 --- a/robocop_ng/cogs/mod_watch.py +++ b/robocop_ng/cogs/mod_watch.py @@ -42,5 +42,5 @@ class ModWatch(Cog): await ctx.send(f"{target.mention}: user is now not on watch.") -def setup(bot): - bot.add_cog(ModWatch(bot)) +async def setup(bot): + await bot.add_cog(ModWatch(bot)) diff --git a/robocop_ng/cogs/pin.py b/robocop_ng/cogs/pin.py index 9d8ac98..6a964b1 100644 --- a/robocop_ng/cogs/pin.py +++ b/robocop_ng/cogs/pin.py @@ -157,5 +157,5 @@ def check(msg): return msg.type is MessageType.pins_add -def setup(bot): - bot.add_cog(Pin(bot)) +async def setup(bot): + await bot.add_cog(Pin(bot)) diff --git a/robocop_ng/cogs/remind.py b/robocop_ng/cogs/remind.py index 0a03ce7..c402b3e 100644 --- a/robocop_ng/cogs/remind.py +++ b/robocop_ng/cogs/remind.py @@ -73,5 +73,5 @@ class Remind(Cog): await msg.delete() -def setup(bot): - bot.add_cog(Remind(bot)) +async def setup(bot): + await bot.add_cog(Remind(bot)) diff --git a/robocop_ng/cogs/robocronp.py b/robocop_ng/cogs/robocronp.py index 47c32a0..97eea2e 100644 --- a/robocop_ng/cogs/robocronp.py +++ b/robocop_ng/cogs/robocronp.py @@ -3,7 +3,7 @@ import config import time import discord import traceback -from discord.ext import commands +from discord.ext import commands, tasks from discord.ext.commands import Cog from helpers.robocronp import get_crontab, delete_job from helpers.restrictions import remove_restriction @@ -13,9 +13,14 @@ from helpers.checks import check_if_staff class Robocronp(Cog): def __init__(self, bot): self.bot = bot - bot.loop.create_task(self.minutely()) - bot.loop.create_task(self.hourly()) - bot.loop.create_task(self.daily()) + self.minutely.start() + self.hourly.start() + self.daily.start() + + def cog_unload(self): + self.minutely.cancel() + self.hourly.cancel() + self.daily.cancel() async def send_data(self): data_files = [discord.File(fpath) for fpath in self.bot.wanted_jsons] @@ -113,69 +118,54 @@ class Robocronp(Cog): f"Cronclean has errored: ```{traceback.format_exc()}```" ) + @tasks.loop(minutes=1) async def minutely(self): - await self.bot.wait_until_ready() log_channel = self.bot.get_channel(config.botlog_channel) - while not self.bot.is_closed(): - try: - ctab = get_crontab() - timestamp = time.time() - for jobtype in ctab: - for jobtimestamp in ctab[jobtype]: - if timestamp > int(jobtimestamp): - await self.do_jobs(ctab, jobtype, jobtimestamp) + try: + ctab = get_crontab() + timestamp = time.time() + for jobtype in ctab: + for jobtimestamp in ctab[jobtype]: + if timestamp > int(jobtimestamp): + await self.do_jobs(ctab, jobtype, jobtimestamp) - # Handle clean channels - for clean_channel in config.minutely_clean_channels: - await self.clean_channel(clean_channel) - except: - # Don't kill cronjobs if something goes wrong. - await log_channel.send( - f"Cron-minutely has errored: ```{traceback.format_exc()}```" - ) - await asyncio.sleep(60) + # Handle clean channels + for clean_channel in config.minutely_clean_channels: + await self.clean_channel(clean_channel) + except: + # Don't kill cronjobs if something goes wrong. + await log_channel.send( + f"Cron-minutely has errored: ```{traceback.format_exc()}```" + ) + @tasks.loop(hours=1) async def hourly(self): - await self.bot.wait_until_ready() log_channel = self.bot.get_channel(config.botlog_channel) - while not self.bot.is_closed(): - # Your stuff that should run at boot - # and after that every hour goes here - await asyncio.sleep(3600) - try: - await self.send_data() - - # Handle clean channels - for clean_channel in config.hourly_clean_channels: - await self.clean_channel(clean_channel) - except: - # Don't kill cronjobs if something goes wrong. - await log_channel.send( - f"Cron-hourly has errored: ```{traceback.format_exc()}```" - ) - # Your stuff that should run an hour after boot - # and after that every hour goes here + try: + await self.send_data() + # Handle clean channels + for clean_channel in config.hourly_clean_channels: + await self.clean_channel(clean_channel) + except: + # Don't kill cronjobs if something goes wrong. + await log_channel.send( + f"Cron-hourly has errored: ```{traceback.format_exc()}```" + ) + @tasks.loop(hours=24) async def daily(self): - await self.bot.wait_until_ready() log_channel = self.bot.get_channel(config.botlog_channel) - while not self.bot.is_closed(): - # Your stuff that should run at boot - # and after that every day goes here - try: - # Reset verification and algorithm - if "cogs.verification" in config.initial_cogs: - verif_channel = self.bot.get_channel(config.welcome_channel) - await self.bot.do_resetalgo(verif_channel, "daily robocronp") - except: - # Don't kill cronjobs if something goes wrong. - await log_channel.send( - f"Cron-daily has errored: ```{traceback.format_exc()}```" - ) - await asyncio.sleep(86400) - # Your stuff that should run a day after boot - # and after that every day goes here + try: + # Reset verification and algorithm + if "cogs.verification" in config.initial_cogs: + verif_channel = self.bot.get_channel(config.welcome_channel) + await self.bot.do_resetalgo(verif_channel, "daily robocronp") + except: + # Don't kill cronjobs if something goes wrong. + await log_channel.send( + f"Cron-daily has errored: ```{traceback.format_exc()}```" + ) -def setup(bot): - bot.add_cog(Robocronp(bot)) +async def setup(bot): + await bot.add_cog(Robocronp(bot)) diff --git a/robocop_ng/cogs/sar.py b/robocop_ng/cogs/sar.py index d1723df..21f83cd 100644 --- a/robocop_ng/cogs/sar.py +++ b/robocop_ng/cogs/sar.py @@ -44,5 +44,5 @@ class SAR(Cog): ) -def setup(bot): - bot.add_cog(SAR(bot)) +async def setup(bot): + await bot.add_cog(SAR(bot)) diff --git a/robocop_ng/cogs/verification.py b/robocop_ng/cogs/verification.py index d30e737..9756c9f 100644 --- a/robocop_ng/cogs/verification.py +++ b/robocop_ng/cogs/verification.py @@ -218,5 +218,5 @@ class Verification(Cog): await chan.send("💢 I don't have permission to do this.") -def setup(bot): - bot.add_cog(Verification(bot)) +async def setup(bot): + await bot.add_cog(Verification(bot)) diff --git a/robocop_ng/cogs/yubicootp.py b/robocop_ng/cogs/yubicootp.py index a5df8ed..b322889 100644 --- a/robocop_ng/cogs/yubicootp.py +++ b/robocop_ng/cogs/yubicootp.py @@ -148,5 +148,5 @@ class YubicoOTP(Cog): await msg.delete() -def setup(bot): - bot.add_cog(YubicoOTP(bot)) +async def setup(bot): + await bot.add_cog(YubicoOTP(bot))