Lukim
Lukim

Reputation: 167

Python 3.6 Discord bot Bot events conflict

So I have two bot events One that responds to the message "k" with "k" and one that's a simple guess my number between 1-10 The problem is that they conflict and only one works (the one that's below) IDK what I am missing. Code:

@client.event
async def on_message(message):
    # we do not want the bot to reply to itself
    if message.author == client.user:
        return
    if message.author.bot: return
    if message.content==('k'):
        msg = 'k'.format(message)
        await client.send_message(message.channel, msg)

    await client.process_commands(message)




@client.event
async def on_message(message):
    # we do not want the bot to reply to itself
    if message.author == client.user:
        return

    if message.content.startswith('!guess'):
        await client.send_message(message.channel, 'Guess a number     between 1 to 10')

        def guess_check(m):
            return m.content.isdigit()

        guess = await client.wait_for_message(timeout=10.0,    author=message.author, check=guess_check)
        answer = random.randint(1, 10)
        if guess is None:
            fmt = 'Sorry, you took too long. It was {}.'
            await client.send_message(message.channel,     fmt.format(answer))
            return
        if int(guess.content) == answer:
            await client.send_message(message.channel, 'You are right!')
        else:
            await client.send_message(message.channel, 'Sorry. It is     actually {}.'.format(answer))

    await client.process_commands(message)

So How do I make it so they DON'T conflict?

Upvotes: 2

Views: 876

Answers (1)

Adam Barnes
Adam Barnes

Reputation: 3232

You have defined the function on_message() twice.

To demonstrate the problem, what would you expect the output to be if I ran the following code?

def f(x):
    print(x)

def f(x):
    print('Nothing useful')

f(3)

The same issue is present in your code.

Assuming the discord framework will call a function called on_message() when a message is received, you need to have one on_message() function that handles any input. As such, it will look like this:

@client.event
async def on_message(message):
    # we do not want the bot to reply to itself
    if message.author == client.user:
        return

    if message.content==('k'):
        ...

    if message.content.startswith('!guess'):
        ...

If you're feeling particularly snazzy, you can break out the contents of the if blocks into their own functions, to make the script easier to read, but I'll leave that as an exercise for you, once you have the rest of it working.

Upvotes: 3

Related Questions