From a9320cb0f1252233af2f39dc36e499724b75b1ec Mon Sep 17 00:00:00 2001
From: Jimmy <git@jimmy.nz>
Date: Mon, 23 Aug 2021 13:33:17 +1200
Subject: [PATCH] Intial commit

---
 Dockerfile         | 10 +++++++++
 Pipfile            |  2 +-
 Pipfile.lock       |  9 +++++++-
 docker-compose.yml |  8 +++++++
 main.py            | 54 +++++++++++++++++++++++++++++++---------------
 requirements.txt   | 12 +++++++++++
 6 files changed, 76 insertions(+), 19 deletions(-)
 create mode 100644 Dockerfile
 create mode 100644 docker-compose.yml
 create mode 100644 requirements.txt

diff --git a/Dockerfile b/Dockerfile
new file mode 100644
index 0000000..0da529f
--- /dev/null
+++ b/Dockerfile
@@ -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" ]
+
diff --git a/Pipfile b/Pipfile
index 3474175..b122167 100644
--- a/Pipfile
+++ b/Pipfile
@@ -8,10 +8,10 @@ watchgod = "*"
 
 [packages]
 discord = "*"
+mcrcon = "*"
 
 [requires]
 python_version = "3.8"
 
-
 [scripts]
 dev = "watchgod main.main"
diff --git a/Pipfile.lock b/Pipfile.lock
index b90577e..7b48e98 100644
--- a/Pipfile.lock
+++ b/Pipfile.lock
@@ -1,7 +1,7 @@
 {
     "_meta": {
         "hash": {
-            "sha256": "956115984ba9564a0be5ce187a54020a2b250a6c607d79ee8f4e15ac86e15ccd"
+            "sha256": "515209fb41ecd2e0e6fc16cdfc149773ae6d8e2880415328840d6c4ef946b408"
         },
         "pipfile-spec": 6,
         "requires": {
@@ -107,6 +107,13 @@
             "markers": "python_version >= '3.5'",
             "version": "==3.2"
         },
+        "mcrcon": {
+            "hashes": [
+                "sha256:c2f1caf2467e283e0ccd2436f1ce21f04242b03c5fafb3860e3924d764a910d4"
+            ],
+            "index": "pypi",
+            "version": "==0.7.0"
+        },
         "multidict": {
             "hashes": [
                 "sha256:018132dbd8688c7a69ad89c4a3f39ea2f9f33302ebe567a879da8f4ca73f0d0a",
diff --git a/docker-compose.yml b/docker-compose.yml
new file mode 100644
index 0000000..076d984
--- /dev/null
+++ b/docker-compose.yml
@@ -0,0 +1,8 @@
+version: '3'
+
+services: 
+  whitelistbot:
+    image: jimmy1248/whitelistbot:latest
+    env_file: 
+      - .env
+    tty: true
\ No newline at end of file
diff --git a/main.py b/main.py
index f8782c7..974ec28 100644
--- a/main.py
+++ b/main.py
@@ -1,23 +1,43 @@
-# bot.py
-import os
-
 import discord
+from discord.ext import commands
+from os import getenv
+from mcrcon import MCRcon
 
-TOKEN = os.getenv('DISCORD_TOKEN')
-GUILD = os.getenv('DISCORD_GUILD')
+bot = commands.Bot(command_prefix='!')
 
-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
-async def on_ready():
-    for guild in client.guilds:
-        if guild.name == GUILD:
-            break
-
-    print(
-        f'{client.user} is connected to the following guild:\n'
-        f'{guild.name}(id: {guild.id})'
-    )
+def command(command):
+    with MCRcon(getenv("RCON_HOST"), getenv("RCON_PASS"), int(getenv("RCON_PORT")) ) as mcr:
+        resp = mcr.command(command)
+        print(resp)
+        return resp
 
 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()
\ No newline at end of file
diff --git a/requirements.txt b/requirements.txt
new file mode 100644
index 0000000..ab703b1
--- /dev/null
+++ b/requirements.txt
@@ -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'