【问题标题】:Discord.py buttons - Adding role to member returns NoneType errorDiscord.py 按钮 - 向成员添加角色返回 NoneType 错误
【发布时间】:2021-10-20 19:01:50
【问题描述】:

我正在尝试使用带有 discord.py 模块的 discord-components 包制作一个按钮。

我有以下代码:

@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_member 返回 NoneType? 是否有人可能有一些如何使用 discord.py 按钮实现角色更改的示例代码?因为我似乎无法让它工作

谢谢:)

【问题讨论】:

  • get_member 是普通函数,不是协程。你不需要await它。

标签: python discord.py


【解决方案1】:

首先,确保你也导入了 Discord。

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

然后你可以简单地让用户使用这个: user = client.get_user(INT)

【讨论】:

  • @itzFlubby 你可以再看看他说的,guild.member 正在返回 NoneType ,这是另一种解决方案。
  • 他在问为什么“guild.get_member return[s] a NoneType?”。我在你的帖子中没有看到答案,但是 nvm
  • @itzFlubby 这就是为什么我说要先启用意图!然后如果不起作用,请尝试将 guild.get_member 替换为 client.get_user。就是这样!
【解决方案2】:
@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"
        )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-12
    • 2021-07-26
    • 2021-03-26
    • 1970-01-01
    • 1970-01-01
    • 2020-12-19
    • 1970-01-01
    相关资源
    最近更新 更多