Discord Python 子命令不起作用?
Posted
技术标签:
【中文标题】Discord Python 子命令不起作用?【英文标题】:Discord Python Subcommands aren't working? 【发布时间】:2021-04-28 09:54:12 【问题描述】:我对这些子命令尝试了许多可能的解决方案,但我找不到问题。
@client.command(aliases=['commands', 'Commands', 'Help'])
async def help(ctx, command=None):
if command == None:
author = ctx.message.author
embed = discord.Embed(colour=discord.Colour.magenta())
embed.set_author(name='Help Command')
embed.add_field(name='Here are all the commands!',
value='If you want to know how to use a command, do ``;help <command name>`` *--coming soon--*',
inline=False)
embed.add_field(name='Miscellaneous',
value='``8ball``, ``flip``, ``userinfo``, ``drip``, ``annoy``, ``hug``, ``slap``, ``cry`` ``pat``, ``punch``, ``invite``',
inline=False)
embed.add_field(name='\nInformation', value='``userinfo``, ``avatar``, ``rules``', inline=False)
embed.add_field(name='\nModeration',
value='``kick``, ``ban``, ``unban``, ``mute``, ``unmute``, ``report``, ``clear``', inline=False)
await ctx.author.send(embed=embed)
await ctx.send(f'***✅ author.mention Check your DMs!***')
if command == '8ball':
embed = discord.Embed(colour=discord.Colour.magenta())
embed.add_field(name='8ball command', value='A random yes/no answer. ``;8ball <question>``')
embed.thumbnail(url='https://cdn.discordapp.com/attachments/801233629954048040/802533814282879026/e39-min.png')
embed.set_footer(icon_url=ctx.author.avatar_url, text=f'Requested by ctx.author.name')
await ctx.author.send(embed=embed)
return
if command == 'flip':
embed = discord.Embed(colour=discord.Colour.magenta())
embed.add_field(name='Flip command', value='Flips a coin. ``;flip <question>``')
embed.thumbnail(url='https://cdn.discordapp.com/attachments/801233629954048040/802533814282879026/e39-min.png')
embed.set_footer(icon_url=ctx.author.avatar_url, text=f'Requested by ctx.author.name')
await ctx.author.send(embed=embed)
return
if command == 'userinfo':
embed = discord.Embed(colour=discord.Colour.magenta())
embed.add_field(name='Userinfo command', value='Displays user information. ``;userinfo``')
embed.thumbnail(url='https://cdn.discordapp.com/attachments/801233629954048040/802533814282879026/e39-min.png')
embed.set_footer(icon_url=ctx.author.avatar_url, text=f'Requested by ctx.author.name')
await ctx.author.send(embed=embed)
return
if command == 'drip':
embed = discord.Embed(colour=discord.Colour.magenta())
embed.add_field(name='Drip command', value='Shows you the real drip. ``;drip``')
embed.thumbnail(url='https://cdn.discordapp.com/attachments/801233629954048040/802533814282879026/e39-min.png')
embed.set_footer(icon_url=ctx.author.avatar_url, text=f'Requested by ctx.author.name')
await ctx.author.send(embed=embed)
return
if command == 'annoy':
embed = discord.Embed(colour=discord.Colour.magenta())
embed.add_field(name='Annoy command', value='Pings a user 10 times. ``;annoy user``')
embed.thumbnail(url='https://cdn.discordapp.com/attachments/801233629954048040/802533814282879026/e39-min.png')
embed.set_footer(icon_url=ctx.author.avatar_url, text=f'Requested by ctx.author.name')
await ctx.author.send(embed=embed)
return
if command == 'hug':
embed = discord.Embed(colour=discord.Colour.magenta())
embed.add_field(name='Hug command', value='Hugs the user. ``;hug <user>``')
embed.thumbnail(url='https://cdn.discordapp.com/attachments/801233629954048040/802533814282879026/e39-min.png')
embed.set_footer(icon_url=ctx.author.avatar_url, text=f'Requested by ctx.author.name')
await ctx.author.send(embed=embed)
return
if command == 'slap':
embed = discord.Embed(colour=discord.Colour.magenta())
embed.add_field(name='Slap command', value='Slaps the user. ``;slap <user>``')
embed.thumbnail(url='https://cdn.discordapp.com/attachments/801233629954048040/802533814282879026/e39-min.png')
embed.set_footer(icon_url=ctx.author.avatar_url, text=f'Requested by ctx.author.name')
await ctx.author.send(embed=embed)
return
if command == 'cry':
embed = discord.Embed(colour=discord.Colour.magenta())
embed.add_field(name='Cry command', value='You cry. ``;cry``')
embed.thumbnail(url='https://cdn.discordapp.com/attachments/801233629954048040/802533814282879026/e39-min.png')
embed.set_footer(icon_url=ctx.author.avatar_url, text=f'Requested by ctx.author.name')
await ctx.author.send(embed=embed)
return
if command == 'pat':
embed = discord.Embed(colour=discord.Colour.magenta())
embed.add_field(name='Pat command', value='Pats user``;pat <user>``')
embed.thumbnail(url='https://cdn.discordapp.com/attachments/801233629954048040/802533814282879026/e39-min.png')
embed.set_footer(icon_url=ctx.author.avatar_url, text=f'Requested by ctx.author.name')
await ctx.author.send(embed=embed)
return
if command == 'punch':
embed = discord.Embed(colour=discord.Colour.magenta())
embed.add_field(name='Punch command', value='Punches user. ``;punch <user>``')
embed.thumbnail(url='https://cdn.discordapp.com/attachments/801233629954048040/802533814282879026/e39-min.png')
embed.set_footer(icon_url=ctx.author.avatar_url, text=f'Requested by ctx.author.name')
await ctx.author.send(embed=embed)
return
if command == 'invite':
embed = discord.Embed(colour=discord.Colour.magenta())
embed.add_field(name='Invite command', value='Gives you the permanent invite link. ``;invite``')
embed.thumbnail(url='https://cdn.discordapp.com/attachments/801233629954048040/802533814282879026/e39-min.png')
embed.set_footer(icon_url=ctx.author.avatar_url, text=f'Requested by ctx.author.name')
await ctx.author.send(embed=embed)
return
if command == 'userinfo':
embed = discord.Embed(colour=discord.Colour.magenta())
embed.add_field(name='Userinfo command', value='Shows information about user``;userinfo <user>``')
embed.thumbnail(url='https://cdn.discordapp.com/attachments/801233629954048040/802533814282879026/e39-min.png')
embed.set_footer(icon_url=ctx.author.avatar_url, text=f'Requested by ctx.author.name')
await ctx.author.send(embed=embed)
return
if command == 'avatar':
embed = discord.Embed(colour=discord.Colour.magenta())
embed.add_field(name='Avatar command', value='Shows users avatar. ``;avatar <user>``')
embed.thumbnail(url='https://cdn.discordapp.com/attachments/801233629954048040/802533814282879026/e39-min.png')
embed.set_footer(icon_url=ctx.author.avatar_url, text=f'Requested by ctx.author.name')
await ctx.author.send(embed=embed)
return
if command == 'rules':
embed = discord.Embed(colour=discord.Colour.magenta())
embed.add_field(name='Rules command', value='Shows you server rules. ``;rules``')
embed.thumbnail(url='https://cdn.discordapp.com/attachments/801233629954048040/802533814282879026/e39-min.png')
embed.set_footer(icon_url=ctx.author.avatar_url, text=f'Requested by ctx.author.name')
await ctx.author.send(embed=embed)
return
if command == 'kick':
embed = discord.Embed(colour=discord.Colour.magenta())
embed.add_field(name='Kick command', value='Kicks mentioned user. ``;kick <user>``')
embed.thumbnail(url='https://cdn.discordapp.com/attachments/801233629954048040/802533814282879026/e39-min.png')
embed.set_footer(icon_url=ctx.author.avatar_url, text=f'Requested by ctx.author.name')
await ctx.author.send(embed=embed)
return
if command == 'ban':
embed = discord.Embed(colour=discord.Colour.magenta())
embed.add_field(name='Ban command', value='Bans mentioned user. ``;ban <user>``')
embed.thumbnail(url='https://cdn.discordapp.com/attachments/801233629954048040/802533814282879026/e39-min.png')
embed.set_footer(icon_url=ctx.author.avatar_url, text=f'Requested by ctx.author.name')
await ctx.author.send(embed=embed)
return
if command == 'unban':
embed = discord.Embed(colour=discord.Colour.magenta())
embed.add_field(name='Unban command', value='Unbans user. ``;unban <user tag(name#0001)>``')
embed.thumbnail(url='https://cdn.discordapp.com/attachments/801233629954048040/802533814282879026/e39-min.png')
embed.set_footer(icon_url=ctx.author.avatar_url, text=f'Requested by ctx.author.name')
await ctx.author.send(embed=embed)
return
if command == 'mute':
embed = discord.Embed(colour=discord.Colour.magenta())
embed.add_field(name='Mute command', value='Mutes mentioned user. ``;mute <user>``')
embed.thumbnail(url='https://cdn.discordapp.com/attachments/801233629954048040/802533814282879026/e39-min.png')
embed.set_footer(icon_url=ctx.author.avatar_url, text=f'Requested by ctx.author.name')
await ctx.author.send(embed=embed)
return
if command == 'unmute':
embed = discord.Embed(colour=discord.Colour.magenta())
embed.add_field(name='Unmute command', value='Unmutes mentioned user. ``;unmute <user>``')
embed.thumbnail(url='https://cdn.discordapp.com/attachments/801233629954048040/802533814282879026/e39-min.png')
embed.set_footer(icon_url=ctx.author.avatar_url, text=f'Requested by ctx.author.name')
await ctx.author.send(embed=embed)
return
if command == 'clear':
embed = discord.Embed(colour=discord.Colour.magenta())
embed.add_field(name='Clear command', value='Clears recent messages. ``;clear <amount>``')
embed.thumbnail(url='https://cdn.discordapp.com/attachments/801233629954048040/802533814282879026/e39-min.png')
embed.set_footer(icon_url=ctx.author.avatar_url, text=f'Requested by ctx.author.name')
await ctx.author.send(embed=embed)
return
if command == 'report':
embed = discord.Embed(colour=discord.Colour.magenta())
embed.add_field(name='Report command', value='Reports user to staff team. ``;report <grade (mild / severe / urgent)> <user> <reason>``')
embed.thumbnail(url='https://cdn.discordapp.com/attachments/801233629954048040/802533814282879026/e39-min.png')
embed.set_footer(icon_url=ctx.author.avatar_url, text=f'Requested by ctx.author.name')
await ctx.author.send(embed=embed)
return
有什么解决办法吗?我自己一个也找不到。 ';help' 是获取所有命令的常规命令。子命令将是 ';help userinfo'、';help rules' 等。我在 discord.py discord 服务器中询问过,我也尝试过使用官方 Python discord 服务器寻求帮助。但似乎没有任何效果。有什么修复吗? 任何帮助都会很棒:)
【问题讨论】:
你为什么不使用内置的子命令支持?为什么要使用 if 语句?继承默认的帮助命令不是更容易吗? @ŁukaszKwieciński 该死。不知道那是一回事 如果您需要嵌入帮助,请查看 this 示例 【参考方案1】:您可以使用 commands.HelpCommand 或 commands.MinimalHelpCommand
例如:
class MyHelpCommand(commands.MinimalHelpCommand):
def get_command_signature(self, command):
return '0.clean_prefix1.qualified_name 1.signature'.format(self, command)
class MyCog(commands.Cog):
def __init__(self, bot):
self._original_help_command = bot.help_command
bot.help_command = MyHelpCommand()
bot.help_command.cog = self
def cog_unload(self):
self.bot.help_command = self._original_help_command
Help command docs
RoboDanny's Help command
注意: 我使用的示例取自 discord.py discord 服务器。
【讨论】:
以上是关于Discord Python 子命令不起作用?的主要内容,如果未能解决你的问题,请参考以下文章