我不能在 discord.py 中制作超过 5 个按钮,这怎么办以及如何清理它们?
I can't make more than 5 buttons in discord.py displesh how can this be done and how to clean them?
我想制作一个会从角色中出来的机器人,但它不允许我添加超过 5 个按钮,如何使用 discord.py dislash 来实现(或者你可以扔掉您自己的版本)以及如何通过单击按钮 2 次来删除此角色,并且有一个单独的按钮可以删除所有添加的角色。
import discord
from discord import member
from discord.ext import commands
from dislash import InteractionClient, ActionRow, Button, ButtonStyle
from discord_components import DiscordComponents, ComponentsBot
intents = discord.Intents.all()
bot = commands.Bot(command_prefix = "!", intents = intents)
bot.remove_command("help")
inter_client = InteractionClient(bot)
@bot.event
async def on_ready():
print(f'Вы вошли как {bot.user}')
@bot.command()
async def verif(ctx):
emb = discord.Embed(
description =
f"""
1 {ctx.guild.name}, 1
""",
colour = 0xFF8C00
)
emb.set_thumbnail(url = 'https://cdn.discordapp.com/attachments/772850448892690462/880752123418136596/947d1f802c858b540b84bc3000fc2439_1_-removebg-preview.png')
emb.set_author(name = 'Роли')
row = ActionRow(
Button(
style = ButtonStyle.gray,
label = 'button1',
emoji = '',
custom_id = 'button1_button'
),
Button(
style = ButtonStyle.gray,
label = 'button2',
emoji = '',
custom_id = 'button2_button'
),
Button(
style = ButtonStyle.gray,
label = 'button3',
emoji = '❤',
custom_id = 'button3_button'
),
Button(
style = ButtonStyle.gray,
label = 'button4',
emoji = '',
custom_id = 'button4_button'
),
Button(
style = ButtonStyle.gray,
label = 'button5',
emoji = '',
custom_id = 'button5_button'
),
Button(
style = ButtonStyle.red,
label = 'button6',
emoji = '',
custom_id = 'button6_button'
),
)
await ctx.send(embed = emb, components = [row])
@bot.event
async def on_button_click(inter):
res = ' Role give !'
guild = bot.get_guild(inter.guild.id)
if inter.component.id == "button1_button":
verif = guild.get_role(ID ROLE)
member = inter.author
await member.add_roles(verif)
await inter.reply(res, ephemeral = True)
if inter.component.id == "button2_button":
verif = guild.get_role(ID ROLE)
member = inter.author
await member.add_roles(verif)
await inter.reply(res, ephemeral = True)
if inter.component.id == "button3_button":
verif = guild.get_role(ID ROLE)
member = inter.author
await member.add_roles(verif)
await inter.reply(res, ephemeral = True)
if inter.component.id == "button4_button":
verif = guild.get_role(ID ROLE)
member = inter.author
await member.add_roles(verif)
await inter.reply(res, ephemeral = True)
if inter.component.id == "button5_button":
verif = guild.get_role(ID ROLE)
member = inter.author
await member.add_roles(verif)
await inter.reply(res, ephemeral = True)
根据 Discord 的 API 文档,一个 Action Row 最多只能有 5 个按钮,一条消息最多可以有 5 个操作行。如果您想发送超过 5 个消息,则需要创建另一个操作行并将其作为组件发送。
rowA = ActionRow(
Button(...),
Button(...),
Button(...),
Button(...),
Button(...)
)
rowB = ActionRow(
Button(...),
Button(...)
)
await ctx.send(embed = emb, components = [rowA, rowB])
如果您不想每次都这样做,我建议创建一个辅助函数,它接收一个按钮列表,遍历它们,为每五个按钮创建操作行,然后 returns供您发送到 API.
的操作行列表
我想制作一个会从角色中出来的机器人,但它不允许我添加超过 5 个按钮,如何使用 discord.py dislash 来实现(或者你可以扔掉您自己的版本)以及如何通过单击按钮 2 次来删除此角色,并且有一个单独的按钮可以删除所有添加的角色。
import discord
from discord import member
from discord.ext import commands
from dislash import InteractionClient, ActionRow, Button, ButtonStyle
from discord_components import DiscordComponents, ComponentsBot
intents = discord.Intents.all()
bot = commands.Bot(command_prefix = "!", intents = intents)
bot.remove_command("help")
inter_client = InteractionClient(bot)
@bot.event
async def on_ready():
print(f'Вы вошли как {bot.user}')
@bot.command()
async def verif(ctx):
emb = discord.Embed(
description =
f"""
1 {ctx.guild.name}, 1
""",
colour = 0xFF8C00
)
emb.set_thumbnail(url = 'https://cdn.discordapp.com/attachments/772850448892690462/880752123418136596/947d1f802c858b540b84bc3000fc2439_1_-removebg-preview.png')
emb.set_author(name = 'Роли')
row = ActionRow(
Button(
style = ButtonStyle.gray,
label = 'button1',
emoji = '',
custom_id = 'button1_button'
),
Button(
style = ButtonStyle.gray,
label = 'button2',
emoji = '',
custom_id = 'button2_button'
),
Button(
style = ButtonStyle.gray,
label = 'button3',
emoji = '❤',
custom_id = 'button3_button'
),
Button(
style = ButtonStyle.gray,
label = 'button4',
emoji = '',
custom_id = 'button4_button'
),
Button(
style = ButtonStyle.gray,
label = 'button5',
emoji = '',
custom_id = 'button5_button'
),
Button(
style = ButtonStyle.red,
label = 'button6',
emoji = '',
custom_id = 'button6_button'
),
)
await ctx.send(embed = emb, components = [row])
@bot.event
async def on_button_click(inter):
res = ' Role give !'
guild = bot.get_guild(inter.guild.id)
if inter.component.id == "button1_button":
verif = guild.get_role(ID ROLE)
member = inter.author
await member.add_roles(verif)
await inter.reply(res, ephemeral = True)
if inter.component.id == "button2_button":
verif = guild.get_role(ID ROLE)
member = inter.author
await member.add_roles(verif)
await inter.reply(res, ephemeral = True)
if inter.component.id == "button3_button":
verif = guild.get_role(ID ROLE)
member = inter.author
await member.add_roles(verif)
await inter.reply(res, ephemeral = True)
if inter.component.id == "button4_button":
verif = guild.get_role(ID ROLE)
member = inter.author
await member.add_roles(verif)
await inter.reply(res, ephemeral = True)
if inter.component.id == "button5_button":
verif = guild.get_role(ID ROLE)
member = inter.author
await member.add_roles(verif)
await inter.reply(res, ephemeral = True)
根据 Discord 的 API 文档,一个 Action Row 最多只能有 5 个按钮,一条消息最多可以有 5 个操作行。如果您想发送超过 5 个消息,则需要创建另一个操作行并将其作为组件发送。
rowA = ActionRow(
Button(...),
Button(...),
Button(...),
Button(...),
Button(...)
)
rowB = ActionRow(
Button(...),
Button(...)
)
await ctx.send(embed = emb, components = [rowA, rowB])
如果您不想每次都这样做,我建议创建一个辅助函数,它接收一个按钮列表,遍历它们,为每五个按钮创建操作行,然后 returns供您发送到 API.
的操作行列表