add remove_channel

This commit is contained in:
laToufff
2022-12-15 19:56:15 +01:00
parent 8602dfe330
commit fccac89e2f
3 changed files with 63 additions and 12 deletions

View File

@@ -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)
#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