A
A
ah0R2021-08-28 13:50:39
Python
ah0R, 2021-08-28 13:50:39

How can I make the bot assign roles not only to me, but also to server members upon request?

I make a bot for myself and my friends. It seems that everything is set up, but it gives out roles only to me, when someone else clicks, it only receives a message.

Also sometimes the "Interaction Error" crashes.

Here is the code itself:

bot = commands.Bot( command_prefix = '!')

@bot.event
async def on_ready():
    DiscordComponents(bot)
    await bot.change_presence(activity=discord.Game(name="Monro FamQ"))
    res =  bot.get_channel(880840475312001065)
    log = discord.Embed(title = '**Работает**', colour = 0xFFFF00)
    await res.send(
                      embed = log)
    print("Бот включен")
    

@bot.command()
async def test(ctx):
        emb = discord.Embed(title = ':pencil: Роли Discord', description = "**Автоматическое выдача роли - <@&792385427679477790>**", colour = 0xFF005C)
        emb.set_image(url = 'https://media.discordapp.net/attachments/716571065270140938/880854910005047366/-7.png?width=701&height=701')
        emb.set_footer(text = 'Monro FamQ', icon_url = 'https://media.discordapp.net/attachments/716571065270140938/880854176735854622/-6.png?width=701&height=701')
        await ctx.send(
            embed = emb,
            components = [
                Button(style = ButtonStyle.green, label = "Стать другом Monro!", emoji = '❤️'),
                Button(style = ButtonStyle.blue, label = "Получить дополнительные роли!", emoji = '❤️'),
                Button(style = ButtonStyle.red, label = "Перестать быть другом Monro!", emoji = '')
            ]
        )

        
        while True:

            
            res = await bot.wait_for('button_click')
            member = res.guild.get_member(res.user.id)
            role = res.guild.get_role(792385427679477790)
            rolex = res.guild.get_role(792399225459310613)
            channel = res.guild.get_channel(880840475312001065)
            log = discord.Embed(title = '**Выданы Роли**', colour = 0x00FF00)
            if res.component.label.startswith("Стать другом Monro!"):
                await res.respond(content="Поздравляю, тебе выданы роль <@&792385427679477790> и <@&792399225459310613>!")
                await ctx.author.add_roles(role, rolex)
                await channel.set_permissions(ctx.author, overwrite = discord.PermissionOverwrite(view_channel = True, send_messages = True))
                await channel.send(
                      embed = log)

            res = await bot.wait_for('button_click')
            member = res.guild.get_member(res.user.id)
            channel = res.guild.get_channel(880840475312001065)
            log = discord.Embed(title = ', получил инструкцию', colour = 0x0000FF)
            if res.component.label.startswith("Получить дополнительные роли!"):
                await res.respond(content="Напиши, <@!416881340487827456>, в личные сообщения!")
                await channel.set_permissions(ctx.author, overwrite = discord.PermissionOverwrite(view_channel = True, send_messages = True))
                await channel.send(
                      embed = log)

            res = await bot.wait_for('button_click')
            member = res.guild.get_member(res.user.id)
            role = res.guild.get_role(792385427679477790)
            rolex = res.guild.get_role(792399225459310613)
            channel = res.guild.get_channel(880840475312001065)
            log = discord.Embed(title = 'Забрал Роли', colour = 0xFF0000)
            if res.component.label.startswith("Перестать быть другом Monro!"):
                await res.respond(content="Удачи!")
                await ctx.author.remove_roles(role, rolex)
                await channel.set_permissions(ctx.author, overwrite = discord.PermissionOverwrite(view_channel = True, send_messages = True))
                await channel.send(
                      embed = log)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
ah0R, 2021-08-29
@ah0R

I found a solution to the problem on my own (basic)
It was necessary to replace ctx with res so that it reacts precisely to pressing, and not to the one who created the message

res = await bot.wait_for('button_click')
            current_time = datetime.now().time()
            member = res.guild.get_member(res.user.id)
            role = res.guild.get_role(792385427679477790)
            rolex = res.guild.get_role(792399225459310613)
            channel = res.guild.get_channel(880840475312001065)
            log = discord.Embed(title = 'Получил Роль', description = '**Участник** \n' '<@%d>' % (res.author.id), colour = 0x00FF00)
            log.set_footer(text = "Сегодня в %d:%d" % (current_time.hour, current_time.minute))
            if res.component.label.startswith("Стать другом Monro!"):
                await res.respond(content="Поздравляю, тебе выданы роль <@&792385427679477790> и <@&792399225459310613>!")
                await res.author.add_roles(role, rolex)
                await channel.set_permissions(ctx.author, overwrite = discord.PermissionOverwrite(view_channel = True, send_messages = True))
                await channel.send(
                      embed = log)

But the problem with
It also sometimes crashes "Interaction Error"
left :(

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question