Discord.py #bot.event - python

So I have a script that uses both #bot.event and #bot.command(). The problem is that when I have a #bot.event waiting the #bot.command() will not run.
Here is my code:
#bot.event
async def on_ready():
print("Bot Is Ready And Online!")
async def react(message):
if message.content == "Meeting":
await message.add_reaction("๐Ÿ‘")
#bot.command()
async def info(ctx):
await ctx.send("Hello, thanks for testing out our bot. ~ techNOlogics")
#bot.command(pass_context=True)
async def meet(ctx,time):
if ctx.message.author.name == "techNOlogics":
await ctx.channel.purge(limit=1)
await ctx.send("**Meeting at " + time + " today!** React if you read.")
#bot.event ##THIS ONE HOLDS UP THE WHOLE SCRIPT
async def on_message(message):
await react(message)

When using a mixture of the on_message event with commands, you'll want to add await bot.process_commands(message), like so:
#bot.event
async def on_message(message):
await bot.process_commands(message)
# rest of code
As said in the docs:
This function processes the commands that have been registered to the bot and other groups. Without this coroutine, none of the commands will be triggered.
If you choose to override the on_message() event, you then you should invoke this coroutine as well.
References:
Bot.process_commands()
on_message()

Related

#client.event excludes other commands discord bot python [duplicate]

