Fixes after rebase

This commit is contained in:
Mary 2023-03-03 22:20:05 +01:00
parent d7852c2307
commit 5d43842e3c
3 changed files with 42 additions and 20 deletions

View file

@ -61,13 +61,21 @@ bot.config = config
bot.script_name = script_name bot.script_name = script_name
bot.wanted_jsons = wanted_jsons bot.wanted_jsons = wanted_jsons
async def get_channel_safe(self, id):
res = self.get_channel(id)
if res is None:
res = await self.fetch_channel(id)
return res
commands.Bot.get_channel_safe = get_channel_safe
@bot.event @bot.event
async def on_ready(): async def on_ready():
aioh = {"User-Agent": f"{script_name}/1.0'"} aioh = {"User-Agent": f"{script_name}/1.0'"}
bot.aiosession = aiohttp.ClientSession(headers=aioh) bot.aiosession = aiohttp.ClientSession(headers=aioh)
bot.app_info = await bot.application_info() bot.app_info = await bot.application_info()
bot.botlog_channel = bot.get_channel(config.botlog_channel) bot.botlog_channel = await bot.get_channel_safe(config.botlog_channel)
log.info( log.info(
f"\nLogged in as: {bot.user.name} - " f"\nLogged in as: {bot.user.name} - "
@ -107,7 +115,7 @@ async def on_command(ctx):
@bot.event @bot.event
async def on_error(event_method, *args, **kwargs): async def on_error(event_method, *args, **kwargs):
log.error(f"Error on {event_method}: {sys.exc_info()}") log.exception(f"Error on {event_method}:")
@bot.event @bot.event

View file

@ -24,7 +24,7 @@ class Robocronp(Cog):
async def send_data(self): async def send_data(self):
await self.bot.wait_until_ready() await self.bot.wait_until_ready()
data_files = [discord.File(fpath) for fpath in self.bot.wanted_jsons] data_files = [discord.File(fpath) for fpath in self.bot.wanted_jsons]
log_channel = self.bot.get_channel(config.botlog_channel) log_channel = await self.bot.get_channel_safe(config.botlog_channel)
await log_channel.send("Hourly data backups:", files=data_files) await log_channel.send("Hourly data backups:", files=data_files)
@commands.guild_only() @commands.guild_only()
@ -62,7 +62,7 @@ class Robocronp(Cog):
async def do_jobs(self, ctab, jobtype, timestamp): async def do_jobs(self, ctab, jobtype, timestamp):
await self.bot.wait_until_ready() await self.bot.wait_until_ready()
log_channel = self.bot.get_channel(config.botlog_channel) log_channel = await self.bot.get_channel_safe(config.botlog_channel)
for job_name in ctab[jobtype][timestamp]: for job_name in ctab[jobtype][timestamp]:
try: try:
job_details = ctab[jobtype][timestamp][job_name] job_details = ctab[jobtype][timestamp][job_name]
@ -101,8 +101,8 @@ class Robocronp(Cog):
async def clean_channel(self, channel_id): async def clean_channel(self, channel_id):
await self.bot.wait_until_ready() await self.bot.wait_until_ready()
log_channel = self.bot.get_channel(config.botlog_channel) log_channel = await self.bot.get_channel_safe(config.botlog_channel)
channel = self.bot.get_channel(channel_id) channel = await self.bot.get_channel_safe(channel_id)
try: try:
done_cleaning = False done_cleaning = False
count = 0 count = 0
@ -123,7 +123,7 @@ class Robocronp(Cog):
@tasks.loop(minutes=1) @tasks.loop(minutes=1)
async def minutely(self): async def minutely(self):
await self.bot.wait_until_ready() await self.bot.wait_until_ready()
log_channel = self.bot.get_channel(config.botlog_channel) log_channel = await self.bot.get_channel_safe(config.botlog_channel)
try: try:
ctab = get_crontab() ctab = get_crontab()
timestamp = time.time() timestamp = time.time()
@ -144,7 +144,7 @@ class Robocronp(Cog):
@tasks.loop(hours=1) @tasks.loop(hours=1)
async def hourly(self): async def hourly(self):
await self.bot.wait_until_ready() await self.bot.wait_until_ready()
log_channel = self.bot.get_channel(config.botlog_channel) log_channel = await self.bot.get_channel_safe(config.botlog_channel)
try: try:
await self.send_data() await self.send_data()
# Handle clean channels # Handle clean channels
@ -159,11 +159,11 @@ class Robocronp(Cog):
@tasks.loop(hours=24) @tasks.loop(hours=24)
async def daily(self): async def daily(self):
await self.bot.wait_until_ready() await self.bot.wait_until_ready()
log_channel = self.bot.get_channel(config.botlog_channel) log_channel = await self.bot.get_channel_safe(config.botlog_channel)
try: try:
# Reset verification and algorithm # Reset verification and algorithm
if "cogs.verification" in config.initial_cogs: if "cogs.verification" in config.initial_cogs:
verif_channel = self.bot.get_channel(config.welcome_channel) verif_channel = await self.bot.get_channel_safe(config.welcome_channel)
await self.bot.do_resetalgo(verif_channel, "daily robocronp") await self.bot.do_resetalgo(verif_channel, "daily robocronp")
except: except:
# Don't kill cronjobs if something goes wrong. # Don't kill cronjobs if something goes wrong.

View file

@ -114,14 +114,18 @@ class RyujinxReactionRoles(Cog):
async def handle_offline_reaction_add(self): async def handle_offline_reaction_add(self):
await self.bot.wait_until_ready() await self.bot.wait_until_ready()
for reaction in self.m.reactions: for reaction in self.m.reactions:
for user in await reaction.users().flatten(): reactions_users = []
async for user in reaction.users():
reactions_users.append(user)
for user in reactions_users:
emoji_name = str(reaction.emoji) emoji_name = str(reaction.emoji)
if emoji_name[0] == "<": if emoji_name[0] == "<":
emoji_name = emoji_name[1:-1] emoji_name = emoji_name[1:-1]
if self.get_role_from_emoji(emoji_name) is not None: if self.get_role_from_emoji(emoji_name) is not None:
role = self.get_role(emoji_name) role = self.get_role(emoji_name)
if not user in role.members and not user.bot: if not user in role.members and not user.bot and type(user) is discord.Member:
await user.add_roles(role) await user.add_roles(role)
else: else:
await self.m.clear_reaction(reaction.emoji) await self.m.clear_reaction(reaction.emoji)
@ -134,10 +138,16 @@ class RyujinxReactionRoles(Cog):
if emoji_name[0] == "<": if emoji_name[0] == "<":
emoji_name = emoji_name[1:-1] emoji_name = emoji_name[1:-1]
reactions_users = []
async for user in reaction.users():
reactions_users.append(user)
role = self.get_role(emoji_name) role = self.get_role(emoji_name)
for user in role.members: for user in role.members:
if user not in await reaction.users().flatten(): if user not in reactions_users:
await self.m.guild.get_member(user.id).remove_roles(role) member = self.m.guild.get_member(user.id)
if member is not None:
await member.remove_roles(role)
def load_reaction_config(self): def load_reaction_config(self):
if not os.path.exists(self.file): if not os.path.exists(self.file):
@ -164,9 +174,14 @@ class RyujinxReactionRoles(Cog):
guild = self.bot.guilds[0] # The ryu guild in which the bot is. guild = self.bot.guilds[0] # The ryu guild in which the bot is.
channel = guild.get_channel(self.channel_id) channel = guild.get_channel(self.channel_id)
m = discord.utils.get( if channel is None:
await channel.history().flatten(), id=self.reaction_config["id"] channel = await guild.fetch_channel(self.channel_id)
)
history = []
async for msg in channel.history():
history.append(msg)
m = discord.utils.get(history, id=self.reaction_config["id"])
if m is None: if m is None:
self.reaction_config["id"] = None self.reaction_config["id"] = None
@ -183,9 +198,7 @@ class RyujinxReactionRoles(Cog):
await self.handle_offline_reaction_remove() await self.handle_offline_reaction_remove()
else: else:
self.m = discord.utils.get( self.m = m
await channel.history().flatten(), id=self.reaction_config["id"]
)
self.msg_id = self.m.id self.msg_id = self.m.id
await self.m.edit(embed=await self.generate_embed()) await self.m.edit(embed=await self.generate_embed())
@ -199,6 +212,7 @@ class RyujinxReactionRoles(Cog):
@Cog.listener() @Cog.listener()
async def on_ready(self): async def on_ready(self):
await self.bot.wait_until_ready()
self.reaction_config = self.load_reaction_config() self.reaction_config = self.load_reaction_config()
await self.reload_reaction_message() await self.reload_reaction_message()