commit ab0e53acc914de581b004fdec0853174eec4a1a6 Author: Ruthenic Date: Wed Dec 29 00:40:57 2021 -0500 [Meta] Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d344ba6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +config.json diff --git a/__pycache__/northstar.cpython-310.pyc b/__pycache__/northstar.cpython-310.pyc new file mode 100644 index 0000000..eb8ac8c Binary files /dev/null and b/__pycache__/northstar.cpython-310.pyc differ diff --git a/bot.py b/bot.py new file mode 100644 index 0000000..bfe1681 --- /dev/null +++ b/bot.py @@ -0,0 +1,105 @@ +import hikari, lightbulb, json, logging + +import northstar + +config = json.load(open("./config.json")) +token = config["token"] + +bot = lightbulb.BotApp(token=token,prefix=None,help_class=None) +try: + northstarAPI = northstar.northstar() +except: + logging.warn("FAILED TO INIT NORTHSTAR API: CONTINUING WITH NONE") + northstarAPI = None + +@bot.command +@lightbulb.command("ping", "how dead am i", ephemeral=True,guilds=[925509769958162462]) +@lightbulb.implements(lightbulb.SlashCommand) +async def ping(ctx) -> None: + await ctx.respond(f"Pong! Latency: {bot.heartbeat_latency * 1_000:.0f}ms") + +@bot.command +@lightbulb.command("help", "what the hell do i do",guilds=[925509769958162462]) +@lightbulb.implements(lightbulb.SlashCommand) +async def help(ctx) -> None: + embed = hikari.Embed(title = "Help") + embed.add_field("/ping", "Sends you my latency.") + embed.add_field("/search", "Searches for servers.\nOptions:\n`keyword`- Required, what to search for.\n`field`, Optional, tells you what you want to search for. Defaults to `name`.") + embed.add_field("/info", "Gives some more info on a server.\nOptions:\n `name`- Required, the exact name of the server.") + embed.add_field("/status", "Tells you about the masterserver and some other general stats.") + await ctx.respond(embed) + +@bot.command +@lightbulb.option("keyword", "keyword to search for",required=True) +@lightbulb.option("field", "what exactly to search for",required=False,default="name") +@lightbulb.command("search", "where the hell are those servers",guilds=[925509769958162462]) +@lightbulb.implements(lightbulb.SlashCommand) +async def search(ctx) -> None: + if northstarAPI == None: + await ohshit(ctx) + return + embed = hikari.Embed(title = "Servers") + northstarAPI.updateServers() + servers = northstarAPI.searchServers(ctx.options.keyword.lower(), northstarAPI.getServers(), field=ctx.options.field) + for server in servers: + embed.add_field(server["name"], server["description"]) + await ctx.respond(embed) + +@bot.command +@lightbulb.option("name", "the name of the server you want info for",required=True) +@lightbulb.command("info", "what the fuck is that server",guilds=[925509769958162462]) +@lightbulb.implements(lightbulb.SlashCommand) +async def info(ctx) -> None: + if northstarAPI == None: + await ohshit(ctx) + return + northstarAPI.updateServers() + servers = northstarAPI.getServers() + realServer = None + for server in servers: + if server["name"] == ctx.options.name: + realServer = server + break + if realServer == None: + await ctx.respond("Server not found!", flags=hikari.MessageFlags.EPHEMERAL) + else: + server = realServer + embed = hikari.Embed(title=server["name"], description=server["description"]) + embed.set_footer(server["id"]) + embed.add_field("Map", server["map"]) #TODO: convert between raw map names and normal map names + embed.add_field("Players", f"{server['playerCount']}/{server['maxPlayers']}") + await ctx.respond(embed) + +@bot.command +@lightbulb.command("status", "check out the masterserver info and general stats",guilds=[925509769958162462]) +@lightbulb.implements(lightbulb.SlashCommand) +async def status(ctx) -> None: + if northstarAPI == None: + embed = hikari.Embed(title="status", description="Oh no! The masterserver is/was down. Attempting reconnect...") + await ctx.respond(embed) + await ohshit(ctx) + else: + northstarAPI.updateServers() + + serverCount = 0 + playerCount = 0 + maxPlayers = 0 + for server in northstarAPI.servers: + serverCount += 1 + playerCount += server["playerCount"] + maxPlayers += server["maxPlayers"] + + embed = hikari.Embed(title="Status", description="Masterserver is running properly!") + embed.add_field("Servers online", str(serverCount)) + embed.add_field("Players online", f"{playerCount}/{maxPlayers}") + await ctx.respond(embed) + +async def ohshit(ctx): + try: + northstarAPI.__init__() + logging.info("ABLE TO RE-INIT NORTHSTAR API") + await ctx.respond("We were able to re-init the API! Try your command again.", flags=hikari.MessageFlags.EPHEMERAL) + except: + logging.warn("STILL CANNOT ACCESS NORTHSTAR API") + await ctx.respond("Oh shit, the API is still down. Try again later!", flags=hikari.MessageFlags.EPHEMERAL) +bot.run() diff --git a/northstar.py b/northstar.py new file mode 100644 index 0000000..323942f --- /dev/null +++ b/northstar.py @@ -0,0 +1,19 @@ +import requests + +class northstar(): + def __init__(self, url="https://northstar.tf/client"): + self.backendUrl = url + self.servers = requests.get(url + "/servers").json() + + def updateServers(self): + self.servers = requests.get(self.backendUrl + "/servers").json() + + def getServers(self): + return self.servers + + def searchServers(self, keyword, servers, field="name"): + founds = [] + for server in servers: + if keyword in server[field]: + founds.append(server) + return founds