python - Discord.py 按钮 - 向成员添加角色返回 NoneType 错误
问题描述
我正在尝试使用带有 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 按钮实现角色更改的示例代码?因为我似乎无法让它工作
谢谢 :)
解决方案
首先,确保您也导入了 Discord。
intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)
然后你可以简单地让用户使用这个:
user = client.get_user(INT)
推荐阅读
- javascript - 慢反弹Javascript
- json - 具有自定义键类型的 Data.Map.Strict.Map 的 Aeson 编码导致数组数组而不是对象
- javascript - 如何从 Firebase 函数 forEach 循环返回单个变量?
- .net - 如何计算 .net core Webapi 请求次数;
- typescript - TSLint:相对之前的绝对导入
- javascript - 带有 Babel 7 的 NuxtJS:在构建文件中仍然有扩展运算符
- c++ - 原子操作传播/可见性(原子负载与原子 RMW 负载)
- sql - SQL Server 2008 按唯一值获取最后一行
- ruby-on-rails - 从 URL 请求 Rails 模型的一部分
- javafx - 可以(Javafx)我使用 ComboBox 项目来显示页面吗?我需要将filmCombo组合框中的每个选项链接到一个fxml文件