【问题标题】:Commands with buttons - Discord.py带有按钮的命令 - Discord.py
【发布时间】:2021-09-26 01:16:40
【问题描述】:

在我的 Discord 机器人中,我发出了带有反应的帮助命令。但最近我听说了“按钮”。许多开发人员建议我使用按钮而不是反应。有人知道如何修改我的代码以使消息保持不变吗? 顺便说一下,这是我当前的代码:

@client.command()
async def help(ctx):
    contents = [
    discord.Embed(title='Utilities commands',description=
f'''
text 1    
''',colour=0xF00C0C),
    discord.Embed(title='Moderation commands',description=
f'''
text 2
''',colour=0xF00C0C),
    discord.Embed(title='Fun commands',description=
f'''
text 3
''',colour=0xF00C0C),
    discord.Embed(title='Coding commands',description=
f'''
text 4
''',colour=0xF00C0C),
    discord.Embed(title='Bot\'s info | ID: 856643485340139580',description=
'''
text 5
''',colour=0xF00C0C)
]
    pages = 5
    cur_page = 1
    message = await ctx.send(embed=contents[cur_page - 1])

    await message.add_reaction("◀️")
    await message.add_reaction("▶️")

    def check(reaction, user):
        return user == ctx.author and str(reaction.emoji) in ["◀️", "▶️"]

    while True:
        try:
            reaction, user = await client.wait_for("reaction_add", check=check)

            if str(reaction.emoji) == "▶️" and cur_page != pages:
                cur_page += 1
                await message.edit(embed=contents[cur_page - 1])
                await message.remove_reaction(reaction, user)

            elif str(reaction.emoji) == "◀️" and cur_page > 1:
                cur_page -= 1
                await message.edit(embed=contents[cur_page - 1])
                await message.remove_reaction(reaction, user)

            else:
                await message.remove_reaction(reaction, user)

        except:
            break

感谢任何形式的帮助!

【问题讨论】:

    标签: python discord discord.py bots


    【解决方案1】:

    discord.py 尚未实现组件“按钮”,这是在 v2.0 中实现的。
    他们可以通过在官方 discord 服务器上注册为测试人员来进行 beta 测试。

    另外,还有一个用于组件的 3rd 方库,但根本不建议这样做,因为它具有破坏性功能和猴子补丁,其中大部分将在 discord.py 的未来更新中停止工作。

    我建议在 2.0 正式发布之前推迟按钮,除非你觉得自己有足够的能力对 2.0 进行 beta 测试。

    https://discord.gg/dpy 是 Discord 服务器。

    【讨论】:

    • 感谢您的建议!我如何在他们的官方 Discord 服务器上注册?因为我很想测试它们?
    • 这是服务器邀请discord.gg/dpy,要获得实际的测试员角色,您必须在#testing 频道中使用?tester 命令,新频道将是可用称为 #beta-testing。另请阅读 #guild-news 中发送的最后一条消息
    • 感谢兄弟的链接!
    • 这个文档太好了
    【解决方案2】:

    可以创建一个 on_button_click 监听器。

    下面的代码给出了一个如何让它工作的例子

    import discord
    import settings
    from discord_components import DiscordComponents, Button
    
    class MyClient(discord.Client):
        async def on_ready(self):
            buttons = [Button(label="button 1", custom_id="1"), Button(label="button 2", custom_id="2"),
                       Button(label="button 3", custom_id="3")]
    
            channel = self.get_channel(settings.DISCORD_ROLEBOT_SETTINGS_CHANNEL)
            await channel.send("test", components=buttons)
    
        async def on_button_click(self, interaction):
            await interaction.respond(content=f"you clicked button {interaction.component.custom_id}")
    
    
    if __name__ == "__main__":
        client = MyClient()
        DiscordComponents(client)
        client.run(settings.DISCORD_TOKEN)
    
    

    【讨论】:

    • 感谢分享这个例子!
    猜你喜欢
    • 1970-01-01
    • 2013-08-11
    • 1970-01-01
    • 2020-11-25
    • 2020-11-24
    • 2021-04-20
    • 1970-01-01
    • 2013-04-19
    • 2019-06-02
    相关资源
    最近更新 更多