Kali
Kali

Reputation: 65

Python Discord Bot: how to interact with the user?

I'm trying to make a Discord bot for my server, and I'm having some difficulties. I've looked at other people's questions, applied all type of changes and I'm still stuck. As reference, I'm relative new with Python, and 100% a beginner with Discord bots. So, this is my code:

import discord
from discord.ext import commands


prefix = ">"
client = commands.Bot(command_prefix=prefix, case_insensitive=True)

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('>hello'):
        msg = 'Hello, {0.author.mention}!'.format(message)
        await message.channel.send(msg)
        

@client.command(name = "pomodoro")
async def Pomodoro(ctx):
    if ctx.content.startswith('>pomodoro'):
        await ctx.channel.send("Let's grab some tomatoes! For how many minutes?")

    def check(msg):
        return msg.author == ctx.author and msg.channel == ctx.channel and \
               type(msg.content)==int

    msg = await client.wait_for("message", check=check)

The hello function works perfectly. My issue is the pomodoro one (it's unfinished, of course). My intention with this function is to ask the user for how many minutes they want to study and then for how many minutes they want to rest and then set a timer with both variables. But I can't even make it send the first message ("Let's grab some tomatoes! For how many minutes?"). I don't know what I'm doing wrong, specially when the first function is working fine. Thanks in advance!

Upvotes: 0

Views: 454

Answers (1)

Jawad
Jawad

Reputation: 2041

Overriding the default provided on_message forbids any extra commands from running. To fix this, add a client.process_commands(message) line at the end of your on_message.

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('>hello'):
        msg = 'Hello, {0.author.mention}!'.format(message)
        await message.channel.send(msg)

    await client.process_commands(message)  # <----


@client.command(name="pomodoro")
async def _pomodoro(ctx):
    await ctx.channel.send("Let's grab some tomatoes! For how many minutes?")

    def check(msg):
        return msg.author == ctx.author and msg.channel == ctx.channel and \
               type(msg.content) == int

    msg = await client.wait_for("message", check=check)

Why does on_message make my commands stop working?

Upvotes: 1

Related Questions