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:
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.")
def setup(bot):
bot.add_cog(Admin(bot))
async def setup(bot):
await bot.add_cog(Admin(bot))

View file

@ -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))

View file

@ -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))

View file

@ -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))

View file

@ -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))

View file

@ -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))

View file

@ -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))

View file

@ -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))

View file

@ -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))

View file

@ -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))

View file

@ -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))

View file

@ -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))

View file

@ -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))

View file

@ -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))

View file

@ -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))

View file

@ -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))

View file

@ -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))

View file

@ -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))

View file

@ -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))

View file

@ -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))

View file

@ -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))

View file

@ -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))

View file

@ -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))

View file

@ -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))

View file

@ -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))

View file

@ -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))