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.
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.
basically i have no idea why this code is not working, ive tried using on_message and that works so long as i dont include and if statement to filter for the messages with the prefix at the start. so i scrapped that, this code worked a few months ago with a different bot i made and ive ended up scrapping all the other stuff i was doing and bringing it down to the bare basics because i cant figure out why the bot doesnt recognise messages starting with the prefix.
import discord
import os
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
TOKEN = "Token_here"
#bot.command(name='tst')
async def test(ctx):
await ctx.send('testt')
#bot.event
async def on_ready():
print ('Successful login as {0.user}'.format(bot))
bot.run(TOKEN)
ive tried print('Test') debugging aswell see below
#bot.command(name='tst')
async def test(ctx):
print('Test")
then in discord typing the !test command still does nothing and the terminal also remains empty other than the on_ready result of
Successful login as botname#0001
i have no idea whats going on honestly
There are a couple of things that could be causing your problems. I fixed the issue by enabling intents for the bot. This also must be done on the developer portal. https://discord.com/developers/applications
intents = discord.Intents().all()
intents.members = True
bot = commands.Bot(command_prefix="!", intents=intents)
you also don't need the name parameter in the command decorator. It can just be written as :
#bot.command()
async def test(ctx):
await ctx.send('testt')
Try these changes:
bot= commands.Bot(command_prefix='!!', intents=discord.Intents.all())
#bot.command()
async def test():
await ctx.channel.send("Test successful")
I am trying to build a Discord bot; which tells you about the weather condition. But no matter what I did I was unable to execute even the simplest commands such as:
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix="!")
#bot.command()
async def test(ctx, arg):
await ctx.send(arg)
client = discord.Client()
client.run(token)
I just do not get it on Discord side I enter the command "!test hello" to the chat screen and nothing happens. Please help thanks !
The problem is, that you first define a bot using bot = commands.Bot(command_prefix="!") then add the command to the bot, but then create a new client using client = discord.Client() and then run the client.
The problem here is that, you never run the bot, which has the command, so instead of
client = discord.Client()
client.run(token)
use
bot.run(token)
I am trying to make a discord bot with the Discord.py library. The commands with the #client.command() decorator work fine for me, but none of the event ones that I tried work.
#client.event
async def on_member_join(member):
channel = client.get_channel(ChannelId) #I did define channel Id in my code
await channel.send("someone has joined")
#client.event
async def on_member_remove(member):
print("Someone has left")
I would expect this to output to the terminal or in the channel id I put in, but nothing appears, not even an error message.
*I used client. for all functions.
*I am doing this on mac.
Im not quite sure why its working, I do not get any error messages, and I cant seem to find anyone else with this problem.
Thanks in advance
With version >1.5.0 you can do something like this:
import discord
intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)
#client.event
async def on_member_join(member):
await member.send("Welcome!")
You also need to enable intents in the developer portal in https://discord.com/developers/applications. Bot > Bot > Presence & Server Members Intents > Toggle On
import discord
intents = discord.Intents.all()
discord.member = True
bot = commands.Bot(command_prefix="[",intents = intents)
and you need to go to developer portal --> applications(select your bot)
and under the setting have a bot. Click it under the page has PRESENCE INTENT and SERVER MEMBERS INTENT you need to open it. That will be working.
If you are using discord.py v1.5.0, see the docs for Gateway Intents
I made a simple discord.py bot that reacts whenver it is PM'd. However, if someone messages it while my bot is offline, it will not react. How can i make it display all messages received while it was online on startup?
Current code:
import discord
from discord.ext import commands
import asyncio
client = commands.Bot(command_prefix='.')
#client.event
async def on_message(message):
if message.guild is None and message.author != client.user:
send_this_console=f'{message.author}: \n{message.content}\n'
print(send_this_console)
await client.process_commands(message)
You'll need to use message history and track the last message sent.
To get when a message was created:
lastMessageTimestamp = message.created_at
You'll need to store this in whatever way you want when your bot is offline (for example with pickle) and do this:
async for message in user.history(after = lastMessageTimestamp):
print(message.content)