[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

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

View File

@@ -10,11 +10,13 @@ from .log import LOG
__all__ = ["TriviaSession"]
_REVEAL_MESSAGES = ("I know this one! {}!", "Easy: {}.",
"Oh really? It's {} of course.")
_FAIL_MESSAGES = ("To the next one I guess...", "Moving on...",
"I'm sure you'll know the answer of the next one.",
"\N{PENSIVE FACE} Next one.")
_REVEAL_MESSAGES = ("I know this one! {}!", "Easy: {}.", "Oh really? It's {} of course.")
_FAIL_MESSAGES = (
"To the next one I guess...",
"Moving on...",
"I'm sure you'll know the answer of the next one.",
"\N{PENSIVE FACE} Next one.",
)
class TriviaSession():
@@ -49,10 +51,7 @@ class TriviaSession():
"""
def __init__(self,
ctx,
question_list: dict,
settings: dict):
def __init__(self, ctx, question_list: dict, settings: dict):
self.ctx = ctx
list_ = list(question_list.items())
random.shuffle(list_)
@@ -128,9 +127,9 @@ class TriviaSession():
num_lists = len(list_names)
if num_lists > 2:
# at least 3 lists, join all but last with comma
msg = ", ".join(list_names[:num_lists-1])
msg = ", ".join(list_names[:num_lists - 1])
# join onto last with "and"
msg = " and ".join((msg, list_names[num_lists-1]))
msg = " and ".join((msg, list_names[num_lists - 1]))
else:
# either 1 or 2 lists, join together with "and"
msg = " and ".join(list_names)
@@ -150,10 +149,7 @@ class TriviaSession():
answers = _parse_answers(answers)
yield question, answers
async def wait_for_answer(self,
answers,
delay: float,
timeout: float):
async def wait_for_answer(self, answers, delay: float, timeout: float):
"""Wait for a correct answer, and then respond.
Scores are also updated in this method.
@@ -178,7 +174,8 @@ class TriviaSession():
"""
try:
message = await self.ctx.bot.wait_for(
"message", check=self.check_answer(answers), timeout=delay)
"message", check=self.check_answer(answers), timeout=delay
)
except asyncio.TimeoutError:
if time.time() - self._last_response >= timeout:
await self.ctx.send("Guys...? Well, I guess I'll stop then.")
@@ -194,8 +191,7 @@ class TriviaSession():
await self.ctx.send(reply)
else:
self.scores[message.author] += 1
reply = "You got it {}! **+1** to you!".format(
message.author.display_name)
reply = "You got it {}! **+1** to you!".format(message.author.display_name)
await self.ctx.send(reply)
return True
@@ -218,9 +214,11 @@ class TriviaSession():
"""
answers = tuple(s.lower() for s in answers)
def _pred(message: discord.Message):
early_exit = (message.channel != self.ctx.channel
or message.author == self.ctx.guild.me)
early_exit = (
message.channel != self.ctx.channel or message.author == self.ctx.guild.me
)
if early_exit:
return False
@@ -260,8 +258,7 @@ class TriviaSession():
"""Cancel whichever tasks this session is running."""
self._task.cancel()
channel = self.ctx.channel
LOG.debug("Force stopping trivia session; #%s in %s", channel,
channel.guild.id)
LOG.debug("Force stopping trivia session; #%s in %s", channel, channel.guild.id)
async def pay_winner(self, multiplier: float):
"""Pay the winner of this trivia session.
@@ -275,8 +272,7 @@ class TriviaSession():
paid.
"""
(winner, score) = next((tup for tup in self.scores.most_common(1)),
(None, None))
(winner, score) = next((tup for tup in self.scores.most_common(1)), (None, None))
me_ = self.ctx.guild.me
if winner is not None and winner != me_ and score > 0:
contestants = list(self.scores.keys())
@@ -285,13 +281,12 @@ class TriviaSession():
if len(contestants) >= 3:
amount = int(multiplier * score)
if amount > 0:
LOG.debug("Paying trivia winner: %d credits --> %s",
amount, str(winner))
LOG.debug("Paying trivia winner: %d credits --> %s", amount, str(winner))
await deposit_credits(winner, int(multiplier * score))
await self.ctx.send(
"Congratulations, {0}, you have received {1} credits"
" for coming first.".format(winner.display_name,
amount))
" for coming first.".format(winner.display_name, amount)
)
def _parse_answers(answers):

