• <legend id='4Fvhk'><style id='4Fvhk'><dir id='4Fvhk'><q id='4Fvhk'></q></dir></style></legend>

  • <i id='4Fvhk'><tr id='4Fvhk'><dt id='4Fvhk'><q id='4Fvhk'><span id='4Fvhk'><b id='4Fvhk'><form id='4Fvhk'><ins id='4Fvhk'></ins><ul id='4Fvhk'></ul><sub id='4Fvhk'></sub></form><legend id='4Fvhk'></legend><bdo id='4Fvhk'><pre id='4Fvhk'><center id='4Fvhk'></center></pre></bdo></b><th id='4Fvhk'></th></span></q></dt></tr></i><div id='4Fvhk'><tfoot id='4Fvhk'></tfoot><dl id='4Fvhk'><fieldset id='4Fvhk'></fieldset></dl></div>

      • <bdo id='4Fvhk'></bdo><ul id='4Fvhk'></ul>
      1. <small id='4Fvhk'></small><noframes id='4Fvhk'>

        <tfoot id='4Fvhk'></tfoot>

        将按钮组件添加到消息(discord.py)

        时间:2023-09-11
        <tfoot id='QWQte'></tfoot>
          <tbody id='QWQte'></tbody>
          <bdo id='QWQte'></bdo><ul id='QWQte'></ul>
              <legend id='QWQte'><style id='QWQte'><dir id='QWQte'><q id='QWQte'></q></dir></style></legend>

                <i id='QWQte'><tr id='QWQte'><dt id='QWQte'><q id='QWQte'><span id='QWQte'><b id='QWQte'><form id='QWQte'><ins id='QWQte'></ins><ul id='QWQte'></ul><sub id='QWQte'></sub></form><legend id='QWQte'></legend><bdo id='QWQte'><pre id='QWQte'><center id='QWQte'></center></pre></bdo></b><th id='QWQte'></th></span></q></dt></tr></i><div id='QWQte'><tfoot id='QWQte'></tfoot><dl id='QWQte'><fieldset id='QWQte'></fieldset></dl></div>

                <small id='QWQte'></small><noframes id='QWQte'>

                1. 本文介绍了将按钮组件添加到消息(discord.py)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

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

                  I was wondering after seeing this (message components) on discord's API reference if there was any way to implement it using python ?

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

                  I tried making a json array and passing it in my message but couldn't make it work.

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

                  I also tried looking on the reference for python but couldn't find anything.

                  这是我的代码

                  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)
                  

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

                  If you have any informations thank you if you share it.

                  推荐答案

                  新答案


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

                  升级到 Discord.py 2.0:

                  To upgrade to Discord.py 2.0:

                  窗户:

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

                  MacOS 和 Linux:

                  MacOS and Linux:

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

                  旧答案:

                  (此答案已过时.)


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

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

                  Old Answer:

                  (This Answer is OUTDATED.)


                  As of right now, you can get a library called discord_components to use buttons.

                  To install this library, use pip install --upgrade discord-components (Sometimes the command would be pip3 install --upgrade discord-components).

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

                  To import Discord Component Buttons, use

                  from discord_components import DiscordComponents, Button, ButtonStyle, InteractionType
                  

                  然后只需将这段代码添加到机器人的 on_ready() 中:

                  Then just add this code in the bot's on_ready():

                  DiscordComponents(bot, change_discord_methods=True)
                  

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

                  (Make sure to replace bot with the name of your bot, the same one you use for @something.command())

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

                  To add a button to a message, do this:

                  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")])
                  

                  (需要留言)

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

                  To do something when the button is clicked, you can do something like this:

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

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

                  This method even survives reboots!

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

                  Here's an example I put together for you if you need it:

                  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: {}
                  ".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]] 将导致:

                  Tip: If you want the buttons to be in one row, use [[]] instead of just [] for Example: [[btn1, btn2],[btn3, btn4]] will result in:

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

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

                  Extra Tip: You can also set a variable as a button then send the variable

                  这篇关于将按钮组件添加到消息(discord.py)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:on_message() 和@bot.command 问题 下一篇:让 VoiceChannel.members 和 Guild.members 返回完整列表的

                  相关文章

                  最新文章

                2. <legend id='sVAG6'><style id='sVAG6'><dir id='sVAG6'><q id='sVAG6'></q></dir></style></legend>
                3. <tfoot id='sVAG6'></tfoot>

                    <bdo id='sVAG6'></bdo><ul id='sVAG6'></ul>

                    <small id='sVAG6'></small><noframes id='sVAG6'>

                      <i id='sVAG6'><tr id='sVAG6'><dt id='sVAG6'><q id='sVAG6'><span id='sVAG6'><b id='sVAG6'><form id='sVAG6'><ins id='sVAG6'></ins><ul id='sVAG6'></ul><sub id='sVAG6'></sub></form><legend id='sVAG6'></legend><bdo id='sVAG6'><pre id='sVAG6'><center id='sVAG6'></center></pre></bdo></b><th id='sVAG6'></th></span></q></dt></tr></i><div id='sVAG6'><tfoot id='sVAG6'></tfoot><dl id='sVAG6'><fieldset id='sVAG6'></fieldset></dl></div>