diff --git a/client.py b/client.py index adab9a6..faf20ee 100644 --- a/client.py +++ b/client.py @@ -17,6 +17,7 @@ class client(): with open(config_path, "r") as conf_fd: self.config = json.loads(conf_fd.read()) self.plugman = plugin_manager(self.logger, plugin_path = self.config["plugin_path"]) + self.api = disc_api.rest("https://discordapp.com/api", {"Authorization": "Bot {0}".format(self.config["token"]), "User-Agent": "mbdf (cynic.moe, v1)", "Content-Type": "application/json"}) async def shit(self): # https://discord.com/developers/docs/topics/gateway#connecting-to-the-gateway # you're supposed to ask an HTTP endpoint what the ws gateway is but the docs didnt tell me the domain after 1sec so HAHA NOPE @@ -63,20 +64,7 @@ class client(): except websockets.exceptions.ConnectionClosedOK: pass - def get_channel(self, id): - endpoint = f"https://discordapp.com/api/channels/{id}" - headers = {"Authorization": "Bot {0}".format(self.config["token"]), "User-Agent": "mbdf (cynic.moe, v1)", "Content-Type": "application/json"} - return requests.get(endpoint, headers = headers).text - def execute_webhook(self, link, message, name = "webhook"): - #it doesn't work without the headers for some reason - headers = {"Content-Type": "application/json"} - requests.post(link, headers = headers, data = json.dumps({"content": message, "username": name})) - def send_msg(self, id, message): - endpoint = f"https://discordapp.com/api/channels/{id}/messages" - # fstring wasn't working for the auth - headers = {"Authorization": "Bot {0}".format(self.config["token"]), "User-Agent": "mbdf (cynic.moe, v1)", "Content-Type": "application/json"} - res = requests.post(endpoint, headers = headers, data = json.dumps({"content": message})) def __enter__(self): return self def __exit__(self, type, value, traceback): - pass # this should destruct all members probably maybe \ No newline at end of file + pass # this should destruct all members probably maybe diff --git a/default_plugins/authy.py b/default_plugins/authy.py index 0491e75..0202db1 100644 --- a/default_plugins/authy.py +++ b/default_plugins/authy.py @@ -1,12 +1,12 @@ hooks = ["MESSAGE_CREATE"] def run(event, ctx, bot): if ctx["content"].startswith(":") and not(int(ctx["author"]["id"]) in bot.config["authed"]): - bot.send_msg(ctx["channel_id"], "you lack the proper authentication") + bot.api.send_msg(ctx["channel_id"], "you lack the proper authentication") return if ctx["content"] == ":ops": - bot.send_msg(ctx["channel_id"], ", ".join([str(ident) for ident in bot.config["authed"]])) + bot.api.send_msg(ctx["channel_id"], ", ".join([str(ident) for ident in bot.config["authed"]])) elif ctx["content"].startswith(":op"): for mention in ctx["mentions"]: bot.config["authed"].append(int(mention["id"])) - bot.send_msg(ctx["channel_id"], f"added {mention['username']} to the session authed!") + bot.api.send_msg(ctx["channel_id"], f"added {mention['username']} to the session authed!") bot.logger.write(f"added auth {mention['id']} {mention['username']}") \ No newline at end of file diff --git a/default_plugins/hotload.py b/default_plugins/hotload.py index f27eeb5..ba78cb7 100644 --- a/default_plugins/hotload.py +++ b/default_plugins/hotload.py @@ -2,19 +2,19 @@ from importlib import reload hooks = ["MESSAGE_CREATE"] def run(event, ctx, bot): if ctx["content"].startswith(":") and not(int(ctx["author"]["id"]) in bot.config["authed"]): - bot.send_msg(ctx["channel_id"], "you lack the proper authentication") + bot.api.send_msg(ctx["channel_id"], "you lack the proper authentication") return plugin_manager = bot.plugman if ctx["content"].startswith(":reload"): cmd = ctx["content"].split(" ")[1] if not(cmd in [m.__name__ for m in plugin_manager.plugins_loaded]): - bot.send_msg(ctx["channel_id"], f"plugin {cmd} could not be found for reloading!") + bot.api.send_msg(ctx["channel_id"], f"plugin {cmd} could not be found for reloading!") else: for m in plugin_manager.plugins_loaded: if m.__name__ == cmd: reload(m) - bot.send_msg(ctx["channel_id"], f"reloaded plugin {cmd}!") + bot.api.send_msg(ctx["channel_id"], f"reloaded plugin {cmd}!") bot.logger.write(f"reloaded {cmd}") elif ctx["content"] ==":plugins": plugins = ", ".join([m.__name__ for m in plugin_manager.plugins_loaded]) - bot.send_msg(ctx["channel_id"], f"{plugins}") \ No newline at end of file + bot.api.send_msg(ctx["channel_id"], f"{plugins}") \ No newline at end of file diff --git a/disc_api.py b/disc_api.py index 1e171cf..448bdd7 100644 --- a/disc_api.py +++ b/disc_api.py @@ -1,3 +1,5 @@ +import requests, json + gateway_opcodes = { # https://discord.com/developers/docs/topics/opcodes-and-status-codes#gateway-gateway-opcodes 0: "dispatch", 1: "heartbeat", @@ -26,4 +28,24 @@ gateway_close_event_codes = { # https://discord.com/developers/docs/topics/opcod 4012: "You sent an invalid version for the gateway.", 4013: "You sent an invalid intent for a Gateway Intent. You may have incorrectly calculated the bitwise value.", 4014: "You sent a disallowed intent for a Gateway Intent. You may have tried to specify an intent that you have not enabled or are not approved for." -} \ No newline at end of file +} + +class rest(): + def __init__(self, gatewa, header): + self.gateway = gatewa + self.headers = header + def get(self, endpoint): + return requests.get(self.gateway+endpoint, headers = self.headers).text + def post(self, endpoint, dat): + requests.post(self.gateway+endpoint, headers = self.headers, data = dat) + + + def get_channel(self, id): + return self.get(f"/channels/{id}") + def get_guild_channels(self, id): + return self.get(f"/guilds/{id}/channels") + + def execute_webhook(self, link, message, name): + self.post(link, json.dumps({"content": message, "username": name})) + def send_msg(self, id, message): + self.post(f"/channels/{id}/messages", json.dumps({"content": message})) \ No newline at end of file diff --git a/plugins/dango.py b/plugins/dango.py index 39c046e..039427d 100644 --- a/plugins/dango.py +++ b/plugins/dango.py @@ -14,4 +14,4 @@ this particular example listens for a message that contains the string 'dango' a hooks = ["MESSAGE_CREATE"] # run() will be called when client.dispatch() gets a MESSAGE_CREATE def run(event, ctx, bot): if ctx["content"] == "dango": # if the message body matches... - bot.send_msg(ctx["channel_id"], "to all the motherfuckers that shed a tear") \ No newline at end of file + bot.api.send_msg(ctx["channel_id"], "to all the motherfuckers that shed a tear") \ No newline at end of file