首页 > 解决方案 > 如何检查用户是否编写了命令?

问题描述

我有这个

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

标签: pythondiscord.py

解决方案


检查函数必须返回一个布尔值,传递的参数也是一个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}!")

推荐阅读