From 6d8c1c066a7764c95ccaabc2ea5b2298499732d3 Mon Sep 17 00:00:00 2001 From: supopur Date: Sun, 5 Mar 2023 11:28:00 +0100 Subject: [PATCH] Made web ui into a cog it doesnt work yet but it doesnt throw errors. All pages are 404. Tho the web responds to get calls wich is good --- cogs/example.py | 14 +++++++++ cogs/web/__pycache__/web.cpython-39.pyc | Bin 0 -> 1759 bytes cogs/web/config.toml | 13 +++++++++ cogs/web/web.py | 37 ++++++++++++++++++++++++ config.toml | 2 +- main.py | 33 ++------------------- 6 files changed, 68 insertions(+), 31 deletions(-) create mode 100644 cogs/example.py create mode 100644 cogs/web/__pycache__/web.cpython-39.pyc create mode 100644 cogs/web/config.toml create mode 100644 cogs/web/web.py diff --git a/cogs/example.py b/cogs/example.py new file mode 100644 index 0000000..422e3af --- /dev/null +++ b/cogs/example.py @@ -0,0 +1,14 @@ +import discord +from discord.ext import commands + +class Greetings(commands.Cog): + def __init__(self, bot): + self.bot = bot + + @commands.slash_command(guild_ids=["823188238260633600"]) + async def hello(self, ctx): + await ctx.send(f'Hello {ctx.author}') + +def setup(bot): + bot.add_cog(Greetings(bot)) + diff --git a/cogs/web/__pycache__/web.cpython-39.pyc b/cogs/web/__pycache__/web.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..48d32dc0bb453d06376a25b7a92b0fcde1a61574 GIT binary patch literal 1759 zcmb7EOOM+&5GEIZa<**0>1V!45fp!Z7)kV;XT(7p0s8V#B z7~YfjTI4TykyCz8Z#=fA{)L=6q_m0`O&yd1M?=no-wcP7j7C0!@j9{TBO9T=^kRF! z9(n?^{s9O@6z3>v{y4(0COIjr$imH@9oa_5i5&CqMZIT;TGYNk)MnP1yS5^aI<$9z zqCUk(C~*G2gB zNVWRa@PN_tDa`s$AS=8=F_L&gq;;uzTRPg+n!Nh#oXFnBU6D(CZas&9Z1~m?TDkua z)rVgrY`&j>N^jGrV2{DynZFsV(3!m%t{~6@M4bcFa=im|3En^p_vDgi~J2%dxF7I#F7ch;{lI6Ybvcove%1p*_c$o7v$?M0V(O!e3K6NeaFL4F5b=;+nxn(`-GK!aUCuUReqDNX9rW1a~5f5DDB%MSqUcZ#>S zKz_LkWM{GTrd7U}XXX3f=Z;(sTe}Z{b6d4LThQ+Bg4XH>V`Uw4shnSQX)DK6xbot- zObQmq%1SDzYhJNZ+2%diIZvo^b3TV?=E}~>6Ru#BS89+72K5=sr2biXVo^4nBltqH zz*Bb3@)My;QQ;HSYi>~N!CLHVazm4eCS83uB?54oI_-n7{u~IxJv<~s{DAoQE3%J$ z>)l*V{vG!$u*JtXCHmP{512C<~!yX>O JgpW*a{{t|ZmID9) literal 0 HcmV?d00001 diff --git a/cogs/web/config.toml b/cogs/web/config.toml new file mode 100644 index 0000000..492958e --- /dev/null +++ b/cogs/web/config.toml @@ -0,0 +1,13 @@ +#The webserver plugin cofiguration file. +[webserver] +#This will specify the ip adress of the machine that the server is running on. I would recomend to change it as some pages might 404 if you dont. +ip=0.0.0.0 + +#The port to listen on. +port=5000 + +#Section related to security and .key, .crt file locations. Its really recomended to use ssl if you are exposing the web interface to the internet. +[SSL] +userssl=False +crt="certs/certificate.crt" +key="certs/private.key" diff --git a/cogs/web/web.py b/cogs/web/web.py new file mode 100644 index 0000000..e9ebc00 --- /dev/null +++ b/cogs/web/web.py @@ -0,0 +1,37 @@ +import sys +import discord +from discord.ext import commands +import quart +import toml +import logging + +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 03a179a..b1358e4 100644 --- a/config.toml +++ b/config.toml @@ -14,4 +14,4 @@ key = "/path/to/private.key" [bot] -cogs=["cogs.example", "cogs.web"] +cogs=["cogs.example", "cogs.web.web"] diff --git a/main.py b/main.py index 187610a..a8536d2 100644 --- a/main.py +++ b/main.py @@ -19,8 +19,9 @@ else: logging.basicConfig(filename='latest.log', encoding='utf-8', level=logging.INFO) logging.getLogger().addHandler(logging.StreamHandler()) - - +try: + os.remove("latest.log") +except: pass def log(level, log): if level == "inf": logging.info(log) @@ -80,34 +81,6 @@ async def stop(ctx): sys.exit("Terminated.") else: await ctx.respond("https://media.tenor.com/Iv6oKRuAhVEAAAAC/hal9000-im-sorry-dave.gif") -log("inf", "Loading Quart...") -#webserver -app = quart.Quart(__name__) -log("inf", "Quart loaded.") -#The index. -@app.route('/', methods=['GET']) -async def index(): - return await render_template('index.html') - -@app.route('/configuration', methods=['GET']) -async def configuration(): - return await render_template('configuration.html') - -@app.route('/dashboard', methods=['GET']) -async def dashboard(): - return await render_template('dashboard.html') - -@app.route('/plugins', methods=['GET']) -async def plugins(): - return await render_template('plugins.html') - -@app.route('/stop', methods=['GET']) -async def stop(): - sys.exit("Web terminated.") - - - -bot.loop.create_task(app.run_task('0.0.0.0', 5000)) bot.run(token)