[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

@@ -22,15 +22,9 @@ log = logging.getLogger("red.reports")
@cog_i18n(_)
class Reports:
default_guild_settings = {
"output_channel": None,
"active": False,
"next_ticket": 1
}
default_guild_settings = {"output_channel": None, "active": False, "next_ticket": 1}
default_report = {
'report': {}
}
default_report = {"report": {}}
# This can be made configureable later if it
# becomes an issue.
@@ -42,15 +36,14 @@ class Reports:
(timedelta(seconds=5), 1),
(timedelta(minutes=5), 3),
(timedelta(hours=1), 10),
(timedelta(days=1), 24)
(timedelta(days=1), 24),
]
def __init__(self, bot: Red):
self.bot = bot
self.config = Config.get_conf(
self, 78631113035100160, force_registration=True)
self.config = Config.get_conf(self, 78631113035100160, force_registration=True)
self.config.register_guild(**self.default_guild_settings)
self.config.register_custom('REPORT', **self.default_report)
self.config.register_custom("REPORT", **self.default_report)
self.antispam = {}
self.user_cache = []
self.tunnel_store = {}
@@ -59,9 +52,7 @@ class Reports:
@property
def tunnels(self):
return [
x['tun'] for x in self.tunnel_store.values()
]
return [x["tun"] for x in self.tunnel_store.values()]
@checks.admin_or_permissions(manage_guild=True)
@commands.guild_only()
@@ -99,9 +90,7 @@ class Reports:
admin_role = discord.utils.get(
guild.roles, id=await self.bot.db.guild(guild).admin_role()
)
mod_role = discord.utils.get(
guild.roles, id=await self.bot.db.guild(guild).mod_role()
)
mod_role = discord.utils.get(guild.roles, id=await self.bot.db.guild(guild).mod_role())
ret |= any(r in m.roles for r in (mod_role, admin_role))
if perms:
ret |= m.guild_permissions >= perms
@@ -111,10 +100,13 @@ class Reports:
return ret
async def discover_guild(
self, author: discord.User, *,
mod: bool=False,
permissions: Union[discord.Permissions, dict]=None,
prompt: str=""):
self,
author: discord.User,
*,
mod: bool = False,
permissions: Union[discord.Permissions, dict] = None,
prompt: str = ""
):
"""
discovers which of shared guilds between the bot
and provided user based on conditions (mod or permissions is an or)
@@ -151,13 +143,9 @@ class Reports:
return m.author == author and m.channel == dm.channel
try:
message = await self.bot.wait_for(
'message', check=pred, timeout=45
)
message = await self.bot.wait_for("message", check=pred, timeout=45)
except asyncio.TimeoutError:
await author.send(
_("You took too long to select. Try again later.")
)
await author.send(_("You took too long to select. Try again later."))
return None
try:
@@ -187,35 +175,31 @@ class Reports:
if await self.bot.embed_requested(channel, author):
em = discord.Embed(description=report)
em.set_author(
name=_('Report from {0.display_name}').format(author),
icon_url=author.avatar_url
name=_("Report from {0.display_name}").format(author), icon_url=author.avatar_url
)
em.set_footer(text=_("Report #{}").format(ticket_number))
send_content = None
else:
em = None
send_content = _(
'Report from {author.mention} (Ticket #{number})'
).format(author=author, number=ticket_number)
send_content = _("Report from {author.mention} (Ticket #{number})").format(
author=author, number=ticket_number
)
send_content += "\n" + report
try:
await Tunnel.message_forwarder(
destination=channel,
content=send_content,
embed=em,
files=files
destination=channel, content=send_content, embed=em, files=files
)
except (discord.Forbidden, discord.HTTPException):
return None
await self.config.custom('REPORT', guild.id, ticket_number).report.set(
{'user_id': author.id, 'report': report}
await self.config.custom("REPORT", guild.id, ticket_number).report.set(
{"user_id": author.id, "report": report}
)
return ticket_number
@commands.group(name="report", invoke_without_command=True)
async def report(self, ctx: commands.Context, *, _report: str=""):
async def report(self, ctx: commands.Context, *, _report: str = ""):
"""
Follow the prompts to make a report
@@ -226,8 +210,7 @@ class Reports:
guild = ctx.guild
if guild is None:
guild = await self.discover_guild(
author,
prompt=_("Select a server to make a report in by number.")
author, prompt=_("Select a server to make a report in by number.")
)
else:
try:
@@ -238,24 +221,23 @@ class Reports:
return
g_active = await self.config.guild(guild).active()
if not g_active:
return await author.send(
_("Reporting has not been enabled for this server")
)
return await author.send(_("Reporting has not been enabled for this server"))
if guild.id not in self.antispam:
self.antispam[guild.id] = {}
if author.id not in self.antispam[guild.id]:
self.antispam[guild.id][author.id] = AntiSpam(self.intervals)
if self.antispam[guild.id][author.id].spammy:
return await author.send(
_("You've sent a few too many of these recently. "
"Contact a server admin to resolve this, or try again "
"later.")
_(
"You've sent a few too many of these recently. "
"Contact a server admin to resolve this, or try again "
"later."
)
)
if author.id in self.user_cache:
return await author.send(
_("Finish making your prior report "
"before making an additional one")
_("Finish making your prior report " "before making an additional one")
)
if ctx.guild:
@@ -273,13 +255,13 @@ class Reports:
else:
try:
dm = await author.send(
_("Please respond to this message with your Report."
"\nYour report should be a single message")
_(
"Please respond to this message with your Report."
"\nYour report should be a single message"
)
)
except discord.Forbidden:
await ctx.send(
_("This requires DMs enabled.")
)
await ctx.send(_("This requires DMs enabled."))
self.user_cache.remove(author.id)
return
@@ -287,25 +269,17 @@ class Reports:
return m.author == author and m.channel == dm.channel
try:
message = await self.bot.wait_for(
'message', check=pred, timeout=180
)
message = await self.bot.wait_for("message", check=pred, timeout=180)
except asyncio.TimeoutError:
await author.send(
_("You took too long. Try again later.")
)
await author.send(_("You took too long. Try again later."))
else:
val = await self.send_report(message, guild)
with contextlib.suppress(discord.Forbidden, discord.HTTPException):
if val is None:
await author.send(
_("There was an error sending your report.")
)
await author.send(_("There was an error sending your report."))
else:
await author.send(
_("Your report was submitted. (Ticket #{})").format(val)
)
await author.send(_("Your report was submitted. (Ticket #{})").format(val))
self.antispam[guild.id][author.id].stamp()
self.user_cache.remove(author.id)
@@ -318,18 +292,14 @@ class Reports:
return
_id = payload.message_id
t = next(filter(
lambda x: _id in x[1]['msgs'],
self.tunnel_store.items()
), None)
t = next(filter(lambda x: _id in x[1]["msgs"], self.tunnel_store.items()), None)
if t is None:
return
tun = t[1]['tun']
tun = t[1]["tun"]
if payload.user_id in [x.id for x in tun.members]:
await tun.react_close(
uid=payload.user_id,
message=_("{closer} has closed the correspondence")
uid=payload.user_id, message=_("{closer} has closed the correspondence")
)
self.tunnel_store.pop(t[0], None)
@@ -337,12 +307,12 @@ class Reports:
for k, v in self.tunnel_store.items():
topic = _("Re: ticket# {1} in {0.name}").format(*k)
# Tunnels won't forward unintended messages, this is safe
msgs = await v['tun'].communicate(message=message, topic=topic)
msgs = await v["tun"].communicate(message=message, topic=topic)
if msgs:
self.tunnel_store[k]['msgs'] = msgs
self.tunnel_store[k]["msgs"] = msgs
@checks.mod_or_permissions(manage_members=True)
@report.command(name='interact')
@report.command(name="interact")
async def response(self, ctx, ticket_number: int):
"""
opens a message tunnel between things you say in this channel
@@ -353,27 +323,24 @@ class Reports:
# note, mod_or_permissions is an implicit guild_only
guild = ctx.guild
rec = await self.config.custom(
'REPORT', guild.id, ticket_number).report()
rec = await self.config.custom("REPORT", guild.id, ticket_number).report()
try:
user = guild.get_member(rec.get('user_id'))
user = guild.get_member(rec.get("user_id"))
except KeyError:
return await ctx.send(
_("That ticket doesn't seem to exist")
)
return await ctx.send(_("That ticket doesn't seem to exist"))
if user is None:
return await ctx.send(
_("That user isn't here anymore.")
)
return await ctx.send(_("That user isn't here anymore."))
tun = Tunnel(recipient=user, origin=ctx.channel, sender=ctx.author)
if tun is None:
return await ctx.send(
_("Either you or the user you are trying to reach already "
"has an open communication.")
_(
"Either you or the user you are trying to reach already "
"has an open communication."
)
)
big_topic = _(
@@ -387,18 +354,13 @@ class Reports:
"\nTunnels are not persistent across bot restarts."
)
topic = big_topic.format(
ticketnum=ticket_number,
who=_("A moderator in `{guild.name}` has").format(guild=guild)
ticketnum=ticket_number, who=_("A moderator in `{guild.name}` has").format(guild=guild)
)
try:
m = await tun.communicate(
message=ctx.message, topic=topic, skip_message_content=True
)
m = await tun.communicate(message=ctx.message, topic=topic, skip_message_content=True)
except discord.Forbidden:
await ctx.send(_("User has disabled DMs."))
tun.close()
else:
self.tunnel_store[(guild, ticket_number)] = {'tun': tun, 'msgs': m}
await ctx.send(
big_topic.format(who=_("You have"), ticketnum=ticket_number)
)
self.tunnel_store[(guild, ticket_number)] = {"tun": tun, "msgs": m}
await ctx.send(big_topic.format(who=_("You have"), ticketnum=ticket_number))