Reputation: 45
Hey guys i tried to make a simple bot in python but im having a headheach find why my bot doesn´t listen to commands but listens to events, but if i remove my events he listens to commands. And can someone explain to me why some people use client and others use Bot. For example some people start with client = commands.Bot(command_prefix='!',intents = discord.Intents.all())
and others with Client = discord.Client()
import discord
from discord.ext import commands
client = commands.Bot(command_prefix='!',help_command=None,intents = discord.Intents.all())
@client.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
@client.event
async def on_message(message):
#doesnt read the messages from bot
if message.author == client.user:
return
if message.content.startswith('!ligar'):
#get date
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
#turns caldeira
#SwitchCaldeira()
#meter aqui o tempo a que a caldeira foi ligado
await message.channel.send(f'Caldeira ligada, as {current_time}')
@client.command()
async def hello(ctx):
await ctx.channel.send('funciona fdp')
#main runner
client.run('token')
Upvotes: 0
Views: 845
Reputation: 11
I have found a workaround around this problem, as just moving Commands above Events does not solve the issue here... Events somehow take precedence over anything else in the file.
Try adding await process to the function as follows (position matters): await bot.process_commands(message)
@client.event
async def on_message(message):
await bot.process_commands(message)
if message.author == client.user:
return
if message.content.startswith('!ligar'):
#get date
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
#turns caldeira
#SwitchCaldeira()
#meter aqui o tempo a que a caldeira foi ligado
await message.channel.send(f'Caldeira ligada, as {current_time}')
Upvotes: 1
Reputation: 366
The code you posted, you using event listeners and commands in one file. It's not good practice to do like this.
But the reason your bot is not responding to your commands is responding to events is because of the order in which your code is executed. In your script, the event handlers for on_ready
and on_message
are defined before the command handler for hello
. This means that when the bot is ready and receives a message, it first checks if the message is from the bot itself and if the message starts with "!ligar", and only then does it check for any commands. To make the bot respond to your commands, you should move the command handler for hello above the event handlers to make them work properly.
Instead having like that have like this if you want prefix for your bot and simplicity to write neat and clean code.
from discord.ext import commands
intents = discord.Intents.all()
client = Client(command_prefix="!", intents=intents)
To have bot commands, you have to use like:
commands.command()
=> if you using in cogs
client.command()
=> if you using in single file
for example:
@commands.command(description="test")
async def test(self, ctx):
await ctx.send("Test complete!")
Upvotes: 2