role creation and adding working
crispy-caesus 114518720+crispy-caesus@users.noreply.github
Fri, 09 Aug 2024 20:27:21 +0200
M
bot.py
→
bot.py
@@ -1,42 +1,63 @@
import discord +import discord.ext.commands import bot_token import db as database import aiosqlite bot = discord.Bot() -#@bot.on_guild_join() -#def joined(guild) - - - -@bot.slash_command() -async def hello(ctx, name: str = None): - name = name or ctx.author.name - await ctx.respond(f"Hello {name}!") - -@bot.slash_command() -async def club_hinzuftügen(ctx, club_name, emoji, rolle): +@bot.slash_command(description="Erstellt einen Booster Club") +@discord.ext.commands.has_role(1170646611332956208) +async def club_hinzuftügen(ctx, kanalname, emoji, rollenname): + user = ctx.author.id db = database.Database(f"{ctx.guild.id}.db") await db.create_tables() + check = await db.check_if_club_creatable(kanalname, user) + if check != None: + await ctx.respond(check) + return - user = ctx.author.id - channel_name = f"[{emoji}]{club_name}" - response = await db.create_club(channel_name, user, rolle[3:-1]) + await ctx.guild.create_role(name=rollenname, color=0x5460D9, mentionable=False) + + role_converter = discord.ext.commands.RoleConverter() + rolle = await role_converter.convert(ctx, rollenname) + channel_name = f"[{emoji}]{kanalname}" + response = await db.create_club(channel_name, user, rolle.id) if response == None: await ctx.respond(f"{channel_name} created!") else: await ctx.respond(response) -@bot.slash_command() +@bot.slash_command(description="Fügt Member zu eigenem Club hinzu") async def mitglied_hinzufuegen(ctx, member): db = database.Database(f"{ctx.guild.id}.db") response = await db.add_member(member[2:-1], ctx.author.id) + if response != None: + await ctx.respond(response) + return + + member_converter = discord.ext.commands.MemberConverter() + member = await member_converter.convert(ctx, member) + + response = await db.select_club_by_owner(ctx.author.id) + if response == None: + await ctx.respond("Du hast keinen Club") + return + role_converter = discord.ext.commands.RoleConverter() + role = await role_converter.convert(ctx, str(response)) + + + await member.add_roles(role) + await ctx.respond(":white_check_mark:") @bot.user_command(name="Say Hello") async def hi(ctx, user): await ctx.respond(f"{ctx.author.mention} says hello to {user.name}!") + +@bot.command(description="Sends the bot's latency.") # this decorator makes a slash command +async def ping(ctx): # a slash command will be created with the name "ping" + await ctx.respond(f"Pong! Latency is {bot.latency}") bot.run(bot_token.token)
M
db.py
→
db.py
@@ -43,7 +43,7 @@ await db.execute(sql, args)
await db.commit() except Error as e: print(e) - return("Club existiert schon") + return("Error") async def add_member(self, member: int, owner: int): print(f"DB: received:\n member: {member}\n owner: {owner}")@@ -58,7 +58,7 @@ await db.execute(sql, args)
await db.commit() except Error as e: print(e) - return("Member existiert schon") + return("Error")@@ -86,4 +86,21 @@ except Error as e:
print(e) return clubs + async def select_club_by_owner(self, member: int): + async with aiosqlite.connect(self.db_name) as db: + async with db.execute("SELECT role_id FROM clubs WHERE owner = ?;", (member,)) as cursor: + async for row in cursor: + return(row[0]) + + async def check_if_club_creatable(self, channel_name, owner): + async with aiosqlite.connect(self.db_name) as db: + async with db.execute("SELECT owner FROM clubs WHERE owner = ?;", (owner,)) as cursor: + async for row in cursor: + if row != None: + return("Du hast bereits einen Club erstellt") + async with db.execute("SELECT channel_name FROM clubs WHERE channel_name = ?;", (channel_name,)) as cursor: + async for row in cursor: + if row != None: + return("Es existiert bereits ein Channel mit diesem Namen") + return None
M
test.py
→
test.py
@@ -2,12 +2,10 @@ import db
import asyncio async def main(): - my_db = db.Database("test.db") + my_db = db.Database("1170646611236487208.db") await my_db.create_tables() await my_db.list_tables() - print(await my_db.create_club("test channel2", 123, 23)) print(await my_db.select_clubs()) - await my_db.add_member(3, 23) - print(await my_db.select_clubs_of_member(3)) + await my_db.check_if_club_creatable("asdsad", 621759962280099840) asyncio.run(main())