Search on blog:

Python: Jak przetwarzać wszystkie wiadomości w Discord.

bot.commands is extension (you import it from discord.ext) which helps to make code simpler - ie. it automatically recognize if message has prefix, it splits message to words, and it assigns command to function.

But you don't have to use it. You can use event on_message to get full text from user and then you can process it on you own. It means you can compare with pattern/regex, lemmatize, use with NLTK, etc. But it also means you have to on your own split message to words and recognize prefix (if you want to run code only when command has prefix).

import discord
import random
import shlex  # to split `say "A B C"` as two elements `say` `"A B C"`

data = {
    'hello': ['Hello World!', 'Good Morning Vietnam!', 'Good Day My Lord']
}

client = discord.Client()

@client.event
async def on_message(message):
    # ignore messages made by the bot
    if message.author == client.user:
        return

    # preprocessing message
    text = message.content.lower()
    #words = text.split(' ')        # 'say "A B C"' -> ['say', '"A', 'B', 'C"']
    words = list(shlex.shlex(text)) # 'say "A B C"' -> ['say', '"A B C"'] 
    if not words:
        await message.channel.send("I don't understand you: " + str(words))
        return

    first_word = words[0] 

    # only message which starts with prefix `$`
    #if not text.startswith('$'):
    #    return 

    found = False

    # compare message with dictionary
    for word, answers in data.items():
        #if text.startswith(word): # will treat `hellowin` like `hello`
        if first_word == word:     # will skip `hellowin` 
            found = True
            selected = random.choice(answers) 
            await message.channel.send(selected)

    if not found:
        await message.channel.send("I don't understand you: " + str(words))

client.run("MY_TOKEN")

Notatki:

Stackoverflow How do I make my discord bot understand my word list? And execute it?

Przykład w dokumentacji w Quickstart

« Page: 1 / 1 »