Skip to content

Commit

Permalink
Fix bug where the bot tries to delete a non-existing message
Browse files Browse the repository at this point in the history
  • Loading branch information
laggron42 committed Aug 3, 2019
1 parent eff6c24 commit 7d0e900
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion warnsystem/warnsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,8 @@ async def update_message():
)
finally:
task.cancel()
await message.delete()
if message:
await message.delete()

# all settings
@commands.group()
Expand Down Expand Up @@ -1556,6 +1557,46 @@ async def warnsysteminfo(self, ctx):
).format(self)
)

@listener()
async def on_member_unban(self, guild: discord.Guild, user: discord.User):
# if a member gets unbanned, we check if he was temp banned with warnsystem
# if it was, we remove the case so it won't unban him a second time
async with self.data.guild(guild).temporary_warns() as temp_warns:
to_remove = []
for case in temp_warns:
if case["level"] == 2 or case["member"] != user.id:
continue
to_remove.append(case)
for removal in to_remove:
temp_warns.remove(removal)
if to_remove:
log.info(
f"The temporary ban of user {user} (ID: {user.id}) on guild {guild} "
f"(ID: {guild.id} was cancelled due to his manual unban."
)

@listener()
async def on_member_update(self, before: discord.Member, after: discord.Member):
guild = after.guild
mute_role = guild.get_role(await self.data.guild(guild).mute_role())
if not mute_role:
return
if not (mute_role in before.roles and mute_role not in after.roles):
return
async with self.data.guild(guild).temporary_warns() as temp_warns:
to_remove = []
for case in temp_warns:
if case["level"] == 5 or case["member"] != after.id:
continue
to_remove.append(case)
for removal in to_remove:
temp_warns.remove(removal)
if to_remove:
log.info(
f"The temporary mute of member {after} (ID: {after.id}) on guild {guild} "
f"(ID: {guild.id}) was ended due to a manual unmute (role removed)."
)

@listener()
async def on_command_error(self, ctx, error):
if not isinstance(error, commands.CommandInvokeError):
Expand Down

0 comments on commit 7d0e900

Please sign in to comment.