【问题标题】:Add button components to a message (discord.py)将按钮组件添加到消息(discord.py)
【发布时间】:2021-08-15 17:12:47
【问题描述】:

在 discord 的 API 参考中看到this(消息组件)后,我想知道是否有任何方法可以使用 python 来实现它?

我尝试创建一个 json 数组并将其传递到我的消息中,但无法使其工作。

我也尝试查找 python 的参考,但找不到任何东西。

这是我的代码

components= [
  {
    "type": 2,
    "label": "Clear les kick",
    "style": 4,
    "custom_id": "clear_kick_button"
  }
]
@slash.slash(name="kicked", description="Voir qui a été kick et combien de fois.", guild_ids=guild_id)
async def kicked(ctx):
  await ctx.send("test", components= components)

如果你有任何信息谢谢你如果你分享它。

【问题讨论】:

  • 不要介意命令的名称,只是用了一个未完成的来测试组件。
  • 如果你不使用js,请删除discord.js标签
  • 此外,discordpy 已经为您管理了所有组件。你不需要自己做。看看discordpy docs。不是不和谐的
  • 看起来还没有实现,你可能需要尝试使用requests
  • @FluxedScript,不要对机器人使用请求。它阻止事件循环

标签: python api discord.py


【解决方案1】:

新答案


Discord.py 2.0 允许使用按钮和下拉菜单,尽管它不支持斜杠命令。请改用 Discord.py 2.0,但如果您需要斜杠命令,请查看 discord_slash

升级到 Discord.py 2.0:

窗户:

pip install -U git+https://github.com/Rapptz/discord.py

MacOS 和 Linux:

pip3 install -U git+https://github.com/Rapptz/discord.py

旧答案:

(此答案已过时。)


到目前为止,您可以获得一个名为 discord_components 的库来使用按钮。

要安装这个库,请使用pip install --upgrade discord-components (有时命令是pip3 install --upgrade discord-components)。

要导入 Discord 组件按钮,请使用

from discord_components import DiscordComponents, Button, ButtonStyle, InteractionType

然后只需将此代码添加到机器人的on_ready()

DiscordComponents(bot, change_discord_methods=True)

(确保将bot 替换为您的机器人名称,与您用于@something.command() 的名称相同)

要为消息添加按钮,请执行以下操作:

await ctx.send(type=InteractionType.ChannelMessageWithSource, content="Message Here", components=[Button(style=ButtonStyle.URL, label="Example Invite Button", url="https://google.com"), Button(style=ButtonStyle.blue, label="Default Button", custom_id="button")])

(需要留言)

要在单击按钮时执行某些操作,您可以执行以下操作:

@bot.event
async def on_button_click(interaction):
    if interaction.component.label.startswith("Default Button"):
        await interaction.respond(type=InteractionType.ChannelMessageWithSource, content='Button Clicked')

这种方法甚至可以在重新启动后继续存在!

如果您需要,我为您整理了一个示例:

import discord
from discord.ext import commands
from discord_components import DiscordComponents, Button, ButtonStyle, InteractionType

bot = commands.Bot(command_prefix=prefix, description="Desc", help_command=None)

@bot.event
async def on_ready():
    DiscordComponents(bot, change_discord_methods=True)
    await bot.change_presence(activity=discord.Game(name=f"{prefix}help"))
    print("Bot has successfully logged in as: {}".format(bot.user))
    print("Bot ID: {}\n".format(bot.user.id))

@bot.command()
async def button(ctx):
    await ctx.send(type=InteractionType.ChannelMessageWithSource, content="Message Here", components=[Button(style=ButtonStyle.URL, label="Example Invite Button", url="https://google.com"), Button(style=ButtonStyle.blue, label="Default Button", custom_id="button")])

bot.run("token")

希望对您有所帮助!

提示:如果您希望按钮在一行中,请使用 [[]] 而不是仅 [] 例如:[[btn1, btn2],[btn3, btn4]] 将导致:

[btn 1][btn 2]
[btn 3][btn 4]

额外提示:您也可以将变量设置为按钮然后发送变量

【讨论】:

  • 有关如何“结束”交互(禁用按钮)并检查启动交互的用户是否是按下按钮的更新的更新。
  • @PepeOchoa 它已经存在,您应该查看 API 参考中“bot.wait_for”中的“check”参数。
  • discord.py 已经在最新的只读版本中实现了按钮。我没有看到第三方库的使用只是为了添加 DPY 已有的东西
  • @Shom77 我知道这篇文章是在实施 2.0 时发布的。现在我建议使用 discord_slash,它支持按钮、斜线命令、选择,甚至可以自动同步您现有的命令。无需为发布现在过时的内容而感到羞耻。 (另外,D.py 2.0 只支持按钮,不支持斜线或下拉菜单)
  • 我根本不是想羞辱你,只是我现在看不到第三方库的目的。此外,D.py 2.0 允许下拉菜单,我今天将它们用于我的机器人
【解决方案2】:

这是来自discord.pyAlpha 版本示例,因为它尚未实现:

import discord

class Counter(discord.ui.View):
    @discord.ui.button(label='0', style=discord.ButtonStyle.red)
    async def counter(self, button: discord.ui.Button, interaction: discord.Interaction):
        number = int(button.label)
        button.label = str(number + 1)
        if number + 1 >= 5:
            button.style = discord.ButtonStyle.green

        await interaction.message.edit(view=self)

view = Counter()
await ctx.send('Press to increment', view=view)

【讨论】:

    【解决方案3】:

    想说这个页面3个月了,discord_components变化很大,Interaction Type已经不存在了,请关注这个

    https://gitlab.com/discord.py-components/discord.py-components/-/tree/master/examples

    【讨论】:

      【解决方案4】:

      截至当前最新版本v1.7.2Discord.py Wrapper by Rapptz 中尚未实现按钮。


      不过,您可以查看 here 下一次更新的计划以及开发状态。

      与此同时,您必须向 Discord API 提出自己的请求,或者搜索非官方库。

      【讨论】:

      • 这个答案现在已经过时了,因为 Discord.py 2.0 现在可用。查看我关于如何安装它的答案:stackoverflow.com/questions/67722188/…
      • 不太正确。确实,您可以获得按钮的站点包,但是 Discord.py 2.0.0 从未正式发布,遗憾的是,Discord.py 不再得到维护。看看wiki-page
      • 找到了如何安装它。 pip install -U git+github.com/Rapptz/discord.py Source 是 Discord 服务器的官方公告,尽管它不是官方发布.... YET
      【解决方案5】:

      我假设你的装饰者正在使用discord-py-slash-command

      如果这个假设是正确的,您就不能在本地使用带有该库的组件。 discord-py-components 在我们等待 discord.py 2.0 发布时添加了该功能

      【讨论】:

        【解决方案6】:

        这是我编写的一小段代码,我也是这个 Discord 组件的新手,但希望这能帮助您解决问题。

        await ctx.channel.send("Context",components=[Button(style=ButtonStyle.blue, label="Test")]) #Blue button with button label of "Test"
            res = await self.client.wait_for("button_click") #Wait for button to be clicked
            await res.respond(type=InteractionType.ChannelMessageWithSource, content=f'Button Clicked') #Responds to the button click by printing out a message only user can see #In our case, its "Button Clicked"
        

        还要记得先导入组件。

        from discord_components import DiscordComponents, Button, ButtonStyle, InteractionType
        

        【讨论】:

          猜你喜欢
          • 2014-02-23
          • 2021-03-10
          • 2021-03-06
          • 2022-01-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-09-11
          相关资源
          最近更新 更多