【问题标题】:Can I make two functions with the same name but different parameters in discord.py when building a discord bot?在构建不和谐机器人时,我可以在 discord.py 中创建两个同名但参数不同的函数吗?
【发布时间】:2020-09-16 15:39:13
【问题描述】:

我现在正在我的 discord 机器人中处理 8balls 功能。我希望它在人们只调用“!8balls”但在频道中没有输入“问题”时做出反应。它应该在通道中打印出一行而不是什么都不做。

@client.command('8balls')
async def _8balls_1(ctx):
    await ctx.send(':warning:Please enter a question after the command and seperate them with a space.')

@client.command('8balls')
async def _8balls(ctx, *, question):
    responses = ["It is certain.",
"It is decidedly so.",
"Without a doubt.",
"Yes - definitely.",
"You may rely on it.",
"As I see it, yes.",
"Most likely.",
"Outlook good.",
"Yes.",
"Signs point to yes.",
"Reply hazy, try again.",
"Ask again later.",
"Better not tell you now.",
"Cannot predict now.",
"Concentrate and ask again.",
"Don't count on it.",
"My reply is no.",
"My sources say no.",
"Outlook not so good.",
"Very doubtful."]
    await ctx.send(f'Question:\"{question}\"\n Response:\"{random.choice(responses)}\"')

我试图通过这样的写作来实现多态性。不幸的是,它不起作用。我该如何解决?

【问题讨论】:

  • 忽略命令 _8balls 中的异常:回溯(最后一次调用):文件“/home/zyck/anaconda3/lib/python3.7/site-packages/discord/ext/commands/bot.py ”,第 903 行,在调用 await ctx.command.invoke(ctx) 文件“/home/zyck/anaconda3/lib/python3.7/site-packages/discord/ext/commands/core.py”,第 847 行,在调用 await self.prepare(ctx) 文件“/home/zyck/anaconda3/lib/python3.7/site-packages/discord/ext/commands/core.py”,第 784 行,准备等待 self._parse_arguments(ctx)
  • 文件“/home/zyck/anaconda3/lib/python3.7/site-packages/discord/ext/commands/core.py”,第 690 行,在 _parse_arguments 中转换 = await self.transform( ctx,参数)文件“/home/zyck/anaconda3/lib/python3.7/site-packages/discord/ext/commands/core.py”,第 535 行,在转换中提高 MissingRequiredArgument(param) discord.ext.commands。 errors.MissingRequiredArgument:问题是缺少的必填参数。
  • 当我隐藏 _8balls(ctx) 函数时出现这些错误行。

标签: python bots discord.py


【解决方案1】:

如果没有传递question 参数,您可以处理错误,不需要两个单独的命令。

@client.command('8balls')
async def _8balls(ctx, *, question):
    responses = ["It is certain.",
                 "It is decidedly so.",
                 "Without a doubt.",
                 "Yes - definitely.",
                 "You may rely on it.",
                 "As I see it, yes.",
                 "Most likely.",
                 "Outlook good.",
                 "Yes.",
                 "Signs point to yes.",
                 "Reply hazy, try again.",
                 "Ask again later.",
                 "Better not tell you now.",
                 "Cannot predict now.",
                 "Concentrate and ask again.",
                 "Don't count on it.",
                 "My reply is no.",
                 "My sources say no.",
                 "Outlook not so good.",
                 "Very doubtful."]
    await ctx.send(f'**Question**: \"{question}\"\n**Response**: \"{random.choice(responses)}\"')


@_8balls.error
async def missing_question(ctx, error):
    if isinstance(error, commands.MissingRequiredArgument):
        await ctx.send(':warning: Please enter a question after the command and seperate them with a space.')

【讨论】:

    【解决方案2】:

    您不能像在 C 或其他语言中那样重载 Python 中的函数。你可以给你的函数参数提供默认值并在你的函数中处理它:

    def my_func(param1, param2, param3=None, param4=None):
        # your logic
    

    这样您可以使用 2 个必需参数(param1 和 param2)调用函数 my_func,并且可以添加可选参数 param3 和 param4。在函数体内,您可以检查是否存在可选参数并相应地调整您的逻辑。

    【讨论】:

    • 非常感谢。我不知道Python中不允许重载。
    【解决方案3】:

    使用从 Python 3.5 开始的 typing 模块,您实际上可以进行函数重载:

    https://docs.python.org/3/library/typing.html#typing.overload

    https://www.python.org/dev/peps/pep-0484/#function-method-overloading

    【讨论】:

      猜你喜欢
      • 2020-12-23
      • 1970-01-01
      • 2021-06-11
      • 2021-11-06
      • 2023-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-29
      相关资源
      最近更新 更多