From dbf34c639d2c7011f51cba62d4ccccf11eb3e2a1 Mon Sep 17 00:00:00 2001 From: Specoolazius Date: Wed, 30 Mar 2022 16:08:06 +0200 Subject: [PATCH] workz --- bot/config/discord_example.ini | 0 bot/extensions/developer.py | 13 ++++++++++ bot/extensions/executer.py | 37 +++++++++++++++++++++++++++++ bot/libs/__init__.py | 1 + bot/libs/bot.py | 43 ++++++++++++++++++++++++++++++++++ bot/libs/logger.py | 0 bot/run.py | 16 +++++++++++++ bot/scripts/start-server.sh | 3 +++ tests/minecraft.py | 18 ++++++++++++++ 9 files changed, 131 insertions(+) create mode 100644 bot/config/discord_example.ini create mode 100644 bot/extensions/developer.py create mode 100644 bot/extensions/executer.py create mode 100644 bot/libs/__init__.py create mode 100644 bot/libs/bot.py create mode 100644 bot/libs/logger.py create mode 100644 bot/run.py create mode 100644 bot/scripts/start-server.sh create mode 100644 tests/minecraft.py diff --git a/bot/config/discord_example.ini b/bot/config/discord_example.ini new file mode 100644 index 0000000..e69de29 diff --git a/bot/extensions/developer.py b/bot/extensions/developer.py new file mode 100644 index 0000000..340fd0c --- /dev/null +++ b/bot/extensions/developer.py @@ -0,0 +1,13 @@ +import discord + +from libs import ServerBot + + +class Developer(discord.Cog): + + def __init__(self, bot: ServerBot): + self.bot = bot + + +def setup(bot: ServerBot): + bot.add_cog(Developer(bot)) \ No newline at end of file diff --git a/bot/extensions/executer.py b/bot/extensions/executer.py new file mode 100644 index 0000000..7293409 --- /dev/null +++ b/bot/extensions/executer.py @@ -0,0 +1,37 @@ +import asyncio +import os.path + +import discord +from discord.commands import slash_command + +from libs import ServerBot + + +class Executer(discord.Cog): + + def __init__(self, bot: ServerBot): + self.bot = bot + + @slash_command(name='start') + async def __start_server(self, ctx: discord.ApplicationContext) -> None: + await ctx.defer() + + await asyncio.create_subprocess_exec( + os.path.join('scripts', 'start-server.sh'), + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE, + ) + + await ctx.respond('starting server') + + @slash_command(name='stop') + async def __stop_server(self, ctx: discord.ApplicationContext) -> None: + if ctx.user.id not in self.bot.admin_ids: + await ctx.respond('You don\'t have permissions to do that', ephemeral=True) + return + + await ctx.respond('Stopping server') + + +def setup(bot: ServerBot): + bot.add_cog(Executer(bot)) diff --git a/bot/libs/__init__.py b/bot/libs/__init__.py new file mode 100644 index 0000000..567f68b --- /dev/null +++ b/bot/libs/__init__.py @@ -0,0 +1 @@ +from .bot import ServerBot diff --git a/bot/libs/bot.py b/bot/libs/bot.py new file mode 100644 index 0000000..628b52c --- /dev/null +++ b/bot/libs/bot.py @@ -0,0 +1,43 @@ +import os.path +from abc import ABC +from typing import Any + +from mcstatus import JavaServer + +import discord +from discord.commands import slash_command + +TOKEN = "OTU4Njg2Mjg3MDIyNDIwMDE4.YkQ8Lg.cjdTR7syrHPsXVBUINhq3COQQc4" + + +class ServerBot(discord.Bot, ABC): + + def __init__(self): + super(ServerBot, self).__init__( + # general config + owner_id=448814943857410058, + + # starting presence + activity=discord.Game('Beep Boop! Loading...'), + # activity=discord.Game('with 10 people on sfs.ddnss.org'), + status=discord.Status.idle, + + # debug + debug_guilds=[848137923101982741, 958692739065720832], + ) + + # loading extensions + from extensions.executer import Executer + self.add_cog(Executer(self)) + from extensions.developer import Developer + self.add_cog(Developer(self)) + + self.admin_ids = [448814943857410058] + + def run(self, *args: Any, **kwargs: Any) -> None: + super(ServerBot, self).run(TOKEN, *args, **kwargs) + + async def on_ready(self) -> None: + print('online') + + diff --git a/bot/libs/logger.py b/bot/libs/logger.py new file mode 100644 index 0000000..e69de29 diff --git a/bot/run.py b/bot/run.py new file mode 100644 index 0000000..3913eb5 --- /dev/null +++ b/bot/run.py @@ -0,0 +1,16 @@ +import sys +from aiohttp import ClientConnectorError + +from libs.bot import ServerBot + + +if __name__ == '__main__': + bot = ServerBot() + + try: + bot.run() + + except ClientConnectorError as e: + sys.exit(-59) + + sys.exit(0) diff --git a/bot/scripts/start-server.sh b/bot/scripts/start-server.sh new file mode 100644 index 0000000..3a9bdb3 --- /dev/null +++ b/bot/scripts/start-server.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +wakeonlan 90:1B:0E:A5:45:82 diff --git a/tests/minecraft.py b/tests/minecraft.py new file mode 100644 index 0000000..2073f8d --- /dev/null +++ b/tests/minecraft.py @@ -0,0 +1,18 @@ +from mcstatus import JavaServer + +server = JavaServer.lookup('sfs.ddnss.org:25565', 10) +server = JavaServer('sfs.ddnss.org', 25565) + +print('got server') + +status = server.status() +print(f"The server has {status.players.online} players and replied in {status.latency} ms") + + +print(server.status()) +print(vars(server.status())) + +print(server.query()) + +query = server.query() +print(f"The server has the following players online: {', '.join(query.players.names)}") \ No newline at end of file