[Mod] Added unban event to mod-log

Also implemented an interface to deal with the double event triggering issue in an easier way
This commit is contained in:
Twentysix 2017-03-10 17:40:13 +01:00
parent 361d45e724
commit 7ef878864e

View File

@ -17,7 +17,8 @@ ACTIONS_REPR = {
"KICK" : ("Kick", "\N{WOMANS BOOTS}"), "KICK" : ("Kick", "\N{WOMANS BOOTS}"),
"CMUTE" : ("Channel mute", "\N{SPEAKER WITH CANCELLATION STROKE}"), "CMUTE" : ("Channel mute", "\N{SPEAKER WITH CANCELLATION STROKE}"),
"SMUTE" : ("Server mute", "\N{SPEAKER WITH CANCELLATION STROKE}"), "SMUTE" : ("Server mute", "\N{SPEAKER WITH CANCELLATION STROKE}"),
"SOFTBAN" : ("Softban", "\N{DASH SYMBOL} \N{HAMMER}") "SOFTBAN" : ("Softban", "\N{DASH SYMBOL} \N{HAMMER}"),
"UNBAN" : ("Unban", "\N{DOVE OF PEACE}")
} }
ACTIONS_CASES = { ACTIONS_CASES = {
@ -25,7 +26,8 @@ ACTIONS_CASES = {
"KICK" : True, "KICK" : True,
"CMUTE" : False, "CMUTE" : False,
"SMUTE" : True, "SMUTE" : True,
"SOFTBAN" : True "SOFTBAN" : True,
"UNBAN" : True
} }
default_settings = { default_settings = {
@ -56,6 +58,30 @@ class NoModLogChannel(ModError):
pass pass
class TempCache:
"""
This is how we avoid events such as ban and unban
from triggering twice in the mod-log.
Kinda hacky but functioning
"""
def __init__(self, bot):
self.bot = bot
self._cache = []
def add(self, user, server, action, seconds=1):
tmp = (user.id, server.id, action)
self._cache.append(tmp)
async def delete_value():
await asyncio.sleep(seconds)
self._cache.remove(tmp)
self.bot.loop.create_task(delete_value())
def check(self, user, server, action):
return (user.id, server.id, action) in self._cache
class Mod: class Mod:
"""Moderation tools.""" """Moderation tools."""
@ -72,7 +98,7 @@ class Mod:
self.cache = defaultdict(lambda: deque(maxlen=3)) self.cache = defaultdict(lambda: deque(maxlen=3))
self.cases = dataIO.load_json("data/mod/modlog.json") self.cases = dataIO.load_json("data/mod/modlog.json")
self.last_case = defaultdict(dict) self.last_case = defaultdict(dict)
self._tmp_banned_cache = [] self.temp_cache = TempCache(bot)
perms_cache = dataIO.load_json("data/mod/perms_cache.json") perms_cache = dataIO.load_json("data/mod/perms_cache.json")
self._perms_cache = defaultdict(dict, perms_cache) self._perms_cache = defaultdict(dict, perms_cache)
@ -311,7 +337,7 @@ class Mod:
return return
try: try:
self._tmp_banned_cache.append(user) self.temp_cache.add(user, server, "BAN")
await self.bot.ban(user, days) await self.bot.ban(user, days)
logger.info("{}({}) banned {}({}), deleting {} days worth of messages".format( logger.info("{}({}) banned {}({}), deleting {} days worth of messages".format(
author.name, author.id, user.name, user.id, str(days))) author.name, author.id, user.name, user.id, str(days)))
@ -325,9 +351,6 @@ class Mod:
await self.bot.say("I'm not allowed to do that.") await self.bot.say("I'm not allowed to do that.")
except Exception as e: except Exception as e:
print(e) print(e)
finally:
await asyncio.sleep(1)
self._tmp_banned_cache.remove(user)
@commands.command(no_pm=True, pass_context=True) @commands.command(no_pm=True, pass_context=True)
@checks.admin_or_permissions(ban_members=True) @checks.admin_or_permissions(ban_members=True)
@ -356,7 +379,7 @@ class Mod:
"You can now join the server again.{}".format(invite)) "You can now join the server again.{}".format(invite))
except: except:
pass pass
self._tmp_banned_cache.append(user) self.temp_cache.add(user, server, "BAN")
await self.bot.ban(user, 1) await self.bot.ban(user, 1)
logger.info("{}({}) softbanned {}({}), deleting 1 day worth " logger.info("{}({}) softbanned {}({}), deleting 1 day worth "
"of messages".format(author.name, author.id, user.name, "of messages".format(author.name, author.id, user.name,
@ -366,6 +389,7 @@ class Mod:
mod=author, mod=author,
user=user, user=user,
reason=reason) reason=reason)
self.temp_cache.add(user, server, "UNBAN")
await self.bot.unban(server, user) await self.bot.unban(server, user)
await self.bot.say("Done. Enough chaos.") await self.bot.say("Done. Enough chaos.")
except discord.errors.Forbidden: except discord.errors.Forbidden:
@ -373,9 +397,6 @@ class Mod:
await self.bot.delete_message(msg) await self.bot.delete_message(msg)
except Exception as e: except Exception as e:
print(e) print(e)
finally:
await asyncio.sleep(1)
self._tmp_banned_cache.remove(user)
else: else:
await self.bot.say("I'm not allowed to do that.") await self.bot.say("I'm not allowed to do that.")
@ -1459,7 +1480,7 @@ class Mod:
mentions = set(message.mentions) mentions = set(message.mentions)
if len(mentions) >= max_mentions: if len(mentions) >= max_mentions:
try: try:
self._tmp_banned_cache.append(author) self.temp_cache.add(author, server, "BAN")
await self.bot.ban(author, 1) await self.bot.ban(author, 1)
except: except:
logger.info("Failed to ban member for mention spam in " logger.info("Failed to ban member for mention spam in "
@ -1471,9 +1492,6 @@ class Mod:
user=author, user=author,
reason="Mention spam (Autoban)") reason="Mention spam (Autoban)")
return True return True
finally:
await asyncio.sleep(1)
self._tmp_banned_cache.remove(author)
return False return False
async def on_command(self, command, ctx): async def on_command(self, command, ctx):
@ -1518,12 +1536,18 @@ class Mod:
deleted = await self.check_mention_spam(message) deleted = await self.check_mention_spam(message)
async def on_member_ban(self, member): async def on_member_ban(self, member):
if member not in self._tmp_banned_cache: server = member.server
server = member.server if not self.temp_cache.check(member, server, "BAN"):
await self.new_case(server, await self.new_case(server,
user=member, user=member,
action="BAN") action="BAN")
async def on_member_unban(self, server, user):
if not self.temp_cache.check(user, server, "UNBAN"):
await self.new_case(server,
user=user,
action="UNBAN")
async def check_names(self, before, after): async def check_names(self, before, after):
if before.name != after.name: if before.name != after.name:
if before.id not in self.past_names: if before.id not in self.past_names: