我刚刚开始尝试学习如何在discord.py中进行编码。但是,我不确定如何在命令后检查用户是否提供了意见。例如,如果用户输入!kick,但没有在其后标记用户,则应该给他们一条错误消息,例如“请标记用户”。
所以我的问题是,如何在使用命令时检查用户是否标记了另一个用户,如果没有,请给他们一条错误消息。
这是我的代码:
#PyBot by RYAN#2302
import discord
from discord.ext import commands
from discord.ext.commands import Bot
import asyncio
import chalk
bot = commands.Bot(command_prefix='!')
@bot.event
async def on_ready():
print ("Ready when you are xd")
print ("I am running on " + bot.user.name)
print ("With the ID: " + bot.user.id)
@bot.command(pass_context=True)
async def ping(ctx):
await bot.say(":ping_pong: Ping! :ping_pong:")
print ("!ping")
@bot.command(pass_context=True)
async def info(ctx, user: discord.Member):
await bot.say("The users name is: {}".format(user.name))
await bot.say("The users ID is: {}".format(user.id))
await bot.say("The users status is: {}".format(user.status))
await bot.say("The users highest role is: {}".format(user.top_role))
await bot.say("The user joined at: {}".format(user.joined_at))
print ("!info")
@bot.command(pass_context=True)
async def kick(ctx, user: discord.Member):
await bot.say(":boot: Cya, {}. Ya loser!".format(user.name))
await bot.kick(user)
print ("!kick on {}".format(user.name))
任何帮助将不胜感激,谢谢!
给user
参数提供默认值None
,然后检查是否使用了提交值
@bot.command(pass_context=True)
async def kick(ctx, user: discord.Member = None):
if user:
await bot.say(":boot: Cya, {}. Ya loser!".format(user.name))
await bot.kick(user)
print ("!kick on {}".format(user.name))
else:
await bot.say("Please tag a user to kick")
所有Member
对象都是真实的,因此唯一的if user:
失败将是没有用户提供。如果输入内容无法与用户匹配,则该命令将不执行任何操作,除非我们也指定错误侦听器,否则该操作将自动失败
@kick.error
async def kick_error(ctx, error):
if isinstance(error, discord.ext.commands.BadArgument):
await bot.say('Could not recognize user')
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句