Skip to content
This repository has been archived by the owner on Oct 2, 2023. It is now read-only.

Commit

Permalink
moved btp strings to yaml file (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
Defelo committed May 1, 2020
1 parent 3bfaf43 commit 31d0f73
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 32 deletions.
59 changes: 27 additions & 32 deletions cogs/betheprofessional.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from database import run_in_thread, db
from models.btp_role import BTPRole
from translations import translations
from util import permission_level, calculate_edit_distance, send_to_changelog


Expand All @@ -23,17 +24,15 @@ async def parse_topics(guild: Guild, topics: str, author: Member) -> List[Role]:
if role in all_topics:
break
elif not role.managed and role > guild.me.top_role:
raise CommandError(
f"Topic `{topic}` not found.\nYou're not the first one to try this, {author.mention}"
)
raise CommandError(translations.f_youre_not_the_first_one(topic, author.mention))
else:
if all_topics:
best_match = min(
(r.name for r in all_topics), key=lambda a: calculate_edit_distance(a.lower(), topic.lower())
)
raise CommandError(f"Topic `{topic}` not found. Did you mean `{best_match}`?")
raise CommandError(translations.f_topic_not_found_did_you_mean(topic, best_match))
else:
raise CommandError(f"Topic `{topic}` not found.")
raise CommandError(translations.f_topic_not_found(topic))
roles.append(role)
return roles

Expand Down Expand Up @@ -62,9 +61,9 @@ async def list_roles(self, ctx: Context):
out = [role.name for role in await list_topics(ctx.guild)]
out.sort(key=str.lower)
if out:
await ctx.send("Available Topics:\n```\n" + ", ".join(out) + "```")
await ctx.send(translations.available_topics_header + "\n```\n" + ", ".join(out) + "```")
else:
await ctx.send("No topics have been registered yet.")
await ctx.send(translations.no_topics_registered)

@commands.command(name="+")
@guild_only()
Expand All @@ -78,11 +77,11 @@ async def add_role(self, ctx: Context, *, topics: str):

await member.add_roles(*roles)
if len(roles) > 1:
await ctx.send(f"{len(roles)} topics have been added successfully.")
await ctx.send(translations.f_cnt_topics_added(len(roles)))
elif len(roles) == 1:
await ctx.send(f"Topic has been added successfully.")
await ctx.send(translations.topic_added)
else:
await ctx.send("No topic has been added.")
await ctx.send(translations.no_topic_added)

@commands.command(name="-")
@guild_only()
Expand All @@ -100,11 +99,11 @@ async def remove_roles(self, ctx: Context, *, topics: str):

await member.remove_roles(*roles)
if len(roles) > 1:
await ctx.send(f"{len(roles)} topics have been removed successfully.")
await ctx.send(translations.f_cnt_topics_removed(len(roles)))
elif len(roles) == 1:
await ctx.send(f"Topic has been removed successfully.")
await ctx.send(translations.topic_removed)
else:
await ctx.send("No topic has been removed.")
await ctx.send(translations.no_topic_removed)

@commands.command(name="*")
@permission_level(1)
Expand All @@ -120,14 +119,12 @@ async def register_role(self, ctx: Context, *, topics: str):
await ctx.send_help(self.register_role)
return

valid_chars = set(string.ascii_letters + string.digits + " !\"#$%&'()*+-./:<=>?[\\]^_`{|}~")
valid_chars = set(string.ascii_letters + string.digits + " !#$%&'()*+-./:<=>?[\\]^_`{|}~")
to_be_created: List[str] = []
roles: List[Role] = []
for topic in names:
if any(c not in valid_chars for c in topic):
raise CommandError(f"Topic name `{topic}` contains invalid characters.")
elif not topic:
raise CommandError("Topic name may not be empty.")
raise CommandError(translations.f_topic_invalid_chars(topic))

for role in guild.roles:
if role.name.lower() == topic.lower():
Expand All @@ -137,13 +134,11 @@ async def register_role(self, ctx: Context, *, topics: str):
continue

if await run_in_thread(db.get, BTPRole, role.id) is not None:
raise CommandError(f"Topic `{topic}` has already been registered.")
raise CommandError(translations.f_topic_already_registered(topic))
if role > ctx.me.top_role:
raise CommandError(
f"Topic could not be registered because `@{role}` is higher than `@{ctx.me.top_role}`."
)
raise CommandError(translations.f_topic_not_registered_too_high(role, ctx.me.top_role))
if role.managed:
raise CommandError(f"Topic could not be registered because `@{role}` cannot be assigned manually.")
raise CommandError(translations.f_topic_not_registered_managed_role(role))
roles.append(role)

for name in to_be_created:
Expand All @@ -153,13 +148,13 @@ async def register_role(self, ctx: Context, *, topics: str):
await run_in_thread(BTPRole.create, role.id)

if len(roles) > 1:
await ctx.send(f"{len(roles)} topics have been registered successfully.")
await ctx.send(translations.f_cnt_topics_registered(len(roles)))
await send_to_changelog(
ctx.guild, "These topics have been registered: " + ", ".join(f"`{t.name}`" for t in roles)
ctx.guild, translations.log_these_topics_registered + " " + ", ".join(f"`{r}`" for r in roles)
)
elif len(roles) == 1:
await ctx.send(f"Topic has been registered successfully.")
await send_to_changelog(ctx.guild, f"The new topic `{roles[0].name}` has been registered.")
await ctx.send(translations.topic_registered)
await send_to_changelog(ctx.guild, translations.f_log_topic_registered(roles[0]))

@commands.command(name="/")
@permission_level(1)
Expand All @@ -181,9 +176,9 @@ async def unregister_role(self, ctx: Context, *, topics: str):
if role.name.lower() == topic.lower():
break
else:
raise CommandError(f"Topic `{topic}` has not been registered.")
raise CommandError(translations.f_topic_not_registered(topic))
if (btp_role := await run_in_thread(db.get, BTPRole, role.id)) is None:
raise CommandError(f"Topic `{topic}` has not been registered.")
raise CommandError(translations.f_topic_not_registered(topic))

roles.append(role)
btp_roles.append(btp_role)
Expand All @@ -192,10 +187,10 @@ async def unregister_role(self, ctx: Context, *, topics: str):
await run_in_thread(db.delete, btp_role)
await role.delete()
if len(roles) > 1:
await ctx.send(f"{len(roles)} topics have been deleted successfully.")
await ctx.send(translations.f_cnt_topics_unregistered(len(roles)))
await send_to_changelog(
ctx.guild, "These topics have been removed: " + ", ".join(f"`{t.name}`" for t in roles)
ctx.guild, translations.log_these_topics_unregistered + " " + ", ".join(f"`{r}`" for r in roles)
)
elif len(roles) == 1:
await ctx.send(f"Topic has been deleted successfully.")
await send_to_changelog(ctx.guild, f"The topic `{roles[0].name}` has been removed.")
await ctx.send(translations.topic_unregistered)
await send_to_changelog(ctx.guild, translations.f_log_topic_unregistered(roles[0]))
28 changes: 28 additions & 0 deletions translations/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,31 @@ prefix_title: Prefix
help_command_title: Help Command
bugs_features_title: Bug Reports / Feature Requests
bugs_features: Please create an issue in the GitHub repository or contact me (<@370876111992913922>) via Discord.

# betheprofessional
youre_not_the_first_one: "Topic `{}` not found.\nYou're not the first one to try this, {}"
topic_not_found: Topic `{}` not found.
topic_not_found_did_you_mean: Topic `{}` not found. Did you mean `{}`?
available_topics_header: "Available Topics:"
no_topics_registered: No topics have been registered yet.

cnt_topics_added: "{} topics have been added successfully."
topic_added: Topic has been added successfully.
no_topic_added: No topic has been added.
cnt_topics_removed: "{} topics have been removed successfully."
topic_removed: Topic has been removed successfully.
no_topic_removed: No topic has been removed.

topic_invalid_chars: Topic name `{}` contains invalid characters.
topic_already_registered: Topic `{}` has already been registered.
topic_not_registered_too_high: Topic could not be registered because `@{}` is higher than `@{}`.
topic_not_registered_managed_role: Topic could not be registered because `@{}` cannot be assigned manually.
cnt_topics_registered: "{} topics have been registered successfully."
log_these_topics_registered: "These topics have been registered:"
topic_registered: Topic has been registered successfully.
log_topic_registered: The new topic `{}` has been registered.
topic_not_registered: Topic `{}` has not been registered.
cnt_topics_unregistered: "{} topics have been deleted successfully."
log_these_topics_unregistered: "These topics have been removed:"
topic_unregistered: Topic has been deleted successfully.
log_topic_unregistered: The topic `{}` has been removed.

0 comments on commit 31d0f73

Please sign in to comment.