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

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)

Related

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.

Discord.py on_message cancelling other commands

I am trying to build a simple bot that can kick, ban, and say "Hi" to a member. Kick and ban work perfectly fine and do their job, but when I add an "on_message" function, the on_message function works but cancels out kick and ban. Any ideas?
My code:
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix="!")
#bot.command()
async def kick(ctx, member: discord.Member, *, reason=None):
await member.kick(reason=reason)
#bot.command()
async def ban(ctx, member: discord.Member, *, reason=None):
await member.ban(reason=reason)
#bot.event
async def on_message(message):
if "!hello" == message.content:
await message.channel.send(f"Hi")
await bot.process_commands(message)
bot.run("my token")
First, I would make !hello a command:
#bot.command()
async def hello(ctx):
author=ctx.author
await ctx.send(f'Hello, {author.mention}!')
Then change your on_message function to:
#bot.event
async def on_message(msg):
await bot.process_commands(msg)
I usually keep event functions before the commands for organization's sake. Here though, unless you'll have something else to check for when a user sends a message, I don't think you technically need an async def on_message/await bot.process_commands event.
From my personal experience events and commands don't do well when they are in the same file, consider using cogs to seperate them.

discord.py command message is taking in as non command message

I have this issue where I want to take input as a command so the user types !ping and my prefix is !, but for some reason it does not direct towards:
#client.command()
async def ping(ctx):
print("Someone asked for their latency")
for some reason it directs it to:
#client.event
async def on_message(message):
print("Someone send a message")
I want when a user types !ping it directs it to ping(ctx) and not on_message(message) like it is doing right not. This is my full code:
import discord
from discord.ext import commands
intents = discord.Intents.all()
client = commands.Bot(command_prefix="!", intents=intents)
#client.event
async def on_ready():
print("I am ready")
#client.event
async def on_member_join(member):
join_embed = discord.Embed(title=f'{member.name} Joined :)',
description=f"Hey {member.name} remember that addison is weird", colour=16366985)
join_embed.set_image(url="https://www.desicomments.com/wp-content/uploads/2017/07/Hi.gif")
await member.guild.system_channel.send(content=None, embed=join_embed)
print("joined")
#client.event
async def on_member_remove(member):
left_embed = discord.Embed(title=f'{member.name} Left :(',
description=f"Bye Bye {member.name}", colour=16726802)
left_embed.set_image(url="http://www.reactiongifs.com/wp-content/uploads/2012/12/byebye.gif")
await member.guild.system_channel.send(content=None, embed=left_embed)
print("left")
#client.event
async def on_message(message):
print("Someone sent a message")
#client.command()
async def ping(ctx):
print("Someone asked for their latency")
client.run('hajslkdhasjlkdhjwheq')
You can simply add await client.process_commands(message) as the last line in on_message() to fix this problem

Programming a Discord bot in Python- How do I make a kick command?

I want to make a command that kicks a specified user when prompted. Here's what I have:
#bot.command()
async def kick(ctx, user: discord.Member, *, reason="No reason provided"):
await user.kick(reason=reason)
kick = discord.Embed(title=f"Kicked {user.name}!", description=f"Reason: {reason}\nBy: {ctx.author.mention}")
await ctx.message.delete()
await ctx.channel.send(embed=kick)
await user.send(embed=kick)
It doesn't seem to be working, any tips?
Here are two things. Since your on_message events are working using client.event, this means that you should replace your bot.command with client.command as seen below:
#client.command()
async def kick(ctx, user: discord.Member, *, reason="No reason provided"):
await user.kick(reason=reason)
kick = discord.Embed(title=f"Kicked {user.name}!", description=f"Reason: {reason}\nBy: {ctx.author.mention}")
await ctx.message.delete()
await ctx.channel.send(embed=kick)
await user.send(embed=kick)
If your kick command still doesn't work, at the very end of your on_message event, you should add await client.process_commands(message). I'll put an example of this below below:
#client.event
async def on_message(message):
if message.content == "Test":
print("recieved")
await client.process_commands(message)

Discord.py #bot.event

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

Categories