I wanted to test some new things so I wanted to create a bot discord so I started to do something very basic :
import discord
client = discord.Client(intents=discord.Intents.default()
DISCORD_TOKEN = "my token"
#when the bot is online
#client.event
async def on_ready():
print("The bot is online")
#the bot reacts to messages
#client.event
async def on_message(message):
print(message.content)
client.run(DISCORD_TOKEN)
and when I run the code the bot goes online but the bot doesn't react to the message nothing happens so sure the code is basic for now but the bot was supposed to react to the message in the terminal right?
this is the first time I use the discord.py library... so Can someone help me to solve this problem please I would be very grateful
Pretty sure the issue is that you haven't enabled the message_content intent - you need to set that to be able to read message content.
intents = discord.Intents.default()
intents.message_content = True # explicitly enable the message content intents
client = discord.Client(intents=intents)
You will also need to enable this intent for your bot/application in the Discord Developer portal.
The message_content intent is on the first page of the docs.
Discord intents.
My discord bot connects just fine, event on_ready is working with no problems. However when I call any of the commands declared with #bot.command, the bot doesn't recognize the message as a command (at least that's what I think it's happening).
Code:
import discord
import pickle
from discord.ext import commands
from discord.ext.commands import bot
token = ""
bot = commands.Bot(command_prefix='!', case_insensitive=True, intents=discord.Intents.default())
#bot.event
async def on_ready():
print('We have logged in as {0.user}'.format(bot))
#bot.command()
async def test(ctx):
print('received')
await ctx.send('msg received')
bot.run(token)
That's pretty much it, when I run it the bot connects just fine, but the !test command doesn't work.
Let me know if I didn't add important information, first time asking something here.
EDIT: It's working locally, it doesn't work when it's being hosted on heroku (bot connects and is online, doesn't recognize commands).
Requirements file:
git+https://github.com/Rapptz/discord.py
discord~=1.7.3
setuptools~=60.2.0
This is my first time making a discord bot. I've followed every step of the tutorial but my bot doesnt react to the !test command. I do see the bot in the discord server. please help
This is my first time making a discord bot. I've followed every step of the tutorial but my bot doesnt react to the !test command. I do see the bot in the discord server. please help
Firstly, on line 15, your if statement for if(message.content.startswith(!test): is place after the return. The bot will never reach those lines. You will have to move the indent back (indents in python are annoying).
if message.author == client.user:
return
if message.content.startswith('!test'):
await message.channel.send("Hello!")
await client.process_commands(message)
Secondly, await client.process_commands(message) only works with a command client and does not work with the base discord.Client(). The client would have to to be something like
from discord.ext import commands
client = commands.Bot(command_prefix="!")
See What are the differences between Bot and Client? for the difference between discord.Client() and commands.Bot().
Okay so I wanted to make a discord bot for my first python project. However, despite my seemingly correct code, the bot doesn't run:
I used this basic code to simply get the bot online but nothing happens. I haven't seen anyone else have this problem either.
import discord
from discord.ext import commands
client = commands.Bot(command_prefix = '.')
#client.event
async def on_ready():
print("Bot is ready")
client.run("Token") # i removed the token for security
and this is the result of the program
Am I doing something wrong?
I am using this version of the discord botting API: https://github.com/Rapptz/discord.py
As #jwjhdev said, client.run is not supposed to be tabbed under on_ready. If you delete the tab, then everything should work perfectly.
I have previously posted on this same bot and got it working thanks to the people who responded. But, while it finally came to life and turned on, it started spamming messages for no apparent reason. I've looked over the code for typos and can't find any.
Here's the code:
import discord
from discord.ext.commands import bot
from discord.ext import commands
import asyncio
import time
Client = discord.Client()
client = commands.Bot (command_prefix = discord)
#client.event
async def on_ready() :
print("Bepis machine fixed")
#client.event
async def on_message(message) :
if message.content == "bepis" :
await client.send_message (message.channel, "bepis")
client.run("Censored Bot Token")
after #client.event is where i need help. also the bottom line if fine this time! turns out i had hit the space bar before the parenthesis and it didn't like that. Help is very appreciated so i can continue adding on to this awesome bot.
It looks like you are sending a message "bepis" in response to the first, then every, message "bepis" - presumably your first response will appear as an entry on the incoming feed which will trigger a second, etc.
Turns out I was not using proper formatting for my bot.
Whenever you say "bepis" in the discord server, the bot will see it and then say "bepis" back, as its intended to do, but, because of my improper formatting, the bot saw itself say "bepis" and responded as if someone else was saying "bepis".
Old lines:
if message.content == "bepis" :
await client.send_message(message.channel, "bepis")
New lines:
if message.content.startswith('bepis'):
await client.send_message(message.channel, "bepis")
So make sure you're using the right format if you're making a bot!
You seemed to already know what the problem is, from this answer you posted. But your solution is far from being able to solve the problem.
on_message is called whenever a new message is sent to anywhere accessible by the bot; therefore, when you type in "bepis" in discord, the bot replies with "bepis", then the message sent by the bot goes into on_message, which the bot re-replies "bepis", and so on...
The simple solution is to check if the message's author is any bot account, or if you want, if the message's author is your bot.
from discord.ext import commands
client = commands.Bot(command_prefix=None)
#client.event
async def on_ready():
print("Bepis machine fixed")
#client.event
async def on_message(message):
# or `if message.author.bot:` # which checks for any bot account
if message.author == client.user:
return
if message.content == "bepis":
await client.send_message(message.channel, "bepis")
client.run("Token")
Note: I also fixed many of your other problems, such as multiple unused imports, another Client, and indentation.
And FYI, command_prefix is only used when command is being processed by a functional command. When you use on_message it has no use, which means you can set it to None.