Skip to content

Commit 3800bf9

Browse files
authored
move config and add docker setup (#139)
1 parent eaf56ff commit 3800bf9

File tree

7 files changed

+58
-6
lines changed

7 files changed

+58
-6
lines changed

.version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.4.5
1+
3.5.0

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
# Reaction Light - Changelog
2+
3+
### 3.5.0
4+
- BREAKING CHANGE: `./config.ini` needs to be moved to `/config/config.ini` (change needed to better support Docker - sorry!)
5+
- Add the possibility to define a Discord bot token on Docker startup with the environment variable `TOKEN`, eliminating the need to manually create a `config.ini` file. The `TOKEN` environment variable will have no effect if a `config.ini` file already exists.
6+
27
### 3.4.5
38
- Add German translation ([#137](https://github.com/eibex/reaction-light/pull/137) by [Marc-R2](https://github.com/Marc-R2))
49

bot.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
from disnake.ext import commands
3030
from cogs.utils import database, activity, version, config, schema
3131
from cogs.utils.i18n import Response, StaticResponse
32+
from config import docker
33+
3234

3335
static_response = StaticResponse()
3436

@@ -175,6 +177,7 @@ async def getmember(self, guild, user_id):
175177
member = await guild.fetch_member(user_id)
176178
return member
177179

180+
docker.setup(os.path.realpath(__file__))
178181

179182
rl = ReactionLight()
180183

cogs/utils/config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def __init__(self, directory):
3434

3535
def load(self):
3636
# TODO Expose as public attributes
37-
self.config.read(f"{self.directory}/config.ini")
37+
self.config.read(f"{self.directory}/config/config.ini")
3838
self.token = str(self.config.get("server", "token"))
3939
self.botname = str(self.config.get("server", "name", fallback="Reaction Light"))
4040
self.botcolour = disnake.Colour(int(self.config.get("server", "colour", fallback="0xffff00"), 16))
@@ -45,7 +45,7 @@ def load(self):
4545

4646
def update(self, section, option, value):
4747
self.config[section][option] = value
48-
with open(f"{self.directory}/config.ini", "w") as configfile:
48+
with open(f"{self.directory}/config/config.ini", "w") as configfile:
4949
self.config.write(configfile)
5050
self.load()
5151

config/docker.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
MIT License
3+
4+
Copyright (c) 2019-present eibex
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
"""
24+
25+
26+
import os
27+
from shutil import copy
28+
import configparser
29+
30+
31+
def setup(directory):
32+
token = os.environ.get("TOKEN")
33+
bot_folder = os.path.dirname(directory)
34+
35+
if token and not os.path.isfile(f"{bot_folder}/config/config.ini"):
36+
if os.path.isdir(f"{bot_folder}/config/config.ini"):
37+
if not os.listdir(f"{bot_folder}/config/config.ini"):
38+
# If config.ini was created as a directory by mistake (e.g. Unraid), delete it (only if empty)
39+
os.remove(f"{bot_folder}/config/config.ini")
40+
41+
config = configparser.ConfigParser()
42+
config.read(f"{bot_folder}/config/config.ini.sample")
43+
config["server"]["token"] = token
44+
with open(f"{bot_folder}/config/config.ini", "w") as f:
45+
config.write(f)

setup.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,16 @@
115115
else:
116116
break
117117

118-
copy("{}/config.ini.sample".format(folder), "{}/config.ini".format(folder))
119118

120119
config = configparser.ConfigParser()
121-
config.read("{}/config.ini".format(folder))
120+
config.read(f"{folder}/config/config.ini.sample")
122121
config["server"]["token"] = token
123122
config["server"]["name"] = name
124123
config["server"]["logo"] = logo
125124
config["server"]["system_channel"] = system_channel
126125
config["server"]["colour"] = colour
127126

128-
with open("{}/config.ini".format(folder), "w") as f:
127+
with open(f"{folder}/config/config.ini", "w") as f:
129128
config.write(f)
130129

131130
input("Done. You can now start the bot.")

0 commit comments

Comments
 (0)