我正在尝试制作一个搜索维基百科的不和谐机器人

I'm trying to make a discord bot that searches wikipedia

我正在尝试制作一个让人们搜索维基百科的 discord 机器人,例如有人发送 /wikipedia [他们的搜索] 并搜索它并将结果发送到聊天中。这是我第一次尝试制作机器人。这是我到目前为止得到的,但我经常出错。

import wikipedia
def search():
    search = wikipedia.summary(question, sentances=2)
    return search

[some other code]

@client.command()
async def wikipedia(ctx,*,question):
    await ctx.send(search())

我收到这样的错误

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: 
 AttributeError: 'Command' object has no attribute 'summary' 

问题是您的函数名与库本身同名。因此,当您调用 wikipedia.summary 时,您的代码认为您正在尝试访问 wikipedia 函数的摘要 属性。

据我所知,您有两种选择来解决这个问题。你可以

  • 更改命令的名称
  • 使用 import x as y 来更改维基百科库的名称

对于第一个,您可以将其更改为 wikipedia_search 而不是 wikipedia,因此它看起来像这样:

@client.command()
async def wikipedia_search(ctx,*,question):
    await ctx.send(search())

对于第二个选项,您可以将导入行从 import wikipedia 更改为 import wikipedia as wikipedia_lib。然后,当您尝试使用该库时,您可以改为 wikipedia_lib.summary(question, sentances=2)

此外,作为旁注,您将 sentences 拼错为 sentances,这可能会导致错误。因此,我建议您更改它。