From fccac89e2f3e01ced99a00b98aa83c46116d7ac6 Mon Sep 17 00:00:00 2001 From: laToufff Date: Thu, 15 Dec 2022 19:56:15 +0100 Subject: [PATCH] add remove_channel --- code/cogs/chat.py | 13 +++++++++-- code/cogs/setup.py | 57 +++++++++++++++++++++++++++++++++++++++++----- code/config.py | 5 +--- 3 files changed, 63 insertions(+), 12 deletions(-) diff --git a/code/cogs/chat.py b/code/cogs/chat.py index 1f19892..90a93a3 100644 --- a/code/cogs/chat.py +++ b/code/cogs/chat.py @@ -21,7 +21,9 @@ class Chat (discord.Cog) : debug(f"The user {ctx.author.display_name} ran the say command command in the channel {ctx.channel} of the guild {ctx.guild}, named {ctx.guild.name}") await ctx.respond("Message sent !", ephemeral=True) await ctx.send(message) -async def on_message_process(message: discord.Message, self): + + +async def on_message_process(message: discord.Message, self: Chat): #my code #debug the thread id debug(f"Thread id: {threading.get_ident()}") @@ -37,16 +39,23 @@ async def on_message_process(message: discord.Message, self): return #check if the message has been sent in the channel set in the database c.execute("SELECT channel_id FROM data WHERE guild_id = ?", (message.guild.id,)) + #select channels from the database + cp.execute("SELECT * FROM channels WHERE guild_id = ?", (message.guild.id,)) + channels = cp.fetchone()[1:] + debug(f"Channels: {channels}") + debug(f"Message channel: {message.channel.id}") try : original_message = await message.channel.fetch_message(message.reference.message_id) except : original_message = None if original_message != None and original_message.author.id != self.bot.user.id: original_message = None - if str(message.channel.id) != str(c.fetchone()[0]): + if str(message.channel.id) != str(c.fetchone()[0]) : #check if the message is a mention or if the message replies to the bot if original_message != None: debug("wrong channel, but reply") elif message.content.find("<@"+str(self.bot.user.id)+">") != -1: debug("wrong channel, but mention") + elif str(message.channel.id) in channels: + debug("in a channel that is in the database") else : debug("The message has been sent in the wrong channel") return diff --git a/code/cogs/setup.py b/code/cogs/setup.py index bc64c80..b2d3520 100644 --- a/code/cogs/setup.py +++ b/code/cogs/setup.py @@ -80,7 +80,7 @@ class Setup (discord.Cog) : await ctx.respond("Disabled", ephemeral=True) #create a command calles "add channel" that can only be used in premium servers - @discord.slash_command(name="add_channel", description="Add a channel to the list of channels") + @discord.slash_command(name="add_channel", description="Add a channel to the list of channels. Premium only.") @discord.option(name="channel", description="The channel to add", type=discord.TextChannel, required=False) async def add_channel(self, ctx: discord.ApplicationContext, channel: discord.TextChannel = None): debug(f"The user {ctx.author} ran the add_channel command in the channel {ctx.channel} of the guild {ctx.guild}, named {ctx.guild.name}") @@ -101,13 +101,17 @@ class Setup (discord.Cog) : if channel is None: channel = ctx.channel #check if the channel is already in the list + c.execute("SELECT channel_id FROM data WHERE guild_id = ?", (ctx.guild.id,)) + if str(channel.id) == c.fetchone()[0]: + await ctx.respond("This channel is already set as the main channel", ephemeral=True) + return cp.execute("SELECT * FROM channels WHERE guild_id = ?", (ctx.guild.id,)) guild_channels = cp.fetchone() if guild_channels is None: # if the channel is not in the list, add it cp.execute("INSERT INTO channels VALUES (?, ?, ?, ?, ?, ?)", (ctx.guild.id, channel.id, None, None, None, None)) connp.commit() - await ctx.respond("Added", ephemeral=True) + await ctx.respond(f"Added channel **{channel.name}**", ephemeral=True) return channels = guild_channels[1:] if str(channel.id) in channels: @@ -115,10 +119,51 @@ class Setup (discord.Cog) : return for i in range(5): if channels[i] == None: - cp.execute(f"UPDATE channels SET channel{i+1} = ? WHERE guild_id = ?", (channel.id, ctx.guild.id)) + cp.execute(f"UPDATE channels SET channel{i} = ? WHERE guild_id = ?", (channel.id, ctx.guild.id)) connp.commit() - await ctx.respond("Added", ephemeral=True) + await ctx.respond(f"Added channel **{channel.name}**", ephemeral=True) return await ctx.respond("You can only add 5 channels", ephemeral=True) - - \ No newline at end of file + + #create a command called "remove channel" that can only be used in premium servers + @discord.slash_command(name="remove_channel", description="Remove a channel from the list of channels. Premium only.") + @discord.option(name="channel", description="The channel to remove", type=discord.TextChannel, required=False) + async def remove_channel(self, ctx: discord.ApplicationContext, channel: discord.TextChannel = None): + debug(f"The user {ctx.author} ran the remove_channel command in the channel {ctx.channel} of the guild {ctx.guild}, named {ctx.guild.name}") + #check if the guild is in the database + c.execute("SELECT * FROM data WHERE guild_id = ?", (ctx.guild.id,)) + if c.fetchone() is None: + await ctx.respond("This server is not setup", ephemeral=True) + return + #check if the guild is premium + try : + cp.execute("SELECT premium FROM data WHERE guild_id = ?", (ctx.guild.id,)) + premium = cp.fetchone()[0] + except : + premium = 0 + if not premium: + await ctx.respond("This server is not premium", ephemeral=True) + return + if channel is None: + channel = ctx.channel + #check if the channel is in the list + cp.execute("SELECT * FROM channels WHERE guild_id = ?", (ctx.guild.id,)) + guild_channels = cp.fetchone() + c.execute("SELECT channel_id FROM data WHERE guild_id = ?", (ctx.guild.id,)) + if str(channel.id) == c.fetchone()[0]: + await ctx.respond("This channel is set as the main channel and therefore cannot be removed. Type /setup to change the main channel.", ephemeral=True) + return + if guild_channels is None: + await ctx.respond("This channel was not added. Nothing changed", ephemeral=True) + return + channels = guild_channels[1:] + if str(channel.id) not in channels: + await ctx.respond("This channel was not added. Nothing changed", ephemeral=True) + return + #remove the channel from the list + for i in range(5): + if channels[i] == str(channel.id): + cp.execute(f"UPDATE channels SET channel{i} = ? WHERE guild_id = ?", (None, ctx.guild.id)) + connp.commit() + await ctx.respond(f"Removed channel **{channel.name}**", ephemeral=True) + return \ No newline at end of file diff --git a/code/config.py b/code/config.py index 011bfe1..44204b5 100644 --- a/code/config.py +++ b/code/config.py @@ -16,7 +16,4 @@ cp = connp.cursor() c.execute('''CREATE TABLE IF NOT EXISTS data (guild_id text, channel_id text, api_key text, is_active boolean, max_tokens integer, temperature real, frequency_penalty real, presence_penalty real, uses_count_today integer, prompt_size integer, prompt_prefix text, tts boolean, pretend_to_be text, pretend_enabled boolean)''') cp.execute('''CREATE TABLE IF NOT EXISTS data (user_id text, guild_id text, premium boolean)''') # create table called "channels" if it does not exist with the following columns: guild_id, channel1, channel2, channel3, channel4, channel5 -cp.execute('''CREATE TABLE IF NOT EXISTS channels (guild_id text, channel1 text, channel2 text, channel3 text, channel4 text, channel5 text)''') - -cp.execute("DELETE FROM channels") -connp.commit() \ No newline at end of file +cp.execute('''CREATE TABLE IF NOT EXISTS channels (guild_id text, channel0 text, channel1 text, channel2 text, channel3 text, channel4 text)''') \ No newline at end of file