This question already has answers here:
Why does on_message() stop commands from working?
(2 answers)
Closed 10 months ago.
This #client.event excludes other commands!!! Pls help me!
async def on_message(message): if client.user.mention in message.content.split(): await message.channel.send('xyz')
This is my code: (Without the #client.event it works corectly, but when I add other #client.event, rest of the code doesnt works, only client.event works. Guys help!!!
#client.event
async def on_ready():
print('xyz ~ {0.user}'.format(client))
await client.change_presence(activity=discord.Game(name="Piwo?๐Ÿป"))
#client.command()
#has_permissions(ban_members=True)
async def ban(ctx, member : discord.Member, reason="Za darmo lamusie"):
await member.ban(reason=reason)
await ctx.channel.send(f"***xyz๐Ÿ˜Ž***")
#client.command()
#has_permissions(kick_members=True)
async def kick(ctx, member : discord.Member, reason="Za darmo lamusie"):
await member.kick(reason=reason)
await ctx.channel.send(f"***{member.mention} xyz ๐Ÿ˜Ž***")
#client.command()
async def jaka_gierka(ctx):
gierki = [xyz]
await ctx.channel.send(random.choice(gierki))
#client.command()
async def graj(ctx, game):
await client.change_presence(activity=discord.Game(name=game))
#client.command()
async def streamuj(ctx, game):
await client.change_presence(activity=discord.Streaming(name=game, url="xyz"))
#client.command()
async def sluchaj(ctx, music):
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name=music))
#client.command()
async def ogladaj(ctx, film):
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name=film))
#client.event
async def on_message(message):
if client.user.mention in message.content.split():
await message.channel.send('xyz')
client.run("token")```
You need to process commands in on_message.
Without doing this, none of the commands will execute. The default behavior was to have an on_message that only processed commands. However, you made a custom event handler, so this must be done manually. You can change your on_message to this:
On an unrelated note, checking for the <#1234> mention sequence in the message is a bad idea. Instead, you can use mentions.
#client.event
async def on_message(message):
if client.user in message.mentions: # checks if the bot was in the list of people mentioned
await message.channel.send('xyz')
await client.process_commands(message)

Can't run a join function because of a on_message event

i'm making a discord bot with python. I have some issues about running using #client.command and #client.event at the same time.
Here is the code:
when I comment the #client.event before the on message function, the join command run. This function cause a particular issue, do you know guys where it can come from? Thank you guys
import discord
import random
from discord.utils import get
from discord.ext import commands
#client.command(pass_context=True)
async def join(ctx):
channel = ctx.author.voice.channel
await channel.connect()
#client.command(pass_context=True)
async def leave(ctx):
await ctx.voice_client.disconnect()
#client.event
async def on_ready():
print("We have logged as {0.user}".format(client))
#client.event
async def on_message(message):
user = message.author.id
if message.content.lower() == "!poisson":
await message.delete()
with open('myimage.png', 'rb') as f:
picture = discord.File(f)
await message.channel.send(file=picture)
Put await client.process_commands(message) at the end of on_message()
If you're using on_message, then normal commands will be overridden unless you use process_commands.

Commands don't work. They used to work but they don't any more

I added the bot status and after that the
Commands don't work. Added a answerers answer but it still no work ( help works not but not hello ;-;)
import discord
from KeepAlive import keep_alive
client=discord.Client()
#client.event
async def on_ready():
await client.change_presence(status=discord.Status.online,activity=discord.Game('Hey There! Do โ‚ฌhelp to start!'))
print('We have logged in as {0.user}'.format(discord.Client))
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$hello'):
await message.channel.send('Hello!')
if message.content.startswith('$help'):
await message.channel.send('no help here!')
await bot.process_commands(message)
keep_alive()
client.run('wont say token :)')
If you are talking about commands and not "commands" that you run with on_message then you have to add await client.process_commands(message) (check this issue in documentation). If your on_message event is not working then it's probably only because of missing _ in on_message event.
#client.event
async def on_message(message): # your forgot "_"
if message.author == client.user: # discord.Client won't work. Use client.user instead
return
if message.content.startswith('$hello'):
await message.channel.send('Hello!')
if message.content.startswith('$help'):
await message.channel.send('no help here!')
await client.process_commands(message) # add this line at the end of your on_message event
the problem is your message function, it has async def onmessage(message): but the correct one is:
#client.event
async def on_message(message):
And I recommend defining the prefix and then separating it from the message so you don't have to keep typing $ in every if, and save the elements written after the command for future functions:
PREFIX = "$"
#client.event
async def on_message(message):
msg = message.content[1:].split(' ')
command = msg[0]
if command == "hello":
await message.channel.send('Hello!')

on message event make my bot don't working

My commands don't work and I think it's because of the on_message event. When it checks if message is in the good channel, it actually "steel" the command so my bot commands are not triggered but I don't know how to fix that
import discord, os
from discord.ext import commands
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix='.',intents=intents)
defrole = ""
cmdchannels = ["none"]
#bot.event
async def on_ready():
print(f'{bot.user.name} has connected to Discord!')
await bot.change_presence(activity=discord.Game('.help'))
#bot.event
async def on_member_join(member):
await member.add_roles(discord.utils.get(member.guild.roles, name=defrole))
#bot.event
async def on_message(message):
if message.channel.id in cmdchannels :
await message.delete()
#bot.command()
async def setcmd(ctx, arg):
global cmdchannels
cmdchannels.append(discord.utils.get(ctx.guild.channels, name=arg).id)
print(cmdchannels)
await ctx.send(arg+' is now defined as a cmd/bot channel !')
#bot.command()
async def defaultrole(ctx):
global defrole
defrole = str(ctx.message.role_mentions[0])
await ctx.send(defrole+' is now the default on join role !')
bot.run("Token")
You will need to process the commands, add this line at the end of your on_message function:
await bot.process_commands(message)
Reference: process_commands()
on_message event blocks your commands from working. In order to prevent this, you have to process commands.
#bot.event
async def on_message(message):
if message.channel.id in cmdchannels :
await message.delete()
await bot.process_commands(message)

Commands not being recognized by discord.py

I have a simple discord.py set up trying to use .ping, but in this current instance, the actual sending of ".ping" results in nothing being sent by the bot. Is there something I'm missing here?
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix = ".")
#bot.event
async def on_ready():
print("Everything's all ready to go~")
#bot.event
async def on_message(message):
author = message.author
content = message.content
print(content)
#bot.event
async def on_message_delete(message):
author = message.author
channel = message.channel
await bot.send_message(channel, message.content)
#bot.command()
async def ping():
await bot.say('Pong!')
bot.run('Token')
Ensure that you have await bot.process_commands(message) somewhere in your on_message event.
Why does on_message make my commands stop working?
Overriding the default provided on_message forbids any extra commands from running. To fix this, add a bot.process_commands(message) line at the end of your on_message. For example:
#bot.event
async def on_message(message):
# do some extra stuff here
await bot.process_commands(message)
https://discordpy.readthedocs.io/en/latest/faq.html#why-does-on-message-make-my-commands-stop-working

Categories