[mod] Add cleanup bot command, pep8

This commit is contained in:
Caleb Johnson 2016-11-13 19:46:48 -06:00
parent dfc4c833f5
commit 5a7da3540e

View File

@ -582,8 +582,68 @@ class Mod:
else:
await self.slow_deletion(to_delete)
@cleanup.command(pass_context=True, name='bot')
async def cleanup_bot(self, ctx, number: int, match_pattern: str = None):
@cleanup.command(pass_context=True, no_pm=True, name='bot')
async def cleanup_bot(self, ctx, number: int):
"""Cleans up command messages and messages from the bot"""
channel = ctx.message.channel
author = ctx.message.author
server = channel.server
is_bot = self.bot.user.bot
has_permissions = channel.permissions_for(server.me).manage_messages
prefixes = self.bot.command_prefix
if isinstance(prefixes, str):
prefixes = [prefixes]
elif callable(prefixes):
if asyncio.iscoroutine(prefixes):
await self.bot.say('Coroutine prefixes not yet implemented.')
return
prefixes = prefixes(self.bot, ctx.message)
# In case some idiot sets a null prefix
if '' in prefixes:
prefixes.pop('')
def check(m):
if m.author.id == self.bot.user.id:
return True
elif m == ctx.message:
return True
p = discord.utils.find(m.content.startswith, prefixes)
if p and len(p) > 0:
return m.content[len(p):].startswith(tuple(self.bot.commands))
return False
to_delete = [ctx.message]
if not has_permissions:
await self.bot.say("I'm not allowed to delete messages.")
return
tries_left = 5
tmp = ctx.message
while tries_left and len(to_delete) - 1 < number:
async for message in self.bot.logs_from(channel, limit=100,
before=tmp):
if len(to_delete) - 1 < number and check(message):
to_delete.append(message)
tmp = message
tries_left -= 1
logger.info("{}({}) deleted {} "
" command messages in channel {}"
"".format(author.name, author.id, len(to_delete),
channel.name))
if is_bot:
await self.mass_purge(to_delete)
else:
await self.slow_deletion(to_delete)
@cleanup.command(pass_context=True, name='self')
async def cleanup_self(self, ctx, number: int, match_pattern: str = None):
"""Cleans up messages owned by the bot.
By default, all messages are cleaned. If a third argument is specified,
@ -691,7 +751,6 @@ class Mod:
else:
await self.bot.say("Case #{} updated.".format(case))
@commands.group(pass_context=True)
@checks.is_owner()
async def blacklist(self, ctx):
@ -1223,6 +1282,7 @@ class Mod:
empty = [p for p in iter(discord.PermissionOverwrite())]
return original == empty
def check_folders():
folders = ("data", "data/mod/")
for folder in folders:
@ -1251,6 +1311,7 @@ def check_files():
print("Creating empty {}".format(filename))
dataIO.save_json("data/mod/{}".format(filename), value)
def setup(bot):
global logger
check_folders()