From ab0e53acc914de581b004fdec0853174eec4a1a6 Mon Sep 17 00:00:00 2001 From: Ruthenic Date: Wed, 29 Dec 2021 00:40:57 -0500 Subject: [PATCH] [Meta] Initial commit --- .gitignore | 1 + __pycache__/northstar.cpython-310.pyc | Bin 0 -> 1096 bytes bot.py | 105 ++++++++++++++++++++++++++ northstar.py | 19 +++++ 4 files changed, 125 insertions(+) create mode 100644 .gitignore create mode 100644 __pycache__/northstar.cpython-310.pyc create mode 100644 bot.py create mode 100644 northstar.py 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 0000000000000000000000000000000000000000..eb8ac8cd1eeb46456403af8e9429e4f983b734dd GIT binary patch literal 1096 zcmZWo&2H2%5FRH^HcfX6tq_GGaVD1rE*ua-h$3;TkXBqGp(w=O?zU;NwVk4_QVEGZ z$d+55g*WgCi4(8T6EoTFPur3FIkrE0X1< zDV{n_D`ke-Wa|3a(3r7)6UU3KT{DRDVkAqGW9$o~Z;oRRhklOGum(~v zZ0a-^?KWJpse1{y#|8A^%GsX;v6kvoDjj(?{FdcMvJ_ubVFM*kYN?I3?twIxAL^>K zojKRBT+3pRxV0*-K&_*z>(}wHI+n4j%}|!3Jl?Css62>2pP6A*E<^uNndM@4d}f0* z9hD=KrWCm*@YxeK*;*z$C*%wd=SJU)T&YF|rkL%ab3ac-yvS2NPO3xT5d;Kl2ve}tH&ZUvOm*@*qQ|1VUz7;Y_~Y~0;C|@VvuVLjznks5o_J54cxrzG(@#; zJd&!dN1sg|Ej=y?mY3PF%qhFMk)MSsFK}@H=e%(~!q?uphuz>c?o&7gwb}3~G~Oj+ zaKanj2lWui5wm_a9%Bncek+OX9Lclql@e{h)*FmuArgO3)uqshKWm4(DfG0=R6e}P zf|AsvCuafcvXD7H_$>u*u}K^Hb8yNL3FA zHVGaRka{aX5hyAjy{@U;0fa2zfxF?h`;jy258|xcYoBS`A$U9U{*H>;_GtiH&VT&H B?}PvV literal 0 HcmV?d00001 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