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().
Related
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.
I have simplest python program of discord bot
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
#bot.event
async def on_message(msg):
print(msg.content)
bot.run('token')
And it prints just empty string. Before that I tried bot.command() but bot simply doesn't responds to it probably because message is empty so like there's no command. I saw this problem mostly occurs for selfbot clients but in my case client is bot. Would be glad for any help
You have to enable the message intents on https://discord.com/developers/applications
and need to pass them to your commands.Bot
bot = commands.Bot(command_prefix="!", intents=discord.Intents.all())
would be an example of how you can do that.
When I give the bot a command, it doesn't reply.
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix=">")
#bot.event
async def on_ready():
print("Bot is ready!")
#bot.command()
async def hello(ctx):
await ctx.send("Hello!")
bot.run(TOKEN)
The on_ready function works perfectly, displaying "Bot is ready!" in the console, however, when I type ">hello" in the discord channel, it doesn't reply.
Make sure the Bot has enough permissions to send messages.
To do so. Go to https://discord.com/developers/applications then click your application, head to the OAuth2 tab, then you will see several choices on Scopes find the bot and then Check it. After clicking the Bot you'll see a new tab on which below are all of the permissions. Click the permission(s) you desire, then copy the link that appears... Lastly reinvite the bot.
This should resolve the error, however if have more question, please comment them down.
Hello I am creating a discord bot i was trying to add single command but bot doesn't respond to any commands
something like
Bot = commands.Bot(command_prefix="!")
#Bot.commands()
async def ping():
print("Pong!")
this thing should respond when I type !ping in to discord client it should print pong in to terminal
but nothing nothing at all
I have tried Bot.add_command(ping) but it says command it's aleady registered i have no idea..
From what I've read from the discord.py docs you should code it like this:
Bot = commands.Bot(command_prefix="!")
#Bot.commands()
async def ping(ctx):
ctx.send("Pong!")
When you use 'print' you print the answer on your idle terminal. 'ctx.send' print the answer on discord chat. And every function on discord.py needs and argument.
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.