Added mcstat

master
jef 2020-08-08 13:12:09 +02:00
parent 18471abe13
commit 27c082ece7
9 changed files with 47 additions and 11 deletions

1
.gitignore vendored
View File

@ -23,3 +23,4 @@ __pycache__/
# Ctags file # Ctags file
tags tags
frank.yaml

View File

@ -1,2 +0,0 @@
test:
channel_id: 740301700606197918

View File

@ -14,18 +14,18 @@ class Frank(discord.Client):
try: try:
with open(config_file, "r") as f: with open(config_file, "r") as f:
self._settings = yaml.load(f, Loader=yaml.FullLoader) self._config = yaml.load(f, Loader=yaml.FullLoader)
except FileNotFoundError: except FileNotFoundError:
self._settings = None self._config = None
async def on_ready(self): async def on_ready(self):
print("Connected") print("Connected")
# Startup all modules # Startup all modules
for module in self._modules: for module in self._modules:
if self._settings and module.NAME in self._settings: if self._config and module.NAME in self._config:
loaded = module(self, settings=self._settings[module.NAME]) loaded = module(self, config=self._config[module.NAME])
else: else:
loaded = module(self) loaded = module(self)

View File

@ -5,9 +5,9 @@ class Module:
PREFIX = None PREFIX = None
NAME = None NAME = None
def __init__(self, client: "Frank", settings: Dict = None): def __init__(self, client: "Frank", config: Dict = None):
self._client = client self._client = client
self._settings = settings self._config = config
async def start(self): async def start(self):
pass pass

View File

@ -1 +1,2 @@
from .testmod import TestMod from .testmod import TestMod
from .mcstat import McStat

View File

@ -0,0 +1,34 @@
from .. import Module
from mcstatus import MinecraftServer
class McStat(Module):
PREFIX = "mc"
NAME = "mcstat"
async def command(self, cmd):
if cmd[0] == "online":
address = self._config["domain"]
port = self._config.get("port")
if port:
address += ":" + str(port)
server = MinecraftServer.lookup(address)
status = server.status()
if status.players.sample is not None:
players = [player.name for player in status.players.sample]
else:
players = None
channel = self._client.get_channel(self._config["channel_id"])
if players:
await channel.send(f'Currently online: {",".join(players)}')
else:
await channel.send("No one is here bro")

View File

@ -3,10 +3,11 @@ from .. import Module
class TestMod(Module): class TestMod(Module):
PREFIX = "test" PREFIX = "test"
NAME = "test" NAME = "test"
async def command(self, cmd: List[str]): async def command(self, cmd: List[str]):
if cmd[0] == "test": if cmd[0] == "test":
channel = self._client.get_channel(self._settings["channel_id"]) channel = self._client.get_channel(self._config["channel_id"])
await channel.send("psycho frank is in the house") await channel.send("psycho frank is in the house")

View File

@ -1,10 +1,10 @@
import os import os
from dotenv import load_dotenv from dotenv import load_dotenv
from frank.modules import TestMod from frank.modules import TestMod, McStat
from frank import Frank from frank import Frank
if __name__ == "__main__": if __name__ == "__main__":
load_dotenv() load_dotenv()
client = Frank([TestMod]) client = Frank([TestMod, McStat])
client.run(os.getenv('DISCORD_TOKEN')) client.run(os.getenv('DISCORD_TOKEN'))

View File

@ -3,3 +3,4 @@ pylint
jedi jedi
python-dotenv python-dotenv
pyyaml pyyaml
mcstatus