Skip to content

Commit

Permalink
Merge pull request #9 from immervoll/immervoll/issue8
Browse files Browse the repository at this point in the history
  • Loading branch information
immervoll authored Jun 19, 2022
2 parents 3ef6d6e + b771abc commit a0b7bdf
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 13 deletions.
6 changes: 6 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
./.github
./.gitignore
./configuration.json
./readme.md
./history.txt
./docker-compose.yml
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -351,4 +351,6 @@ MigrationBackup/
configuration.json
./configuration.json
history.txt
./history.txt
./history.txt
./docker-compose.yml
docker-compose.yml
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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`
Expand Down
15 changes: 15 additions & 0 deletions dockerfile
Original file line number Diff line number Diff line change
@@ -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" ]
32 changes: 22 additions & 10 deletions main.py
Original file line number Diff line number Diff line change
@@ -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 = ""
Expand Down Expand Up @@ -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:
Expand All @@ -58,18 +66,22 @@ 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")
last_10_maps = "".join(get_last_10_maps())
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)
Expand Down
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
discord.py==1.7.3
python-a2s==1.3.0
jaraco.docker==2.0

0 comments on commit a0b7bdf

Please sign in to comment.