View File

@@ -26,8 +26,7 @@ class Trivia:
def __init__(self):
self.trivia_sessions = []
self.conf = Config.get_conf(
self, identifier=UNIQUE_ID, force_registration=True)
self.conf = Config.get_conf(self, identifier=UNIQUE_ID, force_registration=True)
self.conf.register_guild(
max_score=10,
@@ -36,10 +35,10 @@ class Trivia:
bot_plays=False,
reveal_answer=True,
payout_multiplier=0.0,
allow_override=True)
allow_override=True,
)
self.conf.register_member(
wins=0, games=0, total_score=0)
self.conf.register_member(wins=0, games=0, total_score=0)
@commands.group()
@commands.guild_only()
@@ -60,7 +59,8 @@ class Trivia:
"Payout multiplier: {payout_multiplier}\n"
"Allow lists to override settings: {allow_override}"
"".format(**settings_dict),
lang="py")
lang="py",
)
await ctx.send(msg)
@triviaset.command(name="maxscore")
@@ -81,8 +81,7 @@ class Trivia:
return
settings = self.conf.guild(ctx.guild)
await settings.delay.set(seconds)
await ctx.send("Done. Maximum seconds to answer set to {}."
"".format(seconds))
await ctx.send("Done. Maximum seconds to answer set to {}." "".format(seconds))
@triviaset.command(name="stopafter")
async def triviaset_stopafter(self, ctx: commands.Context, seconds: float):
@@ -92,38 +91,41 @@ class Trivia:
await ctx.send("Must be larger than the answer time limit.")
return
await settings.timeout.set(seconds)
await ctx.send("Done. Trivia sessions will now time out after {}"
" seconds of no responses.".format(seconds))
await ctx.send(
"Done. Trivia sessions will now time out after {}"
" seconds of no responses.".format(seconds)
)
@triviaset.command(name="override")
async def triviaset_allowoverride(self,
ctx: commands.Context,
enabled: bool):
async def triviaset_allowoverride(self, ctx: commands.Context, enabled: bool):
"""Allow/disallow trivia lists to override settings."""
settings = self.conf.guild(ctx.guild)
await settings.allow_override.set(enabled)
enabled = "now" if enabled else "no longer"
await ctx.send("Done. Trivia lists can {} override the trivia settings"
" for this server.".format(enabled))
await ctx.send(
"Done. Trivia lists can {} override the trivia settings"
" for this server.".format(enabled)
)
@triviaset.command(name="botplays")
async def trivaset_bot_plays(self,
ctx: commands.Context,
true_or_false: bool):
async def trivaset_bot_plays(self, ctx: commands.Context, true_or_false: bool):
"""Set whether or not the bot gains points.
If enabled, the bot will gain a point if no one guesses correctly.
"""
settings = self.conf.guild(ctx.guild)
await settings.bot_plays.set(true_or_false)
await ctx.send("Done. " + (
"I'll gain a point if users don't answer in time." if true_or_false
else "Alright, I won't embarass you at trivia anymore."))
await ctx.send(
"Done. "
+ (
"I'll gain a point if users don't answer in time."
if true_or_false
else "Alright, I won't embarass you at trivia anymore."
)
)
@triviaset.command(name="revealanswer")
async def trivaset_reveal_answer(self,
ctx: commands.Context,
true_or_false: bool):
async def trivaset_reveal_answer(self, ctx: commands.Context, true_or_false: bool):
"""Set whether or not the answer is revealed.
If enabled, the bot will reveal the answer if no one guesses correctly
@@ -131,15 +133,18 @@ class Trivia:
"""
settings = self.conf.guild(ctx.guild)
await settings.reveal_answer.set(true_or_false)
await ctx.send("Done. " + (
"I'll reveal the answer if no one knows it." if true_or_false else
"I won't reveal the answer to the questions anymore."))
await ctx.send(
"Done. "
+ (
"I'll reveal the answer if no one knows it."
if true_or_false
else "I won't reveal the answer to the questions anymore."
)
)
@triviaset.command(name="payout")
@check_global_setting_admin()
async def triviaset_payout_multiplier(self,
ctx: commands.Context,
multiplier: float):
async def triviaset_payout_multiplier(self, ctx: commands.Context, multiplier: float):
"""Set the payout multiplier.
This can be any positive decimal number. If a user wins trivia when at
@@ -155,8 +160,7 @@ class Trivia:
return
await settings.payout_multiplier.set(multiplier)
if not multiplier:
await ctx.send("Done. I will no longer reward the winner with a"
" payout.")
await ctx.send("Done. I will no longer reward the winner with a" " payout.")
return
await ctx.send("Done. Payout multiplier set to {}.".format(multiplier))
@@ -174,8 +178,7 @@ class Trivia:
categories = [c.lower() for c in categories]
session = self._get_trivia_session(ctx.channel)
if session is not None:
await ctx.send(
"There is already an ongoing trivia session in this channel.")
await ctx.send("There is already an ongoing trivia session in this channel.")
return
trivia_dict = {}
authors = []
@@ -185,21 +188,26 @@ class Trivia:
try:
dict_ = self.get_trivia_list(category)
except FileNotFoundError:
await ctx.send("Invalid category `{0}`. See `{1}trivia list`"
" for a list of trivia categories."
"".format(category, ctx.prefix))
await ctx.send(
"Invalid category `{0}`. See `{1}trivia list`"
" for a list of trivia categories."
"".format(category, ctx.prefix)
)
except InvalidListError:
await ctx.send("There was an error parsing the trivia list for"
" the `{}` category. It may be formatted"
" incorrectly.".format(category))
await ctx.send(
"There was an error parsing the trivia list for"
" the `{}` category. It may be formatted"
" incorrectly.".format(category)
)
else:
trivia_dict.update(dict_)
authors.append(trivia_dict.pop("AUTHOR", None))
continue
return
if not trivia_dict:
await ctx.send("The trivia list was parsed successfully, however"
" it appears to be empty!")
await ctx.send(
"The trivia list was parsed successfully, however" " it appears to be empty!"
)
return
settings = await self.conf.guild(ctx.guild).all()
config = trivia_dict.pop("CONFIG", None)
@@ -215,13 +223,16 @@ class Trivia:
"""Stop an ongoing trivia session."""
session = self._get_trivia_session(ctx.channel)
if session is None:
await ctx.send(
"There is no ongoing trivia session in this channel.")
await ctx.send("There is no ongoing trivia session in this channel.")
return
author = ctx.author
auth_checks = (await ctx.bot.is_owner(author), await
ctx.bot.is_mod(author), await ctx.bot.is_admin(author),
author == ctx.guild.owner, author == session.ctx.author)
auth_checks = (
await ctx.bot.is_owner(author),
await ctx.bot.is_mod(author),
await ctx.bot.is_admin(author),
author == ctx.guild.owner,
author == session.ctx.author,
)
if any(auth_checks):
await session.end_game()
session.force_stop()
@@ -234,8 +245,7 @@ class Trivia:
"""List available trivia categories."""
lists = set(p.stem for p in self._all_lists())
msg = box("**Available trivia lists**\n\n{}"
"".format(", ".join(sorted(lists))))
msg = box("**Available trivia lists**\n\n{}" "".format(", ".join(sorted(lists))))
if len(msg) > 1000:
await ctx.author.send(msg)
return
@@ -256,10 +266,9 @@ class Trivia:
@trivia_leaderboard.command(name="server")
@commands.guild_only()
async def trivia_leaderboard_server(self,
ctx: commands.Context,
sort_by: str="wins",
top: int=10):
async def trivia_leaderboard_server(
self, ctx: commands.Context, sort_by: str = "wins", top: int = 10
):
"""Leaderboard for this server.
<sort_by> can be any of the following fields:
@@ -271,9 +280,11 @@ class Trivia:
"""
key = self._get_sort_key(sort_by)
if key is None:
await ctx.send("Unknown field `{}`, see `{}help trivia "
"leaderboard server` for valid fields to sort by."
"".format(sort_by, ctx.prefix))
await ctx.send(
"Unknown field `{}`, see `{}help trivia "
"leaderboard server` for valid fields to sort by."
"".format(sort_by, ctx.prefix)
)
return
guild = ctx.guild
data = await self.conf.all_members(guild)
@@ -282,10 +293,9 @@ class Trivia:
await self.send_leaderboard(ctx, data, key, top)
@trivia_leaderboard.command(name="global")
async def trivia_leaderboard_global(self,
ctx: commands.Context,
sort_by: str="wins",
top: int=10):
async def trivia_leaderboard_global(
self, ctx: commands.Context, sort_by: str = "wins", top: int = 10
):
"""Global trivia leaderboard.
<sort_by> can be any of the following fields:
@@ -298,9 +308,11 @@ class Trivia:
"""
key = self._get_sort_key(sort_by)
if key is None:
await ctx.send("Unknown field `{}`, see `{}help trivia "
"leaderboard global` for valid fields to sort by."
"".format(sort_by, ctx.prefix))
await ctx.send(
"Unknown field `{}`, see `{}help trivia "
"leaderboard global` for valid fields to sort by."
"".format(sort_by, ctx.prefix)
)
return
data = await self.conf.all_members()
collated_data = {}
@@ -327,11 +339,7 @@ class Trivia:
elif key in ("total", "score", "answers", "correct"):
return "total_score"
async def send_leaderboard(self,
ctx: commands.Context,
data: dict,
key: str,
top: int):
async def send_leaderboard(self, ctx: commands.Context, data: dict, key: str, top: int):
"""Send the leaderboard from the given data.
Parameters
@@ -382,23 +390,34 @@ class Trivia:
items = sorted(items, key=lambda t: t[1][key], reverse=True)
max_name_len = max(map(lambda m: len(str(m)), data.keys()))
# Headers
headers = ("Rank", "Member{}".format(" " * (max_name_len - 6)), "Wins",
"Games Played", "Total Score", "Average Score")
headers = (
"Rank",
"Member{}".format(" " * (max_name_len - 6)),
"Wins",
"Games Played",
"Total Score",
"Average Score",
)
lines = [" | ".join(headers)]
# Header underlines
lines.append(" | ".join(("-" * len(h) for h in headers)))
for rank, tup in enumerate(items, 1):
member, m_data = tup
# Align fields to header width
fields = tuple(map(str, (rank,
member,
m_data["wins"],
m_data["games"],
m_data["total_score"],
round(m_data["average_score"], 2))))
padding = [
" " * (len(h) - len(f)) for h, f in zip(headers, fields)
]
fields = tuple(
map(
str,
(
rank,
member,
m_data["wins"],
m_data["games"],
m_data["total_score"],
round(m_data["average_score"], 2),
),
)
)
padding = [" " * (len(h) - len(f)) for h, f in zip(headers, fields)]
fields = tuple(f + padding[i] for i, f in enumerate(fields))
lines.append(" | ".join(fields).format(member=member, **m_data))
if rank == top:
@@ -418,8 +437,7 @@ class Trivia:
"""
channel = session.ctx.channel
LOG.debug("Ending trivia session; #%s in %s", channel,
channel.guild.id)
LOG.debug("Ending trivia session; #%s in %s", channel, channel.guild.id)
if session in self.trivia_sessions:
self.trivia_sessions.remove(session)
if session.scores:
@@ -462,10 +480,9 @@ class Trivia:
try:
path = next(p for p in self._all_lists() if p.stem == category)
except StopIteration:
raise FileNotFoundError("Could not find the `{}` category"
"".format(category))
raise FileNotFoundError("Could not find the `{}` category" "".format(category))
with path.open(encoding='utf-8') as file:
with path.open(encoding="utf-8") as file:
try:
dict_ = yaml.load(file)
except yaml.error.YAMLError as exc:
@@ -473,14 +490,13 @@ class Trivia:
else:
return dict_
def _get_trivia_session(self,
channel: discord.TextChannel) -> TriviaSession:
return next((session for session in self.trivia_sessions
if session.ctx.channel == channel), None)
def _get_trivia_session(self, channel: discord.TextChannel) -> TriviaSession:
return next(
(session for session in self.trivia_sessions if session.ctx.channel == channel), None
)
def _all_lists(self):
personal_lists = tuple(p.resolve()
for p in cog_data_path(self).glob("*.yaml"))
personal_lists = tuple(p.resolve() for p in cog_data_path(self).glob("*.yaml"))
return personal_lists + tuple(ext_trivia.lists())