因此,我已经提供了有效的帮助,但是如果用户输入的类别无效,我希望它说些什么。如果类别无效,我得到的工作代码没有错误。代码:
@client.command()
async def help(ctx, *, category = None):
if category is not None:
if category == 'mod' or 'moderation' or 'Mod' or 'Moderation':
modhelpembed = discord.Embed(
title="Moderation Help",
timestamp=datetime.datetime.now(),
colour=discord.Color.green()
)
modhelpembed.add_field(name='kick', value="Kicks a member from the server", inline=False)
modhelpembed.add_field(name='ban', value='bans a member from the server', inline=False)
modhelpembed.add_field(name='unban', value='unbans a member from the server', inline=False)
modhelpembed.add_field(name="nuke", value="Nukes a channel :>", inline=False)
modhelpembed.add_field(name='mute', value="Mute a member", inline=False)
modhelpembed.add_field(name="purge", value='purges (deletes) a certain number of messages', inline=False)
await ctx.send(f'{ctx.author.mention}')
await ctx.send(embed=modhelpembed)
elif category == 'fun' or 'Fun':
funembed = discord.Embed(
title="Fun Help",
timestamp=datetime.datetime.now(),
colour=discord.Color.green()
)
funembed.add_field(name='meme', value='shows a meme from r/memes', inline=False)
funembed.add_field(name='waifu', value='shows a waifu (pic or link) from r/waifu', inline=False)
funembed.add_field(name='anime', value='shows a anime (image or link) from r/anime', inline=False)
funembed.add_field(name='spotify', value='Tells you the targeted user listening on', inline=False)
funembed.add_field(name="song", value="Tells you the whats the targeted user listening in Spotify", inline=False)
funembed.add_field(name="album", value="Tells you whats the targeted user album", inline=False)
funembed.add_field(name="timer", value="Sets a Timer for you.", inline=False)
await ctx.send(f'{ctx.author.mention}')
await ctx.send(embed=funembed)
else:
nonembed = discord.Embed(
title="Help list",
timestamp=datetime.datetime.now(),
colour=discord.Color.green(),
description='Category:\nmod\nfun'
)
await ctx.send(f'{ctx.author.mention}')
await ctx.send(embed=nonembed)
它可以工作,但是当我尝试输入无效类别时,它将发送“审核”。
答案 0 :(得分:1)
您的错误来自您的第二个if
语句。您只需将其替换为:
if category == ('mod' or 'moderation' or 'Mod' or 'Moderation'):
if category in ['mod', 'moderation', 'Mod', 'Moderation']:
这是输入无效类别时触发语句的原因:
一个空字符串返回False
(例如""
),一个字符串返回True
(例如"TEST"
)。
如果不放在方括号中,它将把每个or
作为条件(if category == 'mod'
/ if 'mod'
/ if 'moderation'
/ if 'Mod'
/ if 'Moderation'
)。
由于非空字符串返回True,因此当您输入无效的类别时,第二条if
语句将被触发,并为您提供审核帮助消息。
您还可以使用commands.Command
属性进行一些重构:
@client.command(description='Say hi to the bot')
async def hello(ctx):
await ctx.send(f'Hi {ctx.author.mention}')
@client.command(description='Test command')
async def test(ctx):
await ctx.send('TEST')
@client.command()
async def help(ctx, *, category = None):
categories = {
'fun': ['hello',],
'testing': ['test',],
}
if category is None:
desc = '\n'.join(categories.keys())
embed = discord.Embed(
title="Help list",
timestamp=datetime.datetime.now(),
colour=discord.Color.green(),
description=f'Categories:\n{desc}'
)
else:
category = category.lower()
if not category in categories.keys():
await ctx.send('Category name is invalid!')
return
embed = discord.Embed(
title=f"{category.capitalize()} Help",
timestamp=datetime.datetime.now(),
colour=discord.Color.green()
)
for cmd in categories[category]:
cmd = client.get_command(cmd)
embed.add_field(name=cmd.name, value=cmd.description)
await ctx.send(ctx.author.mention, embed=embed)