【发布时间】:2020-09-14 02:24:38
【问题描述】:
我正在尝试使用 Discordpy 和 WikipediaApi 制作 Discord 机器人。该机器人应该在输入适当的句子后打印维基百科摘要:“什么是......?”或“谁是……?”。到目前为止,它运行良好,但维基百科页面是在代码中定义的。我想将页面标题作为用户的关键字,例如,他可能会问“微软是什么?”或“埃隆·马斯克是谁?”并收到回复(当然,如果维基百科中存在页面)。我知道我应该使用 client.wait_for(),但我对此感到困惑,因为输入将是较长句子的一部分。
import discord
import wikipediaapi
@client.event
async def on_message(message):
if not (message.author == client.user):
if (message.mention_everyone == False):
if (client.user.mentioned_in(message) or isinstance(message.channel, discord.abc.PrivateChannel)):
async with message.channel.typing():
if message.content.startswith('What is') or message.content.startswith('Who is'):
wiki_wiki = wikipediaapi.Wikipedia('pl')
page_py = wiki_wiki.page('Elon Musk') <----- HERE
await message.channel.send("Page - Summary: %s" % page_py.summary)
client.run('token')
【问题讨论】:
-
您使用 wikipedia api 是否有特定原因? IMO 在许多方面使用
requestsapi 来获取页面的源代码(如果存在)会更容易 -
我注意到 Wikipedia api 是一种流行的解决方案,并且不难找到如何在我自己的代码中实现它。此外,Discord 上的结果看起来如我所愿。
标签: python discord bots discord.py