Discord.py按钮向成员添加角色返回非类型错误

2024-05-01 21:52:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用discord.py模块的discord组件包制作一个按钮

我有以下代码:

@client.command()
async def button(ctx):
    await ctx.channel.send(
        "This is a button test",
        components = [
            Button(style=ButtonStyle.blue, label="Button 1")
        ]
    )


    res = await client.wait_for("button_click")
    if res.channel == ctx.channel:
        member = await res.guild.get_member(res.user.id)
        await member.add_role(853692936465940501)
        await res.respond(
            type=InteractionType.ChannelMessageWithSource,
            content=f"{res.component.label} has been clicked! This is button 1"
        )

但我似乎有问题,出错了

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: object NoneType can't be used in 'await' expression

为什么guild.get_成员返回非类型? 有没有人可能有一些关于如何使用discord.py按钮实现角色更改的示例代码?因为我好像没法让它工作

谢谢:)


Tags: 代码pyclientischannelbuttonresawait
2条回答

首先,确保你也导入了Discord

intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)

然后,您可以简单地让用户使用以下内容: user = client.get_user(INT)

@client.command()
async def button(ctx):
    await ctx.channel.send(
        "This is a button test",
        components = [
            Button(style=ButtonStyle.blue, label="Button 1")
        ]
    )


    res = await client.wait_for("button_click")
    if res.channel == ctx.channel:
        role = discord.utils.get(ctx.guild.roles, id = 853692936465940501)
        await res.author.add_roles(role)
        await res.respond(
            type=4,
            content=f"{res.component.label} has been clicked! This is button 1"
        )

相关问题 更多 >