I'm setting up a simple Python Discord bot, but it only seems to respond to one event/command. It only responds to when someone says "supreme sauce" it sends "raw sauce" but doesn't respond to anything else such as ".ping" or ".clear".
Is there anything that I'm doing wrong?
My code:
import discord
from discord.ext import commands
import time
client = commands.Bot(command_prefix = '.')
#client.event
async def on_ready():
print(f'{client.user} has successfully connected to Discord!')
#client.event
async def on_message(message):
if 'supreme sauce' in message.content:
await message.channel.send('raw sauce')
#client.command()
async def ping(ctx):
await ctx.send(f'Pong! {round(client.latency * 1000)}ms')
#client.command
async def clear(ctx, amount=10):
await ctx.channel.purge(limit=amount)
client.run('My Token')
on_message takes priority over commands.
If you want both things to happen, do like this:
async def on_message(message):
if message.author == bot.user: return #Makes sure it can't loop itself when making messages
await bot.process_commands(message)
#rest of your code here
This makes it so that when a message is sent, it will check if that message is a command and go from there, then it will execute the on_message code like normal
Related
I made a discord.py bot for one of our members, as a joke. im testing it out and it doesnt work at all. can someone tell me what can be wrong with this code?
from http import client
import os
import discord
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
client = discord.Client()
#client.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
client.run(TOKEN)
#client.event
async def on_message(message):
await client.process_commands(message) # add this if also using command decorators
if message.author.id == 300677572683890688 and "tranime" in message.content.lower():
await message.delete()
await message.channel.send(f"You are in the process of behavioural therapy. please do not attempt to bypass it, {message.author.mention}")
The structure, how you build the bot, is wrong in many ways, that could cause the error.
To run the bot,
client.run(TOKEN)
always belongs at the end of your code!
To process commands, you put
await client.process_commands(message)
at the end of your on_message event, even though this is not necessary here as you just use discord.Client().
A new structure could be:
client = discord.Client()
#client.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
#client.event
async def on_message(self, message):
if message.author.id == 300677572683890688 and "tranime" in message.content.lower():
await message.delete()
await message.channel.send(f"Your message here")
await client.process_commands(message)
client.run(TOKEN)
The code should also be indented correctly, otherwise there may be problems.
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.
import discord
import os
import requests
import json
import random
from random import randint
from replit import db
from discord.ext import commands
client = discord.Client()
bot = commands.Bot(command_prefix='.')
#client.event
async def on_ready():
print(f'{client.user.name} működik!')
await client.change_presence(activity=discord.Game(name='egy bot vagyok'))
#client.event
async def on_member_join(member):
await member.create_dm()
await member.dm_channel.send(
f' {member.name}, itt van!'
)
#bot.command()
async def test(ctx, arg):
await ctx.send(arg)
print(f'haha')
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('ping'):
await message.channel.send("pong")
if message.content.startswith("shrug"):
await message.channel.send('¯\_(ツ)_/¯')
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('.pic'):
await message.channel.send(file=discord.File('combo-0.png'))
token = os.environ.get("secret")
client.run(token)
im a beginner in discord bot programming
this is my whole code for my discord bot and all of the commands are not working except one
te .pic command
all of the commands worked until now and i dont know why they dont work
if i would get some help i would be happy :D
(sorry for my bad english, english isnt my first language)
You cannot have multiple on_message events, only the last one will be registered. You have to combine them into one
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('ping'):
await message.channel.send("pong")
if message.content.startswith("shrug"):
await message.channel.send('¯\_(ツ)_/¯')
if message.content.startswith('.pic'):
await message.channel.send(file=discord.File('combo-0.png'))
You can't mix up the discord.Client() and commands.Bot clients like this.
commands.Bot is a subclass of discord.Client, so you should be able to do everything that you could do with discord.Client() with commands.Bot()
bot = commands.Bot('!')
#bot.event
async def on_ready():
print('ready')
#bot.command()
async def ping(ctx):
await ctx.send('pong')
For help with the rewrite discord.ext.commands, there are a butt ton of tutorials on the internet.
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
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