make more of the code dpy2 friendly

- robocronp is completely broken
- The overall init should be unfucked, example here: https://discordpy.readthedocs.io/en/latest/migrating.html#extension-and-cog-loading-unloading-is-now-asynchronous
This commit is contained in:
ave 2022-05-24 20:35:42 +02:00
parent 07f53753c3
commit 2d25ab5601
27 changed files with 100 additions and 110 deletions

View file

@ -236,4 +236,4 @@ for wanted_json in wanted_jsons:
with open(wanted_json, "w") as f: with open(wanted_json, "w") as f:
f.write("{}") f.write("{}")
bot.run(config.token, bot=True, reconnect=True) bot.run(config.token, reconnect=True)

View file

@ -172,5 +172,5 @@ class Admin(Cog):
await ctx.send(f":white_check_mark: `{ext}` successfully reloaded.") await ctx.send(f":white_check_mark: `{ext}` successfully reloaded.")
def setup(bot): async def setup(bot):
bot.add_cog(Admin(bot)) await bot.add_cog(Admin(bot))

View file

@ -63,5 +63,5 @@ class Basic(Cog):
await tmp.edit(content=message_text) await tmp.edit(content=message_text)
def setup(bot): async def setup(bot):
bot.add_cog(Basic(bot)) await bot.add_cog(Basic(bot))

View file

@ -26,5 +26,5 @@ class BasicReswitched(Cog):
) )
def setup(bot): async def setup(bot):
bot.add_cog(BasicReswitched(bot)) await bot.add_cog(BasicReswitched(bot))

View file

@ -191,5 +191,5 @@ class Common(Cog):
return "No output." return "No output."
def setup(bot): async def setup(bot):
bot.add_cog(Common(bot)) await bot.add_cog(Common(bot))

View file

@ -194,5 +194,5 @@ class Err(Cog):
await ctx.send("This doesn't look like typical hex!") await ctx.send("This doesn't look like typical hex!")
def setup(bot): async def setup(bot):
bot.add_cog(Err(bot)) await bot.add_cog(Err(bot))

View file

@ -83,5 +83,5 @@ class ImageManip(Cog):
await ctx.send(content=f"{mention}: Enjoy.", file=discord.File(out_filename)) await ctx.send(content=f"{mention}: Enjoy.", file=discord.File(out_filename))
def setup(bot): async def setup(bot):
bot.add_cog(ImageManip(bot)) await bot.add_cog(ImageManip(bot))

View file

@ -43,5 +43,5 @@ class Invites(Cog):
) )
def setup(bot): async def setup(bot):
bot.add_cog(Invites(bot)) await bot.add_cog(Invites(bot))

View file

@ -33,5 +33,5 @@ class Legacy(Cog):
) )
def setup(bot): async def setup(bot):
bot.add_cog(Legacy(bot)) await bot.add_cog(Legacy(bot))

View file

@ -76,5 +76,5 @@ class Links(Cog):
) )
def setup(bot): async def setup(bot):
bot.add_cog(Links(bot)) await bot.add_cog(Links(bot))

View file

@ -389,5 +389,5 @@ class Lists(Cog):
) )
def setup(bot): async def setup(bot):
bot.add_cog(Lists(bot)) await bot.add_cog(Lists(bot))

View file

@ -101,5 +101,5 @@ class Lockdown(Cog):
await log_channel.send(msg) await log_channel.send(msg)
def setup(bot): async def setup(bot):
bot.add_cog(Lockdown(bot)) await bot.add_cog(Lockdown(bot))

View file

@ -381,5 +381,5 @@ class Logs(Cog):
await log_channel.send(msg) await log_channel.send(msg)
def setup(bot): async def setup(bot):
bot.add_cog(Logs(bot)) await bot.add_cog(Logs(bot))

View file

@ -174,5 +174,5 @@ class Meme(Cog):
) )
def setup(bot): async def setup(bot):
bot.add_cog(Meme(bot)) await bot.add_cog(Meme(bot))

View file

@ -697,5 +697,5 @@ class Mod(Cog):
await ctx.send("Successfully set bot nickname.") await ctx.send("Successfully set bot nickname.")
def setup(bot): async def setup(bot):
bot.add_cog(Mod(bot)) await bot.add_cog(Mod(bot))

View file

@ -26,5 +26,5 @@ class ModNote(Cog):
await ctx.send(f"{ctx.author.mention}: noted!") await ctx.send(f"{ctx.author.mention}: noted!")
def setup(bot): async def setup(bot):
bot.add_cog(ModNote(bot)) await bot.add_cog(ModNote(bot))

View file

@ -118,5 +118,5 @@ class ModReact(Cog):
await msg.edit(content=f"{msg_text} Done!") await msg.edit(content=f"{msg_text} Done!")
def setup(bot): async def setup(bot):
bot.add_cog(ModReact(bot)) await bot.add_cog(ModReact(bot))

View file

@ -41,5 +41,5 @@ class ModReswitched(Cog):
await ctx.send(f"{ctx.author.mention}: Gave you mod role.") await ctx.send(f"{ctx.author.mention}: Gave you mod role.")
def setup(bot): async def setup(bot):
bot.add_cog(ModReswitched(bot)) await bot.add_cog(ModReswitched(bot))

View file

@ -156,5 +156,5 @@ class ModTimed(Cog):
add_restriction(target.id, config.mute_role) add_restriction(target.id, config.mute_role)
def setup(bot): async def setup(bot):
bot.add_cog(ModTimed(bot)) await bot.add_cog(ModTimed(bot))

