[V3] Update code standards (black code format pass) (#1650)

* ran black: code formatter against `redbot/` with `-l 99`

* badge
This commit is contained in:
Michael H
2018-05-14 15:33:24 -04:00
committed by Will
parent e7476edd68
commit b88b5a2601
90 changed files with 3629 additions and 3223 deletions

View File

@@ -27,13 +27,16 @@ class Cleanup:
Tries its best to cleanup after itself if the response is positive.
"""
def author_check(message):
return message.author == ctx.author
prompt = await ctx.send(_('Are you sure you want to delete {} messages? (y/n)').format(number))
response = await ctx.bot.wait_for('message', check=author_check)
prompt = await ctx.send(
_("Are you sure you want to delete {} messages? (y/n)").format(number)
)
response = await ctx.bot.wait_for("message", check=author_check)
if response.content.lower().startswith('y'):
if response.content.lower().startswith("y"):
await prompt.delete()
try:
await response.delete()
@@ -41,14 +44,19 @@ class Cleanup:
pass
return True
else:
await ctx.send(_('Cancelled.'))
await ctx.send(_("Cancelled."))
return False
@staticmethod
async def get_messages_for_deletion(
ctx: commands.Context, channel: discord.TextChannel, number,
check=lambda x: True, limit=100, before=None, after=None,
delete_pinned=False
ctx: commands.Context,
channel: discord.TextChannel,
number,
check=lambda x: True,
limit=100,
before=None,
after=None,
delete_pinned=False,
) -> list:
"""
Gets a list of messages meeting the requirements to be deleted.
@@ -65,9 +73,7 @@ class Cleanup:
while not too_old and len(to_delete) - 1 < number:
message = None
async for message in channel.history(limit=limit,
before=before,
after=after):
async for message in channel.history(limit=limit, before=before, after=after):
if (
(not number or len(to_delete) - 1 < number)
and check(message)
@@ -96,7 +102,9 @@ class Cleanup:
@cleanup.command()
@commands.guild_only()
@commands.bot_has_permissions(manage_messages=True)
async def text(self, ctx: commands.Context, text: str, number: int, delete_pinned: bool=False):
async def text(
self, ctx: commands.Context, text: str, number: int, delete_pinned: bool = False
):
"""Deletes last X messages matching the specified text.
Example:
@@ -122,12 +130,18 @@ class Cleanup:
return False
to_delete = await self.get_messages_for_deletion(
ctx, channel, number, check=check, limit=1000, before=ctx.message,
delete_pinned=delete_pinned)
ctx,
channel,
number,
check=check,
limit=1000,
before=ctx.message,
delete_pinned=delete_pinned,
)
reason = "{}({}) deleted {} messages "\
" containing '{}' in channel {}.".format(author.name,
author.id, len(to_delete), text, channel.id)
reason = "{}({}) deleted {} messages " " containing '{}' in channel {}.".format(
author.name, author.id, len(to_delete), text, channel.id
)
log.info(reason)
if is_bot:
@@ -138,7 +152,9 @@ class Cleanup:
@cleanup.command()
@commands.guild_only()
@commands.bot_has_permissions(manage_messages=True)
async def user(self, ctx: commands.Context, user: str, number: int, delete_pinned: bool=False):
async def user(
self, ctx: commands.Context, user: str, number: int, delete_pinned: bool = False
):
"""Deletes last X messages from specified user.
Examples:
@@ -174,13 +190,17 @@ class Cleanup:
return False
to_delete = await self.get_messages_for_deletion(
ctx, channel, number, check=check, limit=1000, before=ctx.message,
delete_pinned=delete_pinned
ctx,
channel,
number,
check=check,
limit=1000,
before=ctx.message,
delete_pinned=delete_pinned,
)
reason = "{}({}) deleted {} messages " " made by {}({}) in channel {}." "".format(
author.name, author.id, len(to_delete), member or "???", _id, channel.name
)
reason = "{}({}) deleted {} messages "\
" made by {}({}) in channel {}."\
"".format(author.name, author.id, len(to_delete),
member or '???', _id, channel.name)
log.info(reason)
if is_bot:
@@ -192,7 +212,7 @@ class Cleanup:
@cleanup.command()
@commands.guild_only()
@commands.bot_has_permissions(manage_messages=True)
async def after(self, ctx: commands.Context, message_id: int, delete_pinned: bool=False):
async def after(self, ctx: commands.Context, message_id: int, delete_pinned: bool = False):
"""Deletes all messages after specified message.
To get a message id, enable developer mode in Discord's
@@ -207,8 +227,7 @@ class Cleanup:
is_bot = self.bot.user.bot
if not is_bot:
await ctx.send(_("This command can only be used on bots with "
"bot accounts."))
await ctx.send(_("This command can only be used on bots with " "bot accounts."))
return
after = await channel.get_message(message_id)
@@ -221,9 +240,9 @@ class Cleanup:
ctx, channel, 0, limit=None, after=after, delete_pinned=delete_pinned
)
reason = "{}({}) deleted {} messages in channel {}."\
"".format(author.name, author.id,
len(to_delete), channel.name)
reason = "{}({}) deleted {} messages in channel {}." "".format(
author.name, author.id, len(to_delete), channel.name
)
log.info(reason)
await mass_purge(to_delete, channel)
@@ -231,7 +250,7 @@ class Cleanup:
@cleanup.command()
@commands.guild_only()
@commands.bot_has_permissions(manage_messages=True)
async def messages(self, ctx: commands.Context, number: int, delete_pinned: bool=False):
async def messages(self, ctx: commands.Context, number: int, delete_pinned: bool = False):
"""Deletes last X messages.
Example:
@@ -248,14 +267,13 @@ class Cleanup:
return
to_delete = await self.get_messages_for_deletion(
ctx, channel, number, limit=1000, before=ctx.message,
delete_pinned=delete_pinned
ctx, channel, number, limit=1000, before=ctx.message, delete_pinned=delete_pinned
)
to_delete.append(ctx.message)
reason = "{}({}) deleted {} messages in channel {}."\
"".format(author.name, author.id,
number, channel.name)
reason = "{}({}) deleted {} messages in channel {}." "".format(
author.name, author.id, number, channel.name
)
log.info(reason)
if is_bot:
@@ -263,10 +281,10 @@ class Cleanup:
else:
await slow_deletion(to_delete)
@cleanup.command(name='bot')
@cleanup.command(name="bot")
@commands.guild_only()
@commands.bot_has_permissions(manage_messages=True)
async def cleanup_bot(self, ctx: commands.Context, number: int, delete_pinned: bool=False):
async def cleanup_bot(self, ctx: commands.Context, number: int, delete_pinned: bool = False):
"""Cleans up command messages and messages from the bot."""
channel = ctx.message.channel
@@ -278,13 +296,13 @@ class Cleanup:
if not cont:
return
prefixes = await self.bot.get_prefix(ctx.message) # This returns all server prefixes
prefixes = await self.bot.get_prefix(ctx.message) # This returns all server prefixes
if isinstance(prefixes, str):
prefixes = [prefixes]
# In case some idiot sets a null prefix
if '' in prefixes:
prefixes.remove('')
if "" in prefixes:
prefixes.remove("")
def check(m):
if m.author.id == self.bot.user.id:
@@ -293,20 +311,24 @@ class Cleanup:
return True
p = discord.utils.find(m.content.startswith, prefixes)
if p and len(p) > 0:
cmd_name = m.content[len(p):].split(' ')[0]
cmd_name = m.content[len(p):].split(" ")[0]
return bool(self.bot.get_command(cmd_name))
return False
to_delete = await self.get_messages_for_deletion(
ctx, channel, number, check=check, limit=1000, before=ctx.message,
delete_pinned=delete_pinned
ctx,
channel,
number,
check=check,
limit=1000,
before=ctx.message,
delete_pinned=delete_pinned,
)
to_delete.append(ctx.message)
reason = "{}({}) deleted {} "\
" command messages in channel {}."\
"".format(author.name, author.id, len(to_delete),
channel.name)
reason = "{}({}) deleted {} " " command messages in channel {}." "".format(
author.name, author.id, len(to_delete), channel.name
)
log.info(reason)
if is_bot:
@@ -314,10 +336,14 @@ class Cleanup:
else:
await slow_deletion(to_delete)
@cleanup.command(name='self')
@cleanup.command(name="self")
async def cleanup_self(
self, ctx: commands.Context, number: int,
match_pattern: str = None, delete_pinned: bool=False):
self,
ctx: commands.Context,
number: int,
match_pattern: str = None,
delete_pinned: bool = False,
):
"""Cleans up messages owned by the bot.
By default, all messages are cleaned. If a third argument is specified,
@@ -343,8 +369,7 @@ class Cleanup:
me = ctx.guild.me
can_mass_purge = channel.permissions_for(me).manage_messages
use_re = (match_pattern and match_pattern.startswith('r(') and
match_pattern.endswith(')'))
use_re = (match_pattern and match_pattern.startswith("r(") and match_pattern.endswith(")"))
if use_re:
match_pattern = match_pattern[1:] # strip 'r'
@@ -352,10 +377,14 @@ class Cleanup:
def content_match(c):
return bool(match_re.match(c))
elif match_pattern:
def content_match(c):
return match_pattern in c
else:
def content_match(_):
return True
@@ -367,8 +396,13 @@ class Cleanup:
return False
to_delete = await self.get_messages_for_deletion(
ctx, channel, number, check=check, limit=1000, before=ctx.message,
delete_pinned=delete_pinned
ctx,
channel,
number,
check=check,
limit=1000,
before=ctx.message,
delete_pinned=delete_pinned,
)
# Selfbot convenience, delete trigger message
@@ -376,14 +410,13 @@ class Cleanup:
to_delete.append(ctx.message)
if channel.name:
channel_name = 'channel ' + channel.name
channel_name = "channel " + channel.name
else:
channel_name = str(channel)
reason = "{}({}) deleted {} messages "\
"sent by the bot in {}."\
"".format(author.name, author.id, len(to_delete),
channel_name)
reason = "{}({}) deleted {} messages " "sent by the bot in {}." "".format(
author.name, author.id, len(to_delete), channel_name
)
log.info(reason)
if is_bot and can_mass_purge:

View File

@@ -1,15 +1,11 @@
import subprocess
TO_TRANSLATE = [
'../cleanup.py'
]
TO_TRANSLATE = ["../cleanup.py"]
def regen_messages():
subprocess.run(
['pygettext', '-n'] + TO_TRANSLATE
)
subprocess.run(["pygettext", "-n"] + TO_TRANSLATE)
if __name__ == "__main__":
regen_messages()
regen_messages()