Intial commit

This commit is contained in:
Jimmy 2021-08-23 13:33:17 +12:00
parent c7578e2bbd
commit a9320cb0f1
6 changed files with 76 additions and 19 deletions

10
Dockerfile Normal file
View File

@ -0,0 +1,10 @@
FROM python:slim
COPY requirements.txt /requirements.txt
RUN pip install -r /requirements.txt && rm /requirements.txt
COPY main.py /main.py
CMD [ "python", "/main.py" ]

View File

@ -8,10 +8,10 @@ watchgod = "*"
[packages] [packages]
discord = "*" discord = "*"
mcrcon = "*"
[requires] [requires]
python_version = "3.8" python_version = "3.8"
[scripts] [scripts]
dev = "watchgod main.main" dev = "watchgod main.main"

9
Pipfile.lock generated
View File

@ -1,7 +1,7 @@
{ {
"_meta": { "_meta": {
"hash": { "hash": {
"sha256": "956115984ba9564a0be5ce187a54020a2b250a6c607d79ee8f4e15ac86e15ccd" "sha256": "515209fb41ecd2e0e6fc16cdfc149773ae6d8e2880415328840d6c4ef946b408"
}, },
"pipfile-spec": 6, "pipfile-spec": 6,
"requires": { "requires": {
@ -107,6 +107,13 @@
"markers": "python_version >= '3.5'", "markers": "python_version >= '3.5'",
"version": "==3.2" "version": "==3.2"
}, },
"mcrcon": {
"hashes": [
"sha256:c2f1caf2467e283e0ccd2436f1ce21f04242b03c5fafb3860e3924d764a910d4"
],
"index": "pypi",
"version": "==0.7.0"
},
"multidict": { "multidict": {
"hashes": [ "hashes": [
"sha256:018132dbd8688c7a69ad89c4a3f39ea2f9f33302ebe567a879da8f4ca73f0d0a", "sha256:018132dbd8688c7a69ad89c4a3f39ea2f9f33302ebe567a879da8f4ca73f0d0a",

8
docker-compose.yml Normal file
View File

@ -0,0 +1,8 @@
version: '3'
services:
whitelistbot:
image: jimmy1248/whitelistbot:latest
env_file:
- .env
tty: true

54
main.py
View File

@ -1,23 +1,43 @@
# bot.py
import os
import discord import discord
from discord.ext import commands
from os import getenv
from mcrcon import MCRcon
TOKEN = os.getenv('DISCORD_TOKEN') bot = commands.Bot(command_prefix='!')
GUILD = os.getenv('DISCORD_GUILD')
client = discord.Client() @bot.command()
async def whitelist(ctx, arg):
try:
resp = command(f'whitelist add {arg}')
if resp == f'Added {arg} to the whitelist':
msg = "You have been whitelisted"
elif resp == "Player is already whitelisted":
msg = f'{arg} is alredy whitelisted'
else:
msg = f'Failed to add {arg}'
except ConnectionRefusedError:
msg = "Failed to add {arg}"
except Exception as e:
print("Discord error")
finally:
await ctx.send(msg)
@client.event def command(command):
async def on_ready(): with MCRcon(getenv("RCON_HOST"), getenv("RCON_PASS"), int(getenv("RCON_PORT")) ) as mcr:
for guild in client.guilds: resp = mcr.command(command)
if guild.name == GUILD: print(resp)
break return resp
print(
f'{client.user} is connected to the following guild:\n'
f'{guild.name}(id: {guild.id})'
)
def main(): def main():
client.run(TOKEN) try:
command("say hello")
bot.run(getenv("DISCORD_TOKEN"))
except ConnectionRefusedError:
exit("RCON failed")
except:
exit("Discord error")
if __name__ == "__main__":
main()

12
requirements.txt Normal file
View File

@ -0,0 +1,12 @@
-i https://pypi.org/simple
aiohttp==3.7.4.post0; python_version >= '3.6'
async-timeout==3.0.1; python_full_version >= '3.5.3'
attrs==21.2.0; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'
chardet==4.0.0; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'
discord.py==1.7.3; python_full_version >= '3.5.3'
discord==1.7.3
idna==3.2; python_version >= '3.5'
mcrcon==0.7.0
multidict==5.1.0; python_version >= '3.6'
typing-extensions==3.10.0.0
yarl==1.6.3; python_version >= '3.6'