Skip to content

Commit

Permalink
feat: localize voting responses
Browse files Browse the repository at this point in the history
  • Loading branch information
Skelmis committed Sep 25, 2022
1 parent ab8f12e commit a144fd1
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 11 deletions.
13 changes: 13 additions & 0 deletions suggestions/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import aiohttp
import disnake
from cooldowns import CallableOnCooldown
from disnake import Locale, LocalizationKeyError
from disnake.ext import commands
from bot_base import BotBase, BotContext, PrefixNotFound

Expand Down Expand Up @@ -395,6 +396,7 @@ async def load_cogs(self):
log.debug("Loaded %s cogs", count)

async def load(self):
self.i18n.load(Path("suggestions/locales"))
await self.state.load()
await self.stats.load()
await self.update_bot_listings()
Expand Down Expand Up @@ -505,3 +507,14 @@ def get_shard_id(self, guild_id: Optional[int]) -> int:
shard_id = (guild_id >> 22) % self.total_shards

return shard_id

def get_locale(self, key: str, locale: Optional[Locale]) -> str:
values = self.i18n.get(key)
if not values:
raise LocalizationKeyError(key)

try:
return values[str(locale)]
except KeyError:
# Default to known translations if not set
return values["en-GB"]
53 changes: 43 additions & 10 deletions suggestions/cogs/suggestion_cog.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

import asyncio
import logging
from typing import TYPE_CHECKING, Optional, cast

Expand Down Expand Up @@ -40,15 +39,23 @@ async def suggestion_up_vote(
suggestion: Suggestion = await Suggestion.from_id(
suggestion_id, inter.guild_id, self.state
)
if suggestion.state != SuggestionState.pending:
if suggestion.state != SuggestionState.pending or 1 == 1:
return await inter.send(
"You can no longer cast votes on this suggestion.", ephemeral=True
self.bot.get_locale(
"SUGGESTION_UP_VOTE_INNER_NO_MORE_CASTING",
inter.locale,
),
ephemeral=True,
)

member_id = inter.author.id
if member_id in suggestion.up_voted_by:
return await inter.send(
"You have already up voted this suggestion.", ephemeral=True
self.bot.get_locale(
"SUGGESTION_UP_VOTE_INNER_ALREADY_VOTED",
inter.locale,
),
ephemeral=True,
)

if member_id in suggestion.down_voted_by:
Expand All @@ -57,7 +64,10 @@ async def suggestion_up_vote(
await self.state.suggestions_db.upsert(suggestion, suggestion)
await suggestion.update_vote_count(self.bot, inter)
await inter.send(
"I have changed your vote from a down vote to an up vote for this suggestion.",
self.bot.get_locale(
"SUGGESTION_UP_VOTE_INNER_MODIFIED_VOTE",
inter.locale,
),
ephemeral=True,
)
log.debug(
Expand All @@ -70,7 +80,13 @@ async def suggestion_up_vote(
suggestion.up_voted_by.add(member_id)
await self.state.suggestions_db.upsert(suggestion, suggestion)
await suggestion.update_vote_count(self.bot, inter)
await inter.send("Thanks!\nI have registered your up vote.", ephemeral=True)
await inter.send(
self.bot.get_locale(
"SUGGESTION_UP_VOTE_INNER_REGISTERED_VOTE",
inter.locale,
),
ephemeral=True,
)
log.debug("Member %s up voted suggestion %s", member_id, suggestion_id)

@components.button_listener()
Expand All @@ -86,13 +102,21 @@ async def suggestion_down_vote(
)
if suggestion.state != SuggestionState.pending:
return await inter.send(
"You can no longer cast votes on this suggestion.", ephemeral=True
self.bot.get_locale(
"SUGGESTION_DOWN_VOTE_INNER_NO_MORE_CASTING",
inter.locale,
),
ephemeral=True,
)

member_id = inter.author.id
if member_id in suggestion.down_voted_by:
return await inter.send(
"You have already down voted this suggestion.", ephemeral=True
self.bot.get_locale(
"SUGGESTION_DOWN_VOTE_INNER_ALREADY_VOTED",
inter.locale,
),
ephemeral=True,
)

if member_id in suggestion.up_voted_by:
Expand All @@ -101,7 +125,10 @@ async def suggestion_down_vote(
await self.state.suggestions_db.upsert(suggestion, suggestion)
await suggestion.update_vote_count(self.bot, inter)
await inter.send(
"I have changed your vote from an up vote to a down vote for this suggestion.",
self.bot.get_locale(
"SUGGESTION_DOWN_VOTE_INNER_MODIFIED_VOTE",
inter.locale,
),
ephemeral=True,
)
log.debug(
Expand All @@ -114,7 +141,13 @@ async def suggestion_down_vote(
suggestion.down_voted_by.add(member_id)
await self.state.suggestions_db.upsert(suggestion, suggestion)
await suggestion.update_vote_count(self.bot, inter)
await inter.send("Thanks!\nI have registered your down vote.", ephemeral=True)
await inter.send(
self.bot.get_locale(
"SUGGESTION_DOWN_VOTE_INNER_REGISTERED_VOTE",
inter.locale,
),
ephemeral=True,
)
log.debug("Member %s down voted suggestion %s", member_id, suggestion_id)

@commands.slash_command(dm_permission=False)
Expand Down
21 changes: 21 additions & 0 deletions suggestions/locales/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Command Locales
---

Please see [this](https://github.com/suggestionsbot/suggestions-bot-rewrite/issues/9) issue.

---

Files are expected in the following format:

```json
{
"<COMMAND>_NAME": "The name of the command",
"<COMMAND>_DESCRIPTION": "The description of the command",
"<COMMAND>_ARG_<ARGUMENT>_NAME": "The name of this argument",
"<COMMAND>_ARG_<ARGUMENT>_DESCRIPTION": "The description of this argument",
"<COMMAND>_INNER_<KEY>": "The value for this key."
}
```

- `_ARG_` is reserved for all parameters to the provided method
- `_INNER_` is reserved for all strings sent within the method itself
10 changes: 9 additions & 1 deletion suggestions/locales/en_GB.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,13 @@
"INFO_ARG_SUPPORT_NAME": "support",
"INFO_ARG_SUPPORT_DESCRIPTION": "Set this to receive info relevant to receiving official support.",
"PING_NAME": "ping",
"PING_DESCRIPTION": "Pong!"
"PING_DESCRIPTION": "Pong!",
"SUGGESTION_UP_VOTE_INNER_NO_MORE_CASTING": "You can no longer cast votes on this suggestion.",
"SUGGESTION_UP_VOTE_INNER_ALREADY_VOTED": "You have already up voted this suggestion.",
"SUGGESTION_UP_VOTE_INNER_MODIFIED_VOTE": "I have changed your vote from a down vote to an up vote for this suggestion.",
"SUGGESTION_UP_VOTE_INNER_REGISTERED_VOTE": "Thanks!\nI have registered your up vote.",
"SUGGESTION_DOWN_VOTE_INNER_NO_MORE_CASTING": "You can no longer cast votes on this suggestion.",
"SUGGESTION_DOWN_VOTE_INNER_ALREADY_VOTED": "You have already down voted this suggestion.",
"SUGGESTION_DOWN_VOTE_INNER_MODIFIED_VOTE": "I have changed your vote from an up vote to a down vote for this suggestion.",
"SUGGESTION_DOWN_VOTE_INNER_REGISTERED_VOTE": "Thanks!\nI have registered your down vote."
}

0 comments on commit a144fd1

Please sign in to comment.