【问题标题】:Discord.py Buttons and drop down menu's, not getting the correct librariesDiscord.py 按钮和下拉菜单,没有得到正确的库
【发布时间】:2021-10-04 05:13:11
【问题描述】:

修复:添加 DiscordComponents(bot)

我正在尝试获取允许用户进行选择的按钮或下拉菜单。但是,我无法让它工作,因为这两种方法最终都会给我一个组件问题。

错误信息是: 第 134 行,以 rps 为单位 m = 等待 ctx.send( TypeError: send() 得到了一个意外的关键字参数“组件”

我已经 pip 安装了 discord_components 并导入了它。我对错误消息的来源感到困惑。

@bot.command(name = "rps")
async def rps(ctx):
    ch1 = ["Rock","Scissors","Paper"]
    comp = random.choice(ch1)

    yet = discord.Embed(title = f"{ctx.author.display_name}'s RPS game", description = "You have yet to select anything")

    win = discord.Embed(title = f"{ctx.author.display_name} Won!", descrption = "Congrats! You won.")

    out = discord.Embed(title = f"{ctx.author.display_name} was too slow.", descrption = "Okay then you were just too slow for me. ")

    lost = discord.Embed(title = f"{ctx.author.display_name} lost", descrption = "Aw darn. Unfortunatly you lost this one.")

    tie = discord.Embed(title = f"{ctx.author.display_name} tied with the bot!", descrption = "Well thats interesting. It was a tie.")


    m = await ctx.send(
        embed=yet,
        components=[Select(placeholder="Select 1", options=[SelectOption(label="Rock", value="Rock"), SelectOption(label="Paper", value="Paper"), SelectOption(label="Scissors", value="Scissors")])]
        ,
    )

    interaction = await bot.wait_for("select_option", check = lambda i: i.component[0].value == "A")
    await interaction.respond(content = f"{interaction.component[0].label} selected!")

我导入的内容以及我的机器人是如何定义的:

import os, csv, random, discord, asyncio, pandas, requests, json, traceback
from discord.ext import commands
from dotenv import load_dotenv
from discord_components import DiscordComponents, Button, Select, SelectOption

load_dotenv()
botToken = os.getenv("DiscBotToken")

【问题讨论】:

  • 您是否也可以发送代码以说明您定义机器人的位置以及如何和您的导入
  • 更新了,希望我给了你正确的信息:)
  • 你在代码中的任何地方都做了DiscordComponents(bot)吗?
  • 天哪,这立即解决了我的问题。为什么答案那么容易,我没有弄清楚。我真的很感激!

标签: python python-3.x discord discord.py


【解决方案1】:

嘿,我建议您使用官方 API WRAPPER,而不是像 discord_componentsnextcord 等 3rd 方库/依赖项... 如果您想查看 discord.py v2(官方版本)的视图,请转到此处 --> https://github.com/Rapptz/discord.py/tree/45d498c1b76deaf3b394d17ccf56112fa691d160/examples/views 如果您对代码有疑问或问题,请转到官方 discord 服务器 --> discord.gg /dpy 并在#testing 频道中输入?tester 以访问v2 问题频道

【讨论】:

  • discord.py 已存档,并且从未正式发布。
【解决方案2】:

我帮不了你,但我可以给你一个工作代码。

import discord
from discord.ext import commands
from discord_components import DiscordComponents, Select, SelectOption, Button,ButtonStyle
from discord.utils import get

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

@bot.event
async def on_ready():
DiscordComponents(bot)
print('Connected')

@bot.command()
async def button(ctx):
    embed = discord.Embed(title='Test',description='Test text')

await ctx.send(embed=embed, components=[Button(label='Test', custom_id="test-id", style=ButtonStyle.red)])
interaction = await bot.wait_for(
"button_click", check=lambda inter: inter.custom_id == "test-id")

@bot.event
async def on_button_click(interaction):
    await interaction.respond(type=6)
        await interaction.author.send("Click")

还有选择菜单

import discord
from discord.ext import commands
from discord_components import DiscordComponents, Select, SelectOption, Button, ButtonStyle
from discord.utils import get

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

@bot.event
async def on_ready():
    DiscordComponents(bot)
    print('Connected')

@bot.command()
async def sm(ctx):
    if ctx.author.id == 670325098598629377 or 426376741028888576:
        await ctx.send(
    '>>> 'Text',
    components = [
    Select(
        placeholder = 'SelectMenu',
        options = [
            SelectOption(label="SelectMenu1", value="value1"),
            SelectOption(label="SelectMenu2", value="value2"),
            SelectOption(label="SelectMenu3", value="value3"),
            SelectOption(label = "SelectMenu4", value = "value4"),
            SelectOption(label="SelectMenu5", value="value5"),
            SelectOption(label="SelectMenu6", value="value6"),
            SelectOption(label = "SelectMenu7", value = "value7"),
            SelectOption(label = "SelectMenu8", value = "value8")
            ])])

@bot.event
async def on_select_option(interaction):
    if interaction.message.id == 891587821368905728: #Message id(not obligatory)
        await interaction.respond(type=6)
        if interaction.values[0] == "value1":
            await interaction.author.send("Menu 1")
        elif interaction.values[0] == "value2":
            await interaction.author.send("Menu 2")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-16
    • 2020-04-28
    • 2012-02-20
    • 2021-10-18
    • 1970-01-01
    • 2014-10-04
    • 2013-07-06
    相关资源
    最近更新 更多