Skip to content

Instantly share code, notes, and snippets.

@Glowstik-YT
Created January 7, 2022 17:57
Show Gist options
  • Save Glowstik-YT/fc7f4f2cdc17e47515486158c4331aa8 to your computer and use it in GitHub Desktop.
Save Glowstik-YT/fc7f4f2cdc17e47515486158c4331aa8 to your computer and use it in GitHub Desktop.
my tags code... ig
from time import time
from nextcord.ext import commands
import nextcord
import aiosqlite
async def runtag(ctx, name, guild):
async with aiosqlite.connect("main.db") as db:
async with db.cursor() as cursor:
await cursor.execute("SELECT content FROM tags WHERE name = ? AND guild = ?", (name, guild.id,))
data = await cursor.fetchone()
if data is None:
em = nextcord.Embed(title="Tag Error", description=f"Tag by then name of `{name}` is not found")
return await ctx.send(embed = em)
else:
await cursor.execute("SELECT creator FROM tags WHERE name = ? AND guild = ?", (name, guild.id,))
creator = await cursor.fetchone()
em = nextcord.Embed(title=f"Tag {name}", description=f"{data[0]}")
em.set_footer(text=f"Created by: {creator[0]}")
await ctx.send(embed=em)
await db.commit()
class TagConfirm(nextcord.ui.View):
def __init__(self, ctx):
super().__init__()
self.value = None
self.ctx = ctx
@nextcord.ui.button(
label="Yes", style=nextcord.ButtonStyle.green, custom_id="yes"
)
async def confirm(self, button: nextcord.ui.Button, interaction: nextcord.Interaction):
if not interaction.user == self.ctx.author:
return await interaction.response.send_message('You have to run the command yourself to be able to use the buttons smh', ephemeral=True)
self.value = True
button.disabled = True
self.stop()
@nextcord.ui.button(label="No", style=nextcord.ButtonStyle.red, custom_id="no")
async def cancel(self, button: nextcord.ui.Button, interaction: nextcord.Interaction):
if not interaction.user == self.ctx.author:
return await interaction.response.send_message('You have to run the command yourself to be able to use the buttons smh', ephemeral=True)
self.value = False
button.disabled = True
self.stop()
class Tags(commands.Cog):
def __init__(self, client):
self.client = client
@commands.Cog.listener()
async def on_guild_remove(self, guild):
async with aiosqlite.connect("main.db") as db:
async with db.cursor() as cursor:
await cursor.execute("SELECT content FROM tags WHERE guild = ?", (guild.id,))
data = await cursor.fetchall()
if data:
for tag in data:
await cursor.execute(f"DELETE FROM tags WHERE guild = ?", (guild.id,))
else:
return
await db.commit()
@commands.command(name='tag', description='Run a created tag')
@commands.cooldown(1, 25, commands.BucketType.user)
async def tag(self, ctx, name):
try:
await runtag(ctx, name, ctx.guild)
except Exception:
print(Exception)
return
@commands.command(name='createtag', decription='Create a new tag')
@commands.cooldown(1, 300, commands.BucketType.user)
async def createtag(self, ctx, name=None, *, content=None):
if name is None:
em = nextcord.Embed(title='Create Tag Error', description="Name for tag is not defiend :I")
return await ctx.send(embed = em)
if content is None:
em = nextcord.Embed(title='Create Tag Error', description="Content for tag is not defiend :I")
return await ctx.send(embed = em)
async with aiosqlite.connect("main.db") as db:
async with db.cursor() as cursor:
await cursor.execute("SELECT content FROM tags WHERE guild = ? AND name = ?", (ctx.guild.id, name,))
data = await cursor.fetchone()
if data is None:
await cursor.execute("INSERT INTO tags (name, content, guild, creator) VALUES (?,?,?,?)", (name, content, ctx.guild.id, ctx.author.id,))
em = nextcord.Embed(title="Create Tag Success", description=f"The tag `{name}` has been successfully added!")
em.add_field(name=name, value=content)
await ctx.send(embed=em)
if data:
view = TagConfirm(ctx)
em = nextcord.Embed(title="Create Tag Error", description=f"A tag by the name of `{name}` is already exists in the guild ._. | Click Yes or No if you would like to run the tag")
await ctx.send(embed=em, view=view)
await view.wait()
if view.value is None:
return
elif view.value:
await runtag(ctx, name, ctx.guild)
await db.commit()
@commands.command(name='changetag', description='Edit an existing tag')
@commands.cooldown(1, 25, commands.BucketType.user)
async def changetag(self, ctx, name, *, content):
if name is None:
em = nextcord.Embed(title='Change Tag Error', description="Name for tag is not defiend :I")
return await ctx.send(embed = em)
if content is None:
em = nextcord.Embed(title='Change Tag Error', description="Content for tag is not defiend :I")
return await ctx.send(embed = em)
async with aiosqlite.connect("main.db") as db:
async with db.cursor() as cursor:
await cursor.execute("SELECT content FROM tags WHERE guild = ? AND name = ? AND creator = ?", (ctx.guild.id, name, ctx.author.id,))
data = await cursor.fetchone()
if data:
await cursor.execute("UPDATE tags SET content = ? WHERE guild = ? AND name = ? AND creator = ?", (content, ctx.guild.id, name, ctx.author.id,))
em = nextcord.Embed(title="Change Tag Success", description=f"The tag `{name}` has been successfully updated!")
em.add_field(name=name, value=content)
await ctx.send(embed=em)
else:
em = nextcord.Embed(title="Change Tag Error", description=f"The tag `{name}` does not exist, please first create a tag then you can change its contents.")
await ctx.send(embed=em)
await db.commit()
@commands.command(name="tags", description="Lists all the tags in the server")
@commands.cooldown(1, 5, commands.BucketType.user)
async def tags(self, ctx):
async with aiosqlite.connect("main.db") as db:
async with db.cursor() as cursor:
await cursor.execute('SELECT name FROM tags WHERE guild = ?',(ctx.guild.id,))
data = await cursor.fetchall()
if data:
em = nextcord.Embed(title="Server Tags", description="All of the tags in the server are listed below")
taglist = []
tagnum = 0
for name in data:
tagnum += 1
taglist.append(name[0])
em.add_field(name=f"{tagnum} Tags", value=f"```{taglist}```")
await ctx.send(embed=em)
else:
em = nextcord.Embed(title="Server Tags Error", description="No tags have been created in this server...")
await ctx.send(embed=em)
await db.commit()
@commands.command(name='removetag', description='Remove a tag from the database')
@commands.cooldown(1, 60, commands.BucketType.user)
async def removetag(self, ctx, name=None):
if name is None:
em = nextcord.Embed(title='Remove Tag Error', description="Name for tag is not defiend :I")
return await ctx.send(embed = em)
async with aiosqlite.connect("main.db") as db:
async with db.cursor() as cursor:
await cursor.execute("SELECT content FROM tags WHERE guild = ? AND name = ? AND creator = ?", (ctx.guild.id, name, ctx.author.id,))
data = await cursor.fetchone()
if data:
await cursor.execute(f"DELETE FROM tags WHERE guild = ? AND name = ? AND creator = ?", (ctx.guild.id, name, ctx.author.id,))
em = nextcord.Embed(title="Remove Tag Success", description=f"The tag by the name of `{name}` is deleted")
await ctx.send(embed=em)
else:
if ctx.author.guild_permissions.manage_messages:
await cursor.execute("SELECT content FROM tags WHERE guild = ? AND name = ?", (ctx.guild.id, name,))
data = await cursor.fetchone()
if data:
await cursor.execute(f"DELETE FROM tags WHERE guild = ? AND name = ?", (ctx.guild.id, name,))
em = nextcord.Embed(title="Remove Tag Success", description=f"The tag by the name of `{name}` is deleted")
await ctx.send(embed=em)
else:
em = nextcord.Embed(title="Remove Tag Error", description="Failed to delete tag | You must be the tag owner or have the `Manage Messages` permission to delete tags")
return await ctx.send(embed=em)
await db.commit()
def setup(client):
client.add_cog(Tags(client))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment