简体   繁体   中英

How do I merge 2 on_message functions in discord.py

I need to merge these 3 on_message functions into 1 function in discord.py rewrite version 1.6.0, what can I do?

import discord

client = discord.Client()

@client.event
async def on_ready():
    print('in on_ready')

@client.event
async def on_message(hi):
    print("hello")

@client.event
async def on_message(Hey there):
    print("General Kenobi")

@client.event
async def on_message(Hello):
    print("Hi")

client.run("TOKEN")

The on_message function is defined as:

async def on_message(message)

So you need to check the message contents in the on_message command and do whatever you need to depending on its contents, for that you can use the Message object's content attribute, as defined here here :

@client.event
async def on_message(message):
    if message.content == "hi":
        print("hello")
    elif message.content == "Hey there":
        print("General Kenobi")
    elif message.content == "Hello":
        print("Hi")

Do note though that if you want the bot to actually send a message, you need to replace print(...) with await message.channel.send(...) , as that is how you send a message, print only outputs to the terminal by default.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM