与discord.py中最近的命令名匹配

2024-06-16 11:37:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试为我的discord.py bot创建一个功能,当用户键入的内容不正确时,该功能将发送类似于用户用作命令的命令名。例如,存在一个名为.slap的命令。但是用户输入.slp或类似的内容

我希望bot使用最相似的命令进行响应,在本例中是.slap。我还是个初学者,所以我不知道怎么做。我发现了一个名为fuzzywuzzyLevenshtein distance的库,我不知道如何将它们用于我的bot

任何帮助都将不胜感激! 谢谢


Tags: 用户py命令功能内容键入botlevenshtein
3条回答

你可以使用别名。别名是命令的快捷方式,下面是一个示例:

@client.command(aliases=["slp","spla","spal","slpa","sap","salp"])
async def slap(ctx):
  #Do whatever slap does

要创建别名,请添加aliases=[""]并开始添加别名。别名将作为命令调用。如果我使用了.spla或您添加的任何别名,它仍将执行.slap的操作。希望这有帮助

首先,模糊匹配命令并执行它认为是正确的,这不是一件好事情。它增加了一个失败点,这可能会让用户非常沮丧

但是,如果您建议一系列可能的命令,它可能会工作得更好

FuzzyWozzy是一个很好的工具。
它的文档非常有用,所以我真的认为如果你真的阅读它们,你不会有问题

我实施的2美分将是(用pythonianpesudocode表示)

# user had input an invalid command
invalid_command = #userinput

command_list = [#list of your commands]
fuzzy_ratios = []
for command in command_list:
   ratio = fuzzywuzzy.ratio(invalid_command, command)
   fuzzy_ratios.append(ratio)

max_ratio_index = fuzzy_ratios.index(max(fuzzy_ratios))
fuzzy_matched = command_list[max_ratio_index]

return f"did you mean {fuzzy_matched}?"

请尝试实施并思考为什么需要实施它。
你需要实际地尝试来实现你自己,否则你永远也学不会

您可以尝试以下方法:

disables = []

@client.command()
@commands.has_permissions(administrator=True)
async def disable(ctx, command):
    command = client.get_command(command)
    if not f"{command}: {ctx.guild.id}" in disables:
        disables.append(f"{command}: {ctx.guild.id}")
        await ctx.send(f"Disabled **{command}** for this server.")

    else:
        await ctx.send('This command is already disabled')



@client.command()
@commands.has_permissions(administrator=True)
async def enable(ctx, command):
    command = client.get_command(command)
    if f"{command}: {ctx.guild.id}\n" in disables:
        await ctx.send(f"Enabled **{command}** for this server.")
    else:
        await ctx.send('This command is already enabled')

现在您必须添加:

if "COMMAND: {ctx.guild.id}" in disables:
    return

async def command(ctx)和此命令的代码之间

警告:这是一种非常糟糕的方法。您可以尝试将禁用列表保存到json文件中。如果需要帮助,请向我发送消息-Special unit#5323

相关问题 更多 >