discord.py - 提及用户的 discord.py 随机消息
问题描述
py 最近,我希望机器人发送提及用户的随机消息,但它给了我这个错误:
discord.ext.commands.errors.CommandInvokeError:命令引发异常:AttributeError:“NoneType”对象没有属性“提及”
这是代码:
@client.command()
async def randomroman(ctx, *,member: discord.Member=None):
mention = member.mention
variable=[
f'{mention} ama tanto roman!',
f'{mention} odia tanto roman!',
f'{mention} ama roman!',
f'{mention} odia roman!'
]
await ctx.message.channel.send(random.choice(variable))
解决方案
所以看起来你已经设置了一个默认值,因此你应该在尝试发送消息之前检查是否提到了一个成员。这是您可以使用的两段不同的代码。
@client.command()
async def randomroman(ctx, member: discord.Member=None):
if not member:
# We dont have anyone to mention so tell the user
await ctx.send("You need to mention someone for me to mention!")
return
variable=[
f'{member.mention} ama tanto roman!',
f'{member.mention} odia tanto roman!',
f'{member.mention} ama roman!',
f'{member.mention} odia roman!'
]
await ctx.send(random.choice(variable))
您也可以简单地使用ctx.send()
您可以做的另一件事是,如果他们不调用命令提及任何人,则让它提及作者,就像这样
@client.command()
async def randomroman(ctx, member: discord.Member=None):
member = member or ctx.author
variable=[
f'{member.mention} ama tanto roman!',
f'{member.mention} odia tanto roman!',
f'{member.mention} ama roman!',
f'{member.mention} odia roman!'
]
await ctx.send(random.choice(variable))
在这种情况下,这两种方法都会起作用。!randomroman
并!randomroman @user
会提到一个用户。
希望这可以帮助!
推荐阅读
- c++ - 将接口指针数组静态声明到专用类
- reactjs - 如何将 react-simple-keyboard 与 react-table 一起使用?
- javascript - 修改函数中的对象时出现意外结果
- html - 根据 HTML 表格中的条件更改列颜色
- c++ - 使用二进制文件的用户名和登录系统
- android - 无法获取 note.dit-inc.us/down.html android studio
- html - 将 ':focus' 样式应用于输入组
- c# - 即使当前视图和其他视图已显示,也无法找到视图
- java - bean 构造函数中的 Java 值注入
- python - python请求中找不到404的解决方案吗?