0

I'm trying to make a Discord bot with Discordpy and WikipediaApi. The bot is supposed to print Wikipedia summary after entering an appropriate sentence: "What is...?" or "Who is...?". So far, it's working well, but the Wikipedia page is defined in the code. I want take a page title as keyword from user, for example, he might ask "What is Microsoft?" or "Who is Elon Musk?" and receive a reply (if a page exists in Wikipedia, of course). I know I should use client.wait_for(), but I feel confused with that, as the input will be a part of longer sentence.

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')

2
  • Is there a specific reason you're using the wikipedia api? IMO it would be easier in many ways to use the requests api to get the source code of the page, if it exists Commented Sep 13, 2020 at 17:55
  • I noticed that Wikipedia api is a popular solution and it was not so hard to find how to implement it in my own code. Also, the result on Discord looks as I want. Commented Sep 13, 2020 at 18:02

1 Answer 1

0

Seeing as you're trying to parse it out of every message, you could try just splitting the "command" (What is/Who is) and take the remainder as the query.

if message.content.startswith('What is') or message.content.startswith('Who is'):
    args = message.content.split(" ")  # Split the message based on spaces
    if len(args) > 2:  # Checks if a query was passed, avoids a potential IndexError
        query = " ".join(args[2:])  # Cut out the "What is" part, and join the rest back into a single string with spaces
        wiki_wiki = wikipediaapi.Wikipedia('pl')
        page_py = wiki_wiki.page(query)

I do strongly recommend using the built-in Commands module though, and just making a "Wiki" command. This way, your code would look as follows:

@client.command()
async def wiki(ctx, *query):
    if query:  # I'm not sure what the wikipedia api does if you give it an empty string, so this if-statement makes sure something was passed
         query = " ".join(query)  # The rest of the args were passed as a list, so join them back into a string with spaces
         wiki_wiki = wikipediaapi.Wikipedia('pl')
         page_py = wiki_wiki.page(query)

And you can call it in Discord by simply doing [prefix]wiki elon musk. In your case, you check if the bot was mentioned to parse commands which can also be handled with prefixes, so you wouldn't lose any functionality on that end (and can add more prefixes if you so wish).

Sign up to request clarification or add additional context in comments.

1 Comment

I decided to not to use cogs and prefix commands, as my bot is a chatbot based on AIML files (so its main role is to have conversations with the user). It was important for me that it can react naturally to certain sentences. I will try you suggestions!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.