Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

i18n #77

Merged
merged 24 commits into from
Jun 12, 2021
Merged

i18n #77

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.4.3
2.5.0
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# Reaction Light - Changelog

### 2.5.0
- Fix `rl!new` showing unsuccessful reaction when the message creation was actually successful
- Add support for translations. To translate the bot's responses make a copy of [en.json](https://github.com/eibex/reaction-light/blob/master/files/i18n/en.json) and translate the associated strings. Consider sharing your translation as a pull request ([#74](https://github.com/eibex/reaction-light/issues/74) closed by [#77](https://github.com/eibex/reaction-light/pull/77) by [eibex](https://github.com/eibex), [Edwinexd](https://github.com/Edwinexd), and [d7415](https://github.com/d7415)).

### 2.4.3
- Fix deletion of wrong database entries under certain circumstances. Please check that all your messages are working before updating, and restore a backup if necessary ([#73](https://github.com/eibex/reaction-light/issues/73) closed by [#78](https://github.com/eibex/reaction-light/pull/78) by [Edwinexd](https://github.com/Edwinexd))


### 2.4.2
- Fix errors when trying to set a new server-specific system channel with `rl!systemchannel server #channel`

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Light yet powerful reaction role bot coded in discord.py.
- Easy installation, setup, and updating
- Optional update notifications and error reporting to your own Discord server
- No need to rely on developer mode and IDs
- Multiple languages

You can host the bot yourself by configuring the `config.ini` file (manually or via `setup.py`).

Expand Down Expand Up @@ -93,6 +94,7 @@ All commands require an admin role which you can set by using `rl!admin` (requir
- `rl!restart` restarts the bot.
- `rl!update` updates the bot and restarts it. Only works on `git clone` installations. Check the [setup](#setup) section to learn how to install with git.
- `rl!version` reports the bot's current version and the latest available one from GitHub.
- `rl!language` sets a new language for the bot. Currently available languages are: `en-gb` (English), `it-it` (Italian).

### Usage Example
In this example the prefix used is `rl!`. Once you initiate the process, be sure only to answer to the bots questions or the bot might record unwanted messages as instructions. You can still send messages to other channels, and others can send messages to the channel you initiated the process in.
Expand Down
549 changes: 219 additions & 330 deletions bot.py

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions config.ini.sample
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ name = Reaction Light
system_channel =
logo = https://cdn.discordapp.com/attachments/671738683623473163/693451064904515645/spell_holy_weaponmastery.jpg
colour = 0xffff00
language = en-gb
1 change: 1 addition & 0 deletions core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
from .database import *
from .github import *
from .schema import *
from .i18n import *
10 changes: 8 additions & 2 deletions core/activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,14 @@ def load(self):
# Get activities.csv contents
reader = csv.reader(f, delimiter=",")
for row in reader:
activity = row[0]
self.activity_list.append(activity)
try:
activity = row[0]
self.activity_list.append(activity)
except IndexError:
pass

if not self.activity_list:
self.activity_list = ["with reactions"]

self.loop = cycle(self.activity_list)

Expand Down
72 changes: 30 additions & 42 deletions core/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,16 +334,25 @@ def get_admins(self, guild_id: int):
except sqlite3.Error as e:
return e

def insert_guildsettings(self, guild_id: int):
conn = sqlite3.connect(self.database)
cursor = conn.cursor()
notify = 0
channel_id = 0
cursor.execute(
"INSERT OR IGNORE INTO guild_settings ('guild_id', 'notify', 'systemchannel')"
" values(?, ?, ?);",
(guild_id, notify, channel_id),
)
conn.commit()
cursor.close()
conn.close()

def add_systemchannel(self, guild_id, channel_id):
try:
conn = sqlite3.connect(self.database)
cursor = conn.cursor()
notify = 0
cursor.execute(
"INSERT OR IGNORE INTO guild_settings ('guild_id', 'notify', 'systemchannel')"
" values(?, ?, ?);",
(guild_id, notify, channel_id),
)
self.insert_guildsettings(guild_id)
cursor.execute(
"UPDATE guild_settings SET systemchannel = ? WHERE guild_id = ?;",
(channel_id, guild_id),
Expand All @@ -360,12 +369,7 @@ def remove_systemchannel(self, guild_id):
conn = sqlite3.connect(self.database)
cursor = conn.cursor()
channel_id = 0 # Set to false
notify = 0
cursor.execute(
"INSERT OR IGNORE INTO guild_settings ('guild_id', 'notify', 'systemchannel')"
" values(?, ?);",
(guild_id, notify, channel_id),
)
self.insert_guildsettings(guild_id)
cursor.execute(
"UPDATE guild_settings SET systemchannel = ? WHERE guild_id = ?;",
(channel_id, guild_id),
Expand Down Expand Up @@ -532,33 +536,25 @@ def toggle_notify(self, guild_id: int):
try:
conn = sqlite3.connect(self.database)
cursor = conn.cursor()
self.insert_guildsettings(guild_id)
cursor.execute(
"SELECT notify FROM guild_settings WHERE guild_id = ?", (guild_id,)
)
results = cursor.fetchall()
if not results:
# If the guild was not in the table because the command was never used before
notify = 1
systemchannel = 0
notify = results[0][0]
if notify:
notify = 0
cursor.execute(
"INSERT INTO 'guild_settings' ('guild_id', 'notify') values(?, ?, ?);",
(guild_id, systemchannel, notify),
"UPDATE guild_settings SET notify = ? WHERE guild_id = ?",
(notify, guild_id),
)
else:
notify = results[0][0]
if notify:
notify = 0
cursor.execute(
"UPDATE guild_settings SET notify = ? WHERE guild_id = ?",
(notify, guild_id),
)

else:
notify = 1
cursor.execute(
"UPDATE guild_settings SET notify = ? WHERE guild_id = ?",
(notify, guild_id),
)
else:
notify = 1
cursor.execute(
"UPDATE guild_settings SET notify = ? WHERE guild_id = ?",
(notify, guild_id),
)
conn.commit()
cursor.close()
conn.close()
Expand All @@ -573,20 +569,12 @@ def notify(self, guild_id: int):
try:
conn = sqlite3.connect(self.database)
cursor = conn.cursor()
self.insert_guildsettings(guild_id)
cursor.execute(
"SELECT notify FROM guild_settings WHERE guild_id = ?", (guild_id,)
)
results = cursor.fetchall()
if not results:
# If the guild was not in the table because the command was never used before
notify = 0
systemchannel = 0
cursor.execute(
"INSERT INTO 'guild_settings' ('guild_id', `systemchannel`, 'notify') values(?, ?, ?);",
(guild_id, systemchannel, notify),
)
else:
notify = results[0][0]
notify = results[0][0]
cursor.close()
conn.close()
return notify
Expand Down
8 changes: 6 additions & 2 deletions core/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,12 @@ async def latest_changelog():
) as r:
changelog = await r.text()

changelog = changelog.split("###")[1].rstrip("\n") # Only get the latest version changes
changelog = changelog[changelog.index("-"):] # Remove every character up to the first bullet point
changelog = changelog.split("###")[1].rstrip(
"\n"
) # Only get the latest version changes
changelog = changelog[
changelog.index("-") :
] # Remove every character up to the first bullet point
changelog += "\n\n[View more](https://github.com/eibex/reaction-light/blob/master/CHANGELOG.md)"

return changelog
29 changes: 29 additions & 0 deletions core/i18n.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import os
import json


class Response:
def __init__(self, directory, language, prefix):
self.directory = directory
self.language = language
self.prefix = prefix
self.responses = self.load()

def load(self):
data = {}
for file in os.listdir(self.directory):
if file.endswith(".json"):
with open(f"{self.directory}/{file}", encoding="utf-8") as f:
data[file.replace(".json", "")] = json.load(f)
return data

def get(self, item):
try:
response = self.responses[self.language][item]
except KeyError:
response = self.responses["en-gb"][item]
print(
f"Could not find a translation ({self.language}) for the requested i18n item: {item}. Please file an issue on GitHub."
)
response = response.replace("{prefix}", self.prefix)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels like this should be handled in the .format elsewhere, seems odd to only handle this replacement here.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did it like this because it's the only var that is fixed and present across several strings.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it feel redundant to .format(prefix) everywhere?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, although I don't like the concept of one variable being handled differently from the others, might get confusing down the line

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I can see your point too.
I will leave this unresolved for now while I think about it.

return response
Loading