ACTIONIFY
ACTIONIFY

Reputation: 5

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

Upvotes: 1

Views: 198

Answers (1)

Jason Rebelo Neves
Jason Rebelo Neves

Reputation: 1271

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.

Upvotes: 1

Related Questions