Discord.py bot wont display text with prefix - python

import discord
from discord.ext.commands import Bot
bot = Bot(command_prefix='!')
TOKEN = 'TOKEN GOES HERE'
#bot.event
async def on_ready():
print(f'Bot connected as {bot.user}')
await bot.change_presence(activity = discord.Game('SAO'))
#Bellow this it the trouble part
#bot.command(name='test')
async def on_message(message):
if message.content == 'test':
await message.channel.send('Testing 1 2 3!')
await bot.process_commands(message)
bot.run(TOKEN)
I need help getting it to display 'Test 1 2 3!' when I type '!test'? Why the heck is it so hard to post ughhhhh they need more detail but I dint know what else to tell you.

The message comes fully as you typed it. You are checking if the user has typed in "test". Maybe change your if statement to be:
if message.content == "!test":

You are mixing the commands with the events, on_message is an event not a command. according to your example you should use commands. Here is an example
Command
#Run this using !test
#bot.command()
async def test(ctx):
await ctx.send('Testing 1 2 3!')
Event
#bot.event
async def on_message(message):
if message.content == 'hey':
await message.channel.send('Hello')
await bot.process_commands(message)
Read more on commands

Related

My discord bot isn't throwing any error but it's not responding to anything either

So, basically i was trying to make a bot for discord using python and this is my first project so i was trying out new stuffs
here's my code
import discord
from http import client
from discord.ext import commands
client = discord.Client()
client = commands.Bot(command_prefix='`')
#client.event
async def on_ready():
print("Bot is online")
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content == 'hello':
await message.channel.send('Welcome to the server, human')
#client.command
async def info(ctx):
await ctx.send(ctx.guild)
client.run(#mytokenishereicantshareit)
as you can see i am completely new to programming in general, so if you may help me out, the bot is saying "Bot is online" in output and it's getting online in my server its not showing any errors either. but it's none of my commands are working, like the "hello" and `info.
Edit : This issue has been fixed, There are two possible solutions for this either you can replace the #client.event with #client.listen or just add a await bot.process_commands(message) after
if message.content == 'hello':
await message.channel.send('Welcome to the server, human')
Part like
if message.content == 'hello':
await message.channel.send('Welcome to the server, human')
await bot.process_commands(message)
and you're done.
Firstly, remove the client = discord.Client() bit because the bot already does that, and secondly, add a bracket after the #client.command so it's #client.command()

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!')

Disocrd.py command

I am coding a discord bot and i think there has got to be a much easier/ simpler way to detect messages using the prefix for commands (and easy to expand in future), my code at the moment just scans each message to see if it contains the exact command, maybe a class will help?
#client.event
async def on_message(message):
# print message content
print(message.content)
# if the message came from the bot ignore it
if message.author == client.user:
return
# if the message starts with "!repeat" then say the message in chat
if message.content.startswith("!repeat"):
sentmessage = message.content.replace("!repeat", "")
await message.channel.send(sentmessage)
if "hello" in message.content.lower():
await message.channel.send("Hello!")
if message.content.startswith("!cleanup"):
if not message.author.guild_permissions.administrator:
await message.channel.send("You do not have permission to run this command!")
else:
num2c = 0
num2c = int(message.content.replace("!cleanup", ""))+1
print(num2c)
await message.channel.purge(limit=num2c)
num2c = num2c-1
cleanmessage = str("Cleared "+str(num2c)+" Messages.")
await message.channel.send(cleanmessage, delete_after=5)
You can use commands.Bot, it has a built-in command system, here's an example:
import discord
from discord.ext import commands
# enabling intents
intents = discord.Intents.default()
bot = commands.Bot(command_prefix='!', intents=intents)
#bot.command()
async def foo(ctx, *args):
await ctx.send('whatever')
# You also have all the functionality that `discord.Client` has
#bot.event
async def on_message(message):
# ...
# you always need to add this when using `commands.Bot`
await bot.process_commands(message)
bot.run('token')
Take a look at the introduction

Discord bot only responds to one command

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

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