Fixed a bug whereby Guild would always return a success

Also handled error checking on NotFound so that the propper handling can be done.
This commit is contained in:
Kowlin
2020-03-01 19:12:23 +01:00
parent 12273ec482
commit 8d08ac31af

View File

@@ -22,7 +22,8 @@ mute_unmute_issues = {
"permission and the user I'm muting must be " "permission and the user I'm muting must be "
"lower than myself in the role hierarchy." "lower than myself in the role hierarchy."
), ),
"left_guild": _("The user has left the server while we're applying an overwrite."), "left_guild": _("The user has left the server while were applying an overwrite."),
"unknown_channel": _("The channel I tried to mute the user in isn't found."),
} }
_ = T_ _ = T_
@@ -252,27 +253,36 @@ class MuteMixin(MixinMeta):
author = ctx.message.author author = ctx.message.author
guild = ctx.guild guild = ctx.guild
audit_reason = get_audit_reason(author, reason) audit_reason = get_audit_reason(author, reason)
guild_success = True
mute_success = [] mute_success = []
for channel in guild.channels: for channel in guild.channels:
success, issue = await self.mute_user(guild, channel, author, user, audit_reason) success, issue = await self.mute_user(guild, channel, author, user, audit_reason)
mute_success.append((success, issue)) mute_success.append((success, issue))
await asyncio.sleep(0.1) await asyncio.sleep(0.1)
try:
await modlog.create_case( if success is False: # Check the latest entry
self.bot, guild_success = False
guild, break
ctx.message.created_at,
"smute", if guild_success:
user, try:
author, await modlog.create_case(
reason, self.bot,
until=None, guild,
channel=None, ctx.message.created_at,
) "smute",
except RuntimeError as e: user,
await ctx.send(e) author,
await ctx.send(_("User has been muted in this server.")) reason,
until=None,
channel=None,
)
except RuntimeError as e:
await ctx.send(e)
await ctx.send(_("User has been muted in this server."))
else:
await ctx.channel.send(issue)
@commands.group() @commands.group()
@commands.guild_only() @commands.guild_only()
@@ -369,26 +379,50 @@ class MuteMixin(MixinMeta):
guild = ctx.guild guild = ctx.guild
author = ctx.author author = ctx.author
audit_reason = get_audit_reason(author, reason) audit_reason = get_audit_reason(author, reason)
guild_success = True
unmute_success = [] unmute_success = []
for channel in guild.channels: for channel in guild.channels:
success, message = await self.unmute_user(guild, channel, author, user, audit_reason) success, message = await self.unmute_user(guild, channel, author, user, audit_reason)
unmute_success.append((success, message)) unmute_success.append((success, message))
await asyncio.sleep(0.1) await asyncio.sleep(0.1)
try:
await modlog.create_case( if success is False: # Check the latest entry
self.bot, guild_success = False
guild, break
ctx.message.created_at,
"sunmute", if guild_success:
user, try:
author, await modlog.create_case(
reason, self.bot,
until=None, guild,
) ctx.message.created_at,
except RuntimeError as e: "sunmute",
await ctx.send(e) user,
await ctx.send(_("User has been unmuted in this server.")) author,
reason,
until=None,
)
except RuntimeError as e:
await ctx.send(e)
await ctx.send(_("User has been unmuted in this server."))
else:
await ctx.channel.send(issue)
# Reference to L#13 for errors
# Kill muting on the following errors:
# Kill the mutes with returning False
# is_admin
# hierarchy_problem
# left_guild
# permissions_issue
#
# Keep alive on the following errors:
# Keep the muting going with None
# unknown_channel
# already_muted
#
# Else return True
async def mute_user( async def mute_user(
self, self,
@@ -412,7 +446,7 @@ class MuteMixin(MixinMeta):
new_overs.update(send_messages=False, add_reactions=False) new_overs.update(send_messages=False, add_reactions=False)
if all(getattr(permissions, p) is False for p in new_overs.keys()): if all(getattr(permissions, p) is False for p in new_overs.keys()):
return False, _(mute_unmute_issues["already_muted"]) return None, _(mute_unmute_issues["already_muted"])
elif not await is_allowed_by_hierarchy(self.bot, self.settings, guild, author, user): elif not await is_allowed_by_hierarchy(self.bot, self.settings, guild, author, user):
return False, _(mute_unmute_issues["hierarchy_problem"]) return False, _(mute_unmute_issues["hierarchy_problem"])
@@ -423,8 +457,11 @@ class MuteMixin(MixinMeta):
await channel.set_permissions(user, overwrite=overwrites, reason=reason) await channel.set_permissions(user, overwrite=overwrites, reason=reason)
except discord.Forbidden: except discord.Forbidden:
return False, _(mute_unmute_issues["permissions_issue"]) return False, _(mute_unmute_issues["permissions_issue"])
except discord.NotFound: except discord.NotFound as e:
return False, _(mute_unmute_issues["left_guild"]) if e.code == 10003:
return None, _(mute_unmute_issues["unknown_channel"])
elif e.code == 10009:
return False, _(mute_unmute_issues["left_guild"])
else: else:
await self.settings.member(user).set_raw( await self.settings.member(user).set_raw(
"perms_cache", str(channel.id), value=old_overs "perms_cache", str(channel.id), value=old_overs
@@ -463,8 +500,11 @@ class MuteMixin(MixinMeta):
await channel.set_permissions(user, overwrite=overwrites, reason=reason) await channel.set_permissions(user, overwrite=overwrites, reason=reason)
except discord.Forbidden: except discord.Forbidden:
return False, _(mute_unmute_issues["permissions_issue"]) return False, _(mute_unmute_issues["permissions_issue"])
except discord.NotFound: except discord.NotFound as e:
return False, _(mute_unmute_issues["left_guild"]) if e.code == 10003:
return None, _(mute_unmute_issues["unknown_channel"])
elif e.code == 10009:
return False, _(mute_unmute_issues["left_guild"])
else: else:
await self.settings.member(user).clear_raw("perms_cache", str(channel.id)) await self.settings.member(user).clear_raw("perms_cache", str(channel.id))
return True, None return True, None