diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..62b7fa1 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,6 @@ +./.github +./.gitignore +./configuration.json +./readme.md +./history.txt +./docker-compose.yml diff --git a/.gitignore b/.gitignore index 95b7483..5cba454 100644 --- a/.gitignore +++ b/.gitignore @@ -351,4 +351,6 @@ MigrationBackup/ configuration.json ./configuration.json history.txt -./history.txt \ No newline at end of file +./history.txt +./docker-compose.yml +docker-compose.yml diff --git a/README.md b/README.md index c6aa662..0f41ac8 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,10 @@ pip3 install discord.py ```sh pip3 install python-a2s ``` - +- jaraco.docker +```sh +pip3 install jaraco.docker +``` ### Installation 1. Get a discord Bot free Token at [Discord Developer Portal](https://discord.com/developers/applications) @@ -84,7 +87,7 @@ gh repo clone immervoll/maphistory-bot 3. Install all the required packages ```sh -pip3 install python-a2s, discord.py +pip3 install -r requirements.txt ``` 4. Enter your Token in `configiguration.json` diff --git a/dockerfile b/dockerfile new file mode 100644 index 0000000..c1b6fbe --- /dev/null +++ b/dockerfile @@ -0,0 +1,15 @@ +FROM python:3.9.7 + +LABEL Maintainer="Immervoll - github.com/immervoll" + +ENV TOKEN="TOKENHERE" +ENV PREFIX="!" +ENV IP="IPHERE" +ENV PORT=27165 + +WORKDIR /usr/src/maphistory-bot/ + +COPY ./ ./ +RUN pip install -r ./requirements.txt + +CMD [ "python", "./main.py" ] diff --git a/main.py b/main.py index 2dcca8e..2ce0862 100644 --- a/main.py +++ b/main.py @@ -1,20 +1,27 @@ +from jaraco.docker import is_docker import json from discord.ext import commands, tasks import discord -import time import a2s import logging +import os logging.basicConfig(level=logging.INFO) +if is_docker(): + TOKEN = os.environ['TOKEN'] + PREFIX = os.environ['PREFIX'] + SERVERADDRESS = (os.environ['IP'], os.environ["PORT"]) -# Get configuration.json -with open("configuration.json", "r") as config: - DATA = json.load(config) - TOKEN = DATA["TOKEN"] - PREFIX = DATA["PREFIX"] - SERVERADDRESS = (DATA["SERVER"]["IP"], DATA["SERVER"]["PORT"]) - INTERVAL = 600.0 # Interval in seconds to check the server - FIELPATH = "./history.txt" # Path to the file to save the history +else: + with open("configuration.json", "r") as config: + DATA = json.load(config) + TOKEN = DATA["TOKEN"] + PREFIX = DATA["PREFIX"] + SERVERADDRESS = (DATA["SERVER"]["IP"], DATA["SERVER"]["PORT"]) + + +INTERVAL = 600.0 # Interval in seconds to check the server +FIELPATH = "./history.txt" # Path to the file to save the history bot = commands.Bot(PREFIX) last_map: str = "" @@ -47,6 +54,7 @@ async def queryServer(): last_map = current_map else: logging.log(logging.INFO, f"Current map: {current_map}") + try: await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name=f"Map History | {current_map}")) except: @@ -58,10 +66,13 @@ async def queryServer(): def get_last_10_maps(): with open(FIELPATH, "r") as file: return file.readlines()[-10:] + + def get_current_map(): with open(FIELPATH, "r") as file: return file.readlines()[-1] + @bot.command(name="history", aliases=["maps"]) async def history(ctx: commands.Context): logging.log(logging.INFO, f"{ctx.author} requested the map history") @@ -69,7 +80,8 @@ async def history(ctx: commands.Context): embed = discord.Embed(title="Map History", description="", color=0xff0000) embed.set_author(name="Map History Bot", url="https://github.com/immervoll/maphistory-bot") - embed.add_field(name="Current Map", value="".join(get_current_map()), inline=False) + embed.add_field(name="Current Map", value="".join( + get_current_map()), inline=False) embed.add_field(name="Last 10 Maps", value=f"{last_10_maps}", inline=False) embed.set_footer(text="by immervoll") await ctx.send(f"{ctx.author.mention} here is the servers map history", embed=embed) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..d421666 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +discord.py==1.7.3 +python-a2s==1.3.0 +jaraco.docker==2.0 \ No newline at end of file