From 2561a3c15712ba8cc77dd9f307334fc37edb9f30 Mon Sep 17 00:00:00 2001 From: supopur Date: Thu, 9 Mar 2023 22:12:50 +0100 Subject: [PATCH] I started to implement a API mainly for cogs so they can comunicate with eachother. Also this will reduce the lines of code in cogs. Also it is handy for saving to the main log and writing to the config as it will reduce the chance of corruption of file. --- cogs/web/__pycache__/web.cpython-39.pyc | Bin 1834 -> 0 bytes cogs/web/config.toml | 13 ---- cogs/web/static/configuration.css | 88 ------------------------ cogs/web/static/dashboard.css | 88 ------------------------ cogs/web/static/index.css | 61 ---------------- cogs/web/static/js/configuration.js | 66 ------------------ cogs/web/static/plugins.css | 88 ------------------------ cogs/web/templates/configuration.html | 47 ------------- cogs/web/templates/dashboard.html | 54 --------------- cogs/web/templates/index.html | 34 --------- cogs/web/templates/plugins.html | 54 --------------- cogs/web/web.py | 39 ----------- config.toml | 2 +- main.py | 61 +++++++++++++--- 14 files changed, 54 insertions(+), 641 deletions(-) delete mode 100644 cogs/web/__pycache__/web.cpython-39.pyc delete mode 100644 cogs/web/config.toml delete mode 100644 cogs/web/static/configuration.css delete mode 100644 cogs/web/static/dashboard.css delete mode 100644 cogs/web/static/index.css delete mode 100644 cogs/web/static/js/configuration.js delete mode 100644 cogs/web/static/plugins.css delete mode 100644 cogs/web/templates/configuration.html delete mode 100644 cogs/web/templates/dashboard.html delete mode 100644 cogs/web/templates/index.html delete mode 100644 cogs/web/templates/plugins.html delete mode 100644 cogs/web/web.py diff --git a/cogs/web/__pycache__/web.cpython-39.pyc b/cogs/web/__pycache__/web.cpython-39.pyc deleted file mode 100644 index 812facbf5e7cc948c3818a112e7205b9bea9e384..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1834 zcmb7FOK;pZ5GM8Br=3kw)J=;f2$FU$+V*Y)0gNE+Wr4y4g6bm3BE74PB&sC0O&04* z*ViC_VSUW+$&JVM(*A{Jxm7#+`p)KQtX<6{M^vl9GsbSnY9I63Syqiz-hf1@WLrUecIT zQL$3cR9o#f3novYi#tFl(wHNQU}C{eHnCwRM|ED%lVi$%qde8>A6o#7?nltYpFq}l zjbbG6){^#x<{jy3R~zf?7iX6Aw*K0>z-RUgNQML72GUaQe~-lBcL{E4fYX7>Q31B;)RM)k|4MLqSkqNE zgBrgI=d#GT4I1wd*NTRG_;yZ;ENH9<=2R z<2Wlb8OPya&e9|oPeP-;8iE@g^pYnsW93Z|kFHo>BEQ64b9;bF4X!93>;N5K2lR^7 zsyiWxn4hqOlbeDaww9j&+G!QP+5!3KI*_f!DwtIHa+Z}hu71C<`vf?@QG2ih?csIM z8vXH5*(XdY_g8(GBiEd16~u9w6f};Nom6m=Sw&0bm>b~aEFsFz*$kqYDJLsWnSw=D zsb0z{JVLQd#LFn)%hGVlXrL?%J1qHTrMd)3L)p12ff)G zF2>lTVrNSet1GBnR*$qRb6+w42Kezn+2XZOKFLJNI8nWZ3F#}zwIbJ7oa^hu?`fg| vMFUgSa8C1za=~3#Q}$5 { - try { - const tokenResponse = await fetch('/api/token'); - const tokenValue = await tokenResponse.text(); - tokenInput.value = tokenValue; - - const nameResponse = await fetch('/api/name'); - const nameValue = await nameResponse.text(); - nameInput.value = nameValue; - - const statusResponse = await fetch('/api/status'); - const statusValue = await statusResponse.text(); - statusSelect.value = statusValue; - } catch (error) { - console.error(error); - } -} - -getConfigValues(); - -form.addEventListener("submit", async (event) => { - event.preventDefault(); - const token = tokenInput.value; - const name = nameInput.value; - const status = statusSelect.value; - - try { - // send POST request to /api/token endpoint with token as the body - if (token !== '') { - await fetch('/api/token', { - method: 'POST', - body: token - }); - } - - - // send POST request to /api/name endpoint with name as the body - if (name !== '') { - await fetch('/api/name', { - method: 'POST', - body: name - }); - } - - - // send POST request to /api/status endpoint with status as the body - if (status !== '') { - await fetch('/api/status', { - method: 'POST', - body: status - }); - } - - } catch (error) { - console.error(error); - } -}); \ No newline at end of file diff --git a/cogs/web/static/plugins.css b/cogs/web/static/plugins.css deleted file mode 100644 index fca533e..0000000 --- a/cogs/web/static/plugins.css +++ /dev/null @@ -1,88 +0,0 @@ -body { - font-family: Arial, sans-serif; - margin: 0; - padding: 0; - background-color: #2c3e50; - color: #fff; -} - -.logo { - width: 25; - height: 25; -} - -header { - background-color: #3e4982; - color: #fff; - padding: 20px; - text-align: center; -} - -header h1 { - margin: 0; -} - -.container { - display: flex; -} - -table { - font-family: Arial, Helvetica, sans-serif; - border-collapse: collapse; - width: 100%; -} - -table td, -#status th { - border: 1px solid #ddd; - padding: 8px; -} - -table tr:nth-child(even) { - background-color: #1c567d; -} - -table tr:hover { - background-color: #2980b9; -} - -table th { - padding-top: 12px; - padding-bottom: 12px; - text-align: left; - background-color: #04AA6D; - color: white; -} - -nav { - background-color: #34495e; - width: 200px; - padding: 20px; - text-align: center; -} - -nav ul { - list-style: none; - margin: 0; - padding: 0; -} - -nav li a { - color: #fff; - text-decoration: none; - padding: 10px 0; - display: block; -} - -nav li a:hover { - background-color: #2980b9; -} - -.selected { - background-color: #2980b9; -} - -main { - padding: 20px; - flex-grow: 1; -} \ No newline at end of file diff --git a/cogs/web/templates/configuration.html b/cogs/web/templates/configuration.html deleted file mode 100644 index c416904..0000000 --- a/cogs/web/templates/configuration.html +++ /dev/null @@ -1,47 +0,0 @@ - - - - - MEE2 Configuration - - - - - - -
-

MEE2 Configuration

-
-
- -
-
- - -
- - -
- - -
- -
-
-
- - - \ No newline at end of file diff --git a/cogs/web/templates/dashboard.html b/cogs/web/templates/dashboard.html deleted file mode 100644 index 8913275..0000000 --- a/cogs/web/templates/dashboard.html +++ /dev/null @@ -1,54 +0,0 @@ - - - - - MEE2 Dashboard - - - - - -
-

MEE2 - Admin Dashboard

-
-
- -
-

This is the admin dashboard. Here you can see important thing like the uptime, hits per minute, cogs status etc..

- - - - - - - - - - - - - - - - - - - - - -
ModuleStatusModule link
Uptime1 Year/api/uptime
Hits Per Minute10/api/hpm
Plugin - FivemOnline/plugins/fivem
-
- -
- - - diff --git a/cogs/web/templates/index.html b/cogs/web/templates/index.html deleted file mode 100644 index 5413691..0000000 --- a/cogs/web/templates/index.html +++ /dev/null @@ -1,34 +0,0 @@ - - - - - MEE2 Index - - - - - - -
-

MEE2

-
-
- -
-

Welcome to the MEE2 index page.

-

This bot is designed to help you with various tasks and make your life easier

-

Here you can find information about the bot's features, plugins, configuration, and support. All this info is public. If you want you can turn it off in the settings page.

-
-
- - - diff --git a/cogs/web/templates/plugins.html b/cogs/web/templates/plugins.html deleted file mode 100644 index 970ce15..0000000 --- a/cogs/web/templates/plugins.html +++ /dev/null @@ -1,54 +0,0 @@ - - - - - MEE2 Plugins - - - - - -
-

MEE2 - Plugins

-
-
- -
-

This is the plugin page here you can disable/enable the individual cogs.

- - - - - - - - - - - - - - - - - - - - - -
PluginStatusPlugin link
Uptime1 Year/api/uptime
Hits Per Minute10/api/hpm
Plugin - FivemOnline/plugins/fivem
-
- -
- - - \ No newline at end of file diff --git a/cogs/web/web.py b/cogs/web/web.py deleted file mode 100644 index 24cba8c..0000000 --- a/cogs/web/web.py +++ /dev/null @@ -1,39 +0,0 @@ -import sys -import discord -from discord.ext import commands -import quart -import toml -import logging -from quart import render_template -from quart.helpers import make_response - -class WebServer(commands.Cog): - def __init__(self, bot): - self.bot = bot - self.app = quart.Quart(__name__) - - with open("config.toml", "r") as f: - config = toml.load(f) - - logging.info("Starting Quart server...") - bot.loop.create_task(self.app.run_task(config["webserver"]["ip"], config["webserver"]["port"])) - - @self.app.route('/', methods=['GET']) - async def index(): - return await render_template('index.html') - - @self.app.route('/configuration', methods=['GET']) - async def configuration(): - return await render_template('configuration.html') - - @self.app.route('/dashboard', methods=['GET']) - async def dashboard(): - return await render_template('dashboard.html') - - @self.app.route('/plugins', methods=['GET']) - async def plugins(): - return await render_template('plugins.html') - -def setup(bot): - bot.add_cog(WebServer(bot)) - diff --git a/config.toml b/config.toml index b1358e4..3ca8931 100644 --- a/config.toml +++ b/config.toml @@ -14,4 +14,4 @@ key = "/path/to/private.key" [bot] -cogs=["cogs.example", "cogs.web.web"] +cogs=["cogs.example", "cogs.web.web", "cogs.sapi.sapi"] diff --git a/main.py b/main.py index 00d9236..2a83043 100644 --- a/main.py +++ b/main.py @@ -2,13 +2,6 @@ import discord, os, sys, toml, time, datetime, logging from discord.ext import commands - - - - - - - with open("config.toml", "r") as f: config = toml.load(f) @@ -53,6 +46,56 @@ bot = commands.Bot(command_prefix='!', intents=intents) logging.debug("Loaded the client.") + +#Api used mainly for plugins so getting uptime, loading, unloading, reloading plugins, getting status of plugins, get plugin info, stopping the bot, getting bot name and tag, getting cpm/commands per minute, get if there is web plugin, if there is web plugin register api, get if plugin supports web and so on.. + +class API: + + #Returns config.tomls content in a array. + def get_config(self): + return self.config + #Dumps all the toml data from self.config and stores it inside toml.config + def save_config(self): + try: + with open("config.toml", w) as f: + self.log("inf", "Saving the configuration file...") + toml.dump(self.config, f) + self.log("inf", "Config was saved to config.toml") + #aok + return 0 + #In case file doesnt exist + except Exception as e: + self.log("err", f"Failed to save to config.toml. {e}") + return 1 + #Connect the main .log file to the api so plugins can log to latest.log w/o risking corrupting the file. + def log(self, status, log): + self.logger(str(status), str(log)) + def load(self, extension): + self.logger("inf", f"Loading {extension}...") + + extension = str(extension) + + if extension.startswith("cogs."): + extension.replace("cogs.", "", 1) + + try: + self.bot.load_extension(f"cogs.{extension}") + except Exception as e: + self.logger("wrn", f"Extension {extension} failed to load due to: {e}") + else: + self.config["bot"]["cogs"].append(f"cogs.{extension}") + self.save_config() + self.logger("inf", f"Extension {extension} loaded.") + + + def __init__(self, config, bot, log): + self.start_time = time.time() + self.config = config + self.bot = bot + self.logger = log + +print(type(config["bot"])) + #Load all the extensions for x in config["bot"]["cogs"]: log("inf", f"Loading {x}...") @@ -81,5 +124,7 @@ async def stop(ctx): else: await ctx.respond("https://media.tenor.com/Iv6oKRuAhVEAAAAC/hal9000-im-sorry-dave.gif") +if __name__ == "__main__": + api = API(config, bot, log) + bot.run(token) -bot.run(token)