index — Deutsche-Haus-Bot @ 938b6f9946a582a8af2e0e595a07a0790ebba6c3

Discord bot to dynamically create voice chats for clubs that boosters can create

bot.py (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
import discord
import discord.ext.commands
import asyncio

import bot_token
import db as database
import logic

# ======================== STARTUP =========================== #

intents = discord.Intents.default()
intents.message_content = True

bot = discord.Bot(intents=intents)

role_converter = discord.ext.commands.RoleConverter()
member_converter = discord.ext.commands.MemberConverter()

# ======================= INITIALIZATION ========================== #

@bot.listen()
async def on_guild_join(guild):
    await logic.on_guild_join(guild.id)


# ======================= SETUP ========================= #

@bot.slash_command(description="Gibt alle Commands zurück, die für die Initialisation des Bots nötig sind") 
async def setup(ctx):
    if not ctx.author.guild_permissions.administrator:
        ctx.respond("Du musst Administrator sein, um diesen Command auszuführen")
        return
    await ctx.respond(await logic.setup(ctx.guild.id))

@bot.slash_command(description="Setzt die Booster Rolle intern im Bot")
async def setze_booster_rolle(ctx, booster_rolle):
    if not ctx.author.guild_permissions.administrator:
        ctx.respond("Du musst Administrator sein, um diesen Command auszuführen")
        return
    await ctx.respond(await logic.add_booster_role(ctx.guild.id, int(booster_rolle[3:-1])))

@bot.slash_command(description="Setzt den Verteiler Channel intern im Bot")
async def setze_verteiler_channel(ctx, verteiler_channel_id):
    if not ctx.author.guild_permissions.administrator:
        ctx.respond("Du musst Administrator sein, um diesen Command auszuführen")
        return
    await ctx.respond(await logic.add_distributor_channel(ctx.guild.id, int(verteiler_channel_id)))

@bot.slash_command(description="Setzt die Kategorie, in der die Clubs erstellt werden sollen")
async def setze_club_kategorie(ctx, kategorie_id):
    if not ctx.author.guild_permissions.administrator:
        ctx.respond("Du musst Administrator sein, um diesen Command auszuführen")
        return
    await ctx.respond(await logic.add_club_category(ctx.guild.id, int(kategorie_id)))



"""
@bot.slash_command(description="Füge neue Rollen zu der Datenbank hinzu")
@discord.ext.commands.has_role()
async def
"""

@bot.slash_command()
async def get_existing_roles(ctx):
    results = ctx.guild.roles
    print(results)


@bot.slash_command()
async def test(ctx):
    await ctx.guild.create_role(name = "hi", color=int("0x"+("#5460D9"[1:]), 16)+0x200, mentionable = False)
    await ctx.respond("hi")

# ======================== ADD CLUB ================================ #

@bot.slash_command(description="Erstellt einen Booster Club")
async def club_hinzufügen(ctx, kanalname, emoji, rollenname, rollenfarbe):

    emoji = emoji.strip()

    if ctx.author.get_role(await logic.add_club(ctx.guild.id, kanalname, emoji, rollenname, rollenfarbe, ctx.author.id, 1))== None:
        await ctx.respond(":x: Du bist kein Booster")
        return
    response = await logic.add_club(ctx.guild.id, kanalname, emoji, rollenname, rollenfarbe, ctx.author.id, 2)
    if type(response) == str:
        await ctx.respond(response)
        return
    else:
        await ctx.guild.create_role(name = response[0], color = response[1], mentionable = False)

    role = await role_converter.convert(ctx, rollenname)

    await ctx.author.add_roles(role)
    
    await ctx.respond(await logic.add_club(ctx.guild.id, kanalname, emoji, rollenname, role.id, ctx.author.id, 3))
    
    db = database.Database(f"{ctx.guild.id}.db")
    await db.add_member(ctx.author.id, ctx.author.id)



# ==================== EDIT CLUBS ====================== #

@bot.slash_command(description="Ändert den Channel Name des Clubs")
async def club_channel_name_aendern(ctx, neuer_name):
    await ctx.respond(await logic.club_change_channel_name(ctx.guild.id, ctx.author.id, neuer_name))

# ========================= dsfsf ======================= #
@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 = await member_converter.convert(ctx, member)

    response = await db.select_role_id_by_owner(ctx.author.id)
    if response == None:
        await ctx.respond("Du hast keinen Club")
        return
    print(f"{type(response)}: {response}")
    role = discord.utils.get(ctx.guild.roles, id=response)


    await member.add_roles(role)
    await ctx.respond(":white_check_mark:")


@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}")

# ==================== distributor vc ======================= #

distributor_vcs = []

@bot.event
async def on_voice_state_update(user, before, after):
    if before.channel != after.channel:

        if after.channel:
            db = database.Database(f"{after.channel.guild.id}.db")

            

            if after.channel.id == await db.get_discord_id("distributor_channel_id"):
                print(f"{user.name} Joined The {after.channel.name} VC")
                print(after.channel.members)
                
               


                db_response = await db.get_channel_name_role_name_by_member(user.id)
                message = "Welchen Club-Kanal willst du öffnen?"
                for i in range(len(db_response)):
                    message += f"\n**{i+1}.** {db_response[i][0]}"

                print(message)
                
                await after.channel.send(message)
                
                def check(m):
                    return m.author == user and m.channel == after.channel
                

                done = False
                while not done:

                    try:
                        response = await bot.wait_for('message', check=check, timeout=30.0)
                    except asyncio.TimeoutError:
                        await after.channel.send("Zu spät")
                    else:
                        try: 
                            int(response.content)
                        except ValueError:
                            await after.channel.send(":x: Konnte nicht in ganze Zahl umwandeln")
                            continue
                        if int(response.content) <= len(db_response) and int(response.content) > 0:
                            channel_name = db_response[int(response.content)-1][0]

                            category = discord.utils.get(after.channel.guild.categories, id=await db.get_discord_id("new_channel_category_id"))
                            if category is None:
                                print("Kategorie nicht gefunden")

                            role = discord.utils.get(after.channel.guild.roles, id=db_response[int(response.content)-1][1])
                            if role is None:
                                print("Rolle nicht gefunden")

                            bot_member = after.channel.guild.me
                            club_owner = discord.utils.get(after.channel.guild.members, id=await db.get_owner_by_club_id(db_response[int(response.content)-1][2]))


                            bot_overwrites = discord.PermissionOverwrite (
                                move_members = True,
                                view_channel = True,
                                manage_channels = True
                            )

                            club_member_overwrites = discord.PermissionOverwrite (
                                view_channel = True
                            )

                            club_owner_overwrites = discord.PermissionOverwrite (
                                manage_channels = True,
                                mute_members = True,
                                deafen_members = True,
                                move_members = True
                            )

                            default_overwrites = discord.PermissionOverwrite (
                                view_channel = False
                            )

                            overwrites = {
                                after.channel.guild.default_role: default_overwrites,
                                bot_member: bot_overwrites,
                                club_owner: club_owner_overwrites,
                                role: club_member_overwrites
                            }


                            voice_channel = await category.create_voice_channel(
                                name = channel_name,
                                overwrites=overwrites
                            )
                            distributor_vcs.append(voice_channel.id)
                            done = True
                        else:
                            await after.channel.send(":x: Nicht zulässige Zahl")

                if user.voice:
                    await user.move_to(voice_channel)
        

        if before.channel and before.channel.id in distributor_vcs:
            if len(before.channel.members) == 0:
                await before.channel.delete(reason="Niemand ist mehr verbunden")
                distributor_vcs.remove(before.channel.id)


bot.run(bot_token.token)