From 8679de3bc52dccdb8971e547067d257ccd3bdd1f Mon Sep 17 00:00:00 2001 From: jneil Date: Sun, 8 Oct 2023 17:25:36 +0100 Subject: [PATCH 1/5] First Draft of server_widgets.py --- cogs/server_widgets.py | 119 +++++++++++++++++++++++++++++++++++++++++ requirements.txt | 6 +-- 2 files changed, 122 insertions(+), 3 deletions(-) create mode 100644 cogs/server_widgets.py diff --git a/cogs/server_widgets.py b/cogs/server_widgets.py new file mode 100644 index 0000000..67ae582 --- /dev/null +++ b/cogs/server_widgets.py @@ -0,0 +1,119 @@ +import discord +from discord.ext import commands +import os +from pathlib import Path +from typing import Literal, Optional +from discord.ext import commands +from discord.ext.commands import Greedy, Context # or a subclass of yours +from utils import Config +import discord +from discord.ext import commands, tasks +import json +from ast import literal_eval +from datetime import datetime +import sqlite3 + +from rcon.instances import check_perms, get_available_instances, get_perms + +from utils import Config, get_player_input_type, add_empty_fields, base_embed, get_name +config = Config() + + + +class ServerStatusWidgets(commands.Cog): + """Server Status widget Extension""" + + def __init__(self, bot): + self.db = sqlite3.connect('instances.db') + self.cur = self.db.cursor() + self.bot = bot + + + @commands.group(invoke_without_command=True, name="widget", description="Create, select and manage widgets", + usage="r!server widget mangagment", aliases=["status_widget"]) + @check_perms(instance=True) + async def widget(self, ctx): + await ctx.send( + f"**Available Operations**\n{ctx.prefix}cog reload [cog]\n{ctx.prefix}cog enable \n{ctx.prefix}cog disable \n{ctx.prefix}cog info ") + + @widget.command() + @check_perms(instance=True) + async def setup(self, ctx): + now = datetime.now() + instances = get_available_instances(ctx.author, ctx.guild.id) + which_instance = await self.ask_instance(ctx) + actual_instance = which_instance - 1 + instance_id = instances[actual_instance][0].id + inst = self.bot.cache.instance(instance_id, by_inst_id=True).update() + playercount = len(inst.players) + + embed = discord.Embed(title="Test Server", color=0xde1b1b) + embed.add_field(name="Current Players", value=f"{str(playercount)}/100", inline=False) + embed.add_field(name="Current Map: ", value=inst.current_map) + embed.add_field(name="Next Map: ", value=inst.next_map) + embed.set_footer(text="Last updated: " + now.strftime("%H:%M:%S")) + + channel_id = ctx.message.channel.id + message_id = await ctx.send(embed=embed) + self.cur.execute(f'INSERT INTO widget_config VALUES (?,?,?)', (instance_id, channel_id, message_id.id)) + self.db.commit() + print(message_id) + + async def ask_instance(self, ctx): + instances = get_available_instances(ctx.author, ctx.guild.id) + await ctx.author.send(f'Please select Instance"') + embed = discord.Embed(title=f"Available Instances ({str(len(instances))})") + embed.set_author(name=str(ctx.author), icon_url=ctx.author.avatar.url) + acceptable_inputs = [] + for i, (inst, perms) in enumerate(instances): + try: self.bot.cache.instance(inst.id, by_inst_id=True) + except: availability = "\🔴" + else: availability = "\🟢" + + acceptable_inputs.append(str(i+1)) + perms = ", ".join([perm for perm, val in perms.to_dict().items() if val]) + embed.add_field(name=f"{str(i+1)} | {inst.name} {availability}", value=None) + embed = add_empty_fields(embed) + + msg = await ctx.author.send(embed=embed) + + def check(message): + return message.channel == msg.channel and message.author == ctx.author + + answer = "" + while answer not in acceptable_inputs: + answer = await self.bot.wait_for('message', check=check, timeout=120.0) + answer = answer.content.lower() + if answer not in acceptable_inputs: + await ctx.author.send(f'Send Server ID Number"') + return int(answer) + + @widget.command() + async def start(self, ctx): + #TODO Remove this and just and just have extension loaded + self.update_widget.start() + await ctx.send("Starting Wdiget") + + @tasks.loop(seconds=30.0) + async def update_widget(self): + self.cur.execute('SELECT * FROM widget_config') + res = self.cur.fetchall() + for instance_id, channel_id, message_id in res: + channel = self.bot.get_channel(channel_id) + message = channel.get_partial_message(message_id) + now = datetime.now() + inst = self.bot.cache.instance(instance_id, by_inst_id=True).update() + playercount = len(inst.players) + embed = discord.Embed(title="Test Server", color=0xde1b1b) + embed.add_field(name="Current Players", value=f"{str(playercount)}/100", inline=False) + embed.add_field(name="Current Map: ", value=inst.current_map) + embed.add_field(name="Next Map: ", value=inst.next_map) + embed.set_footer(text="Last updated: " + now.strftime("%H:%M:%S")) + await message.edit(embed=embed) + + + + + +async def setup(bot): + await bot.add_cog(ServerStatusWidgets(bot)) diff --git a/requirements.txt b/requirements.txt index 8f4e023..34dacf6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ -discord.py>=2.1.0 -pytz -numpy \ No newline at end of file +discord.py>=2.3.2 +pytz~=2023.3.post1 +numpy~=1.26.0 \ No newline at end of file From 9ab49060cc2ca969ec2f98b0b29a14599a4031cf Mon Sep 17 00:00:00 2001 From: jneil Date: Sun, 8 Oct 2023 17:26:25 +0100 Subject: [PATCH 2/5] First Draft of server_widgets.py --- cogs/server_widgets.py | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/cogs/server_widgets.py b/cogs/server_widgets.py index 67ae582..7373e10 100644 --- a/cogs/server_widgets.py +++ b/cogs/server_widgets.py @@ -1,15 +1,6 @@ import discord -from discord.ext import commands -import os -from pathlib import Path -from typing import Literal, Optional -from discord.ext import commands -from discord.ext.commands import Greedy, Context # or a subclass of yours -from utils import Config -import discord from discord.ext import commands, tasks -import json -from ast import literal_eval + from datetime import datetime import sqlite3 From 0a4b3e83beb2beb670925a1efc7fe6af47bbc68c Mon Sep 17 00:00:00 2001 From: jneil Date: Sun, 8 Oct 2023 17:33:32 +0100 Subject: [PATCH 3/5] First Draft of server_widgets.py --- cogs/server_widgets.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cogs/server_widgets.py b/cogs/server_widgets.py index 7373e10..b0fdba0 100644 --- a/cogs/server_widgets.py +++ b/cogs/server_widgets.py @@ -81,7 +81,7 @@ def check(message): @widget.command() async def start(self, ctx): - #TODO Remove this and just and just have extension loaded + #TODO Remove this and just and just have extension loaded when bot is ready or only have the tasks start when the bot is ready self.update_widget.start() await ctx.send("Starting Wdiget") From adf09ac1c2abdfddd48dc7d492c04894e0529417 Mon Sep 17 00:00:00 2001 From: jneil Date: Sun, 8 Oct 2023 17:36:05 +0100 Subject: [PATCH 4/5] First Draft of server_widgets.py --- cogs/server_widgets.py | 1 + 1 file changed, 1 insertion(+) diff --git a/cogs/server_widgets.py b/cogs/server_widgets.py index b0fdba0..757280b 100644 --- a/cogs/server_widgets.py +++ b/cogs/server_widgets.py @@ -46,6 +46,7 @@ async def setup(self, ctx): channel_id = ctx.message.channel.id message_id = await ctx.send(embed=embed) + #TODO Add functionalty to prevent widgets for the same instance being added to the same channel self.cur.execute(f'INSERT INTO widget_config VALUES (?,?,?)', (instance_id, channel_id, message_id.id)) self.db.commit() print(message_id) From de552d6af322d3bd5c4523e7b061d9ba437f3e92 Mon Sep 17 00:00:00 2001 From: jneil Date: Sun, 8 Oct 2023 17:37:26 +0100 Subject: [PATCH 5/5] Add TODO to be compelted --- cogs/server_widgets.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cogs/server_widgets.py b/cogs/server_widgets.py index 757280b..167e3d5 100644 --- a/cogs/server_widgets.py +++ b/cogs/server_widgets.py @@ -19,6 +19,9 @@ def __init__(self, bot): self.cur = self.db.cursor() self.bot = bot + # TODO Add functionality to remove widgets + # TODO Add functionality to check for removed messages and remove that setup from the db + @commands.group(invoke_without_command=True, name="widget", description="Create, select and manage widgets", usage="r!server widget mangagment", aliases=["status_widget"]) @@ -38,6 +41,7 @@ async def setup(self, ctx): inst = self.bot.cache.instance(instance_id, by_inst_id=True).update() playercount = len(inst.players) + #TODO Get Instance Name! embed = discord.Embed(title="Test Server", color=0xde1b1b) embed.add_field(name="Current Players", value=f"{str(playercount)}/100", inline=False) embed.add_field(name="Current Map: ", value=inst.current_map)