View file

@ -244,5 +244,5 @@ class ModUserlog(Cog):
) )
def setup(bot): async def setup(bot):
bot.add_cog(ModUserlog(bot)) await bot.add_cog(ModUserlog(bot))

View file

@ -42,5 +42,5 @@ class ModWatch(Cog):
await ctx.send(f"{target.mention}: user is now not on watch.") await ctx.send(f"{target.mention}: user is now not on watch.")
def setup(bot): async def setup(bot):
bot.add_cog(ModWatch(bot)) await bot.add_cog(ModWatch(bot))

View file

@ -157,5 +157,5 @@ def check(msg):
return msg.type is MessageType.pins_add return msg.type is MessageType.pins_add
def setup(bot): async def setup(bot):
bot.add_cog(Pin(bot)) await bot.add_cog(Pin(bot))

View file

@ -73,5 +73,5 @@ class Remind(Cog):
await msg.delete() await msg.delete()
def setup(bot): async def setup(bot):
bot.add_cog(Remind(bot)) await bot.add_cog(Remind(bot))

View file

@ -3,7 +3,7 @@ import config
import time import time
import discord import discord
import traceback import traceback
from discord.ext import commands from discord.ext import commands, tasks
from discord.ext.commands import Cog from discord.ext.commands import Cog
from helpers.robocronp import get_crontab, delete_job from helpers.robocronp import get_crontab, delete_job
from helpers.restrictions import remove_restriction from helpers.restrictions import remove_restriction
@ -13,9 +13,14 @@ from helpers.checks import check_if_staff
class Robocronp(Cog): class Robocronp(Cog):
def __init__(self, bot): def __init__(self, bot):
self.bot = bot self.bot = bot
bot.loop.create_task(self.minutely()) self.minutely.start()
bot.loop.create_task(self.hourly()) self.hourly.start()
bot.loop.create_task(self.daily()) self.daily.start()
def cog_unload(self):
self.minutely.cancel()
self.hourly.cancel()
self.daily.cancel()
async def send_data(self): async def send_data(self):
data_files = [discord.File(fpath) for fpath in self.bot.wanted_jsons] data_files = [discord.File(fpath) for fpath in self.bot.wanted_jsons]
@ -113,10 +118,9 @@ class Robocronp(Cog):
f"Cronclean has errored: ```{traceback.format_exc()}```" f"Cronclean has errored: ```{traceback.format_exc()}```"
) )
@tasks.loop(minutes=1)
async def minutely(self): async def minutely(self):
await self.bot.wait_until_ready()
log_channel = self.bot.get_channel(config.botlog_channel) log_channel = self.bot.get_channel(config.botlog_channel)
while not self.bot.is_closed():
try: try:
ctab = get_crontab() ctab = get_crontab()
timestamp = time.time() timestamp = time.time()
@ -133,18 +137,12 @@ class Robocronp(Cog):
await log_channel.send( await log_channel.send(
f"Cron-minutely has errored: ```{traceback.format_exc()}```" f"Cron-minutely has errored: ```{traceback.format_exc()}```"
) )
await asyncio.sleep(60)
@tasks.loop(hours=1)
async def hourly(self): async def hourly(self):
await self.bot.wait_until_ready()
log_channel = self.bot.get_channel(config.botlog_channel) 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: try:
await self.send_data() await self.send_data()
# Handle clean channels # Handle clean channels
for clean_channel in config.hourly_clean_channels: for clean_channel in config.hourly_clean_channels:
await self.clean_channel(clean_channel) await self.clean_channel(clean_channel)
@ -153,15 +151,10 @@ class Robocronp(Cog):
await log_channel.send( await log_channel.send(
f"Cron-hourly has errored: ```{traceback.format_exc()}```" f"Cron-hourly has errored: ```{traceback.format_exc()}```"
) )
# Your stuff that should run an hour after boot
# and after that every hour goes here
@tasks.loop(hours=24)
async def daily(self): async def daily(self):
await self.bot.wait_until_ready()
log_channel = self.bot.get_channel(config.botlog_channel) 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: try:
# Reset verification and algorithm # Reset verification and algorithm
if "cogs.verification" in config.initial_cogs: if "cogs.verification" in config.initial_cogs:
@ -172,10 +165,7 @@ class Robocronp(Cog):
await log_channel.send( await log_channel.send(
f"Cron-daily has errored: ```{traceback.format_exc()}```" 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
def setup(bot): async def setup(bot):
bot.add_cog(Robocronp(bot)) await bot.add_cog(Robocronp(bot))

View file

@ -44,5 +44,5 @@ class SAR(Cog):
) )
def setup(bot): async def setup(bot):
bot.add_cog(SAR(bot)) await bot.add_cog(SAR(bot))

View file

@ -218,5 +218,5 @@ class Verification(Cog):
await chan.send("💢 I don't have permission to do this.") await chan.send("💢 I don't have permission to do this.")
def setup(bot): async def setup(bot):
bot.add_cog(Verification(bot)) await bot.add_cog(Verification(bot))

View file

@ -148,5 +148,5 @@ class YubicoOTP(Cog):
await msg.delete() await msg.delete()
def setup(bot): async def setup(bot):
bot.add_cog(YubicoOTP(bot)) await bot.add_cog(YubicoOTP(bot))