Skip to content

Commit

Permalink
move config and add docker setup (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
eibex authored Feb 17, 2025
1 parent eaf56ff commit 3800bf9
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.4.5
3.5.0
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# Reaction Light - Changelog

### 3.5.0
- BREAKING CHANGE: `./config.ini` needs to be moved to `/config/config.ini` (change needed to better support Docker - sorry!)
- 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.

### 3.4.5
- Add German translation ([#137](https://github.com/eibex/reaction-light/pull/137) by [Marc-R2](https://github.com/Marc-R2))

Expand Down
3 changes: 3 additions & 0 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
from disnake.ext import commands
from cogs.utils import database, activity, version, config, schema
from cogs.utils.i18n import Response, StaticResponse
from config import docker


static_response = StaticResponse()

Expand Down Expand Up @@ -175,6 +177,7 @@ async def getmember(self, guild, user_id):
member = await guild.fetch_member(user_id)
return member

docker.setup(os.path.realpath(__file__))

rl = ReactionLight()

Expand Down
4 changes: 2 additions & 2 deletions cogs/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __init__(self, directory):

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

def update(self, section, option, value):
self.config[section][option] = value
with open(f"{self.directory}/config.ini", "w") as configfile:
with open(f"{self.directory}/config/config.ini", "w") as configfile:
self.config.write(configfile)
self.load()

Expand Down
File renamed without changes.
45 changes: 45 additions & 0 deletions config/docker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""
MIT License
Copyright (c) 2019-present eibex
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""


import os
from shutil import copy
import configparser


def setup(directory):
token = os.environ.get("TOKEN")
bot_folder = os.path.dirname(directory)

if token and not os.path.isfile(f"{bot_folder}/config/config.ini"):
if os.path.isdir(f"{bot_folder}/config/config.ini"):
if not os.listdir(f"{bot_folder}/config/config.ini"):
# If config.ini was created as a directory by mistake (e.g. Unraid), delete it (only if empty)
os.remove(f"{bot_folder}/config/config.ini")

config = configparser.ConfigParser()
config.read(f"{bot_folder}/config/config.ini.sample")
config["server"]["token"] = token
with open(f"{bot_folder}/config/config.ini", "w") as f:
config.write(f)
5 changes: 2 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,16 @@
else:
break

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

config = configparser.ConfigParser()
config.read("{}/config.ini".format(folder))
config.read(f"{folder}/config/config.ini.sample")
config["server"]["token"] = token
config["server"]["name"] = name
config["server"]["logo"] = logo
config["server"]["system_channel"] = system_channel
config["server"]["colour"] = colour

with open("{}/config.ini".format(folder), "w") as f:
with open(f"{folder}/config/config.ini", "w") as f:
config.write(f)

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

0 comments on commit 3800bf9

Please sign in to comment.