python - 如何检查用户是否编写了命令?
问题描述
我有这个
await ctx.send("Which inventory do you want to access?")
await ctx.send("Mining, Collecting, Farming, Fishing or Fighting?")
def check(user):
if user == ctx.author:
# Idk what to do here
pass
type_check = await self.bot.wait_for('message', check=check)
if type_check.content.lower() == "mining":
await ctx.send("You chose Mining!")
if type_check.content.lower() == "collecting":
await ctx.send("You chose Collecting!")
if type_check.content.lower() == "farming":
await ctx.send("You chose Farming!")
if type_check.content.lower() == "fishing":
await ctx.send("You chose Fishing!")
if type_check.content.lower() == "fighting":
await ctx.send("You chose Fighting!")
我需要检查用户是否写了消息,如果他们写了,它会等待 ctx send the thing
解决方案
检查函数必须返回一个布尔值,传递的参数也是一个discord.Message
对象而不是用户
def check(message):
if message.author == ctx.author:
return True
或者
# This is a better way
def check(message):
return message.author == ctx.author
顺便说一句,对于那些 if 语句来说,一个更好的解决方案是检查它们是否在列表中:
inv_type = type_check.content.lower()
if inv_type in ['mining', 'collecting', 'farming', 'fishing', 'fighting']:
await ctx.send(f"You chose {inv_type}!")
推荐阅读
- laravel - laravel 存储如何检查存储的文件
- sql - 更新列中的值
- javascript - Cheerio js成对获取元素
- flutter - Flutter 中未显示 BottomNavigationBar 标题
- git - git-flow:如何处理从更改到已发布分支的版本号
- python - Visual Studio Code 智能不显示包装器的参数
- python - 找到点之间的最短距离
- java - 我有从主机收集的数据(小时/日期),但我无法将它们放入文本(场景生成器)
- arrays - 用Scala中的另一个数组中的元素替换一个数组中的元素
- android-camera-intent - 相机意图中的模糊图像