I came back to creating bots on discord, but this time with Python, it seems I am doing something wrong, and I don't know what it is.
I placed a simple code, which is (command-reply), but the bot does not answer and does nothing about it.
My bot is online, I don't get errors in my terminal, and I am using the correct token.
Here is my code:
import discord
intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)
#client.event
async def on_message(message):
if message.content == "hello":
await message.channel.send("Hello there!")
client.run('BOT TOKEN')
You didn't enable the message_content intent.
Docs about intents: https://discordpy.readthedocs.io/en/stable/intents.html
Related
My discord bot is not responding to "test". There are no error messages, so I am very confused why this is happening.
I just started Python today and I wanted to learn how to make a Discord bot
import discord
client = discord.Client(intents = discord.Intents.default())
#client.event
async def on_ready():
general_channel = client.get_channel(1045161050024185899)
await general_channel.send('Bot Activated By-**GotYoHat** \nMade By **GotYoHat** using **Python**')
#client.event
async def on_message(message):
if message.content == 'test':
await message.channel.send('work')
client.run('the token is right here')
I searched the internet for an answer and I can't find a single answer online
You need to turn on intents.
Try to discord.Intents.all()
This is my first time using the Discord.py library. I have the following code:
import discord
from discord.ext import commands
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
bot = commands.Bot(intents=intents, command_prefix='$')
#client.event
async def on_ready():
print(f'Logged in as {client.user}!')
#bot.command()
async def say(ctx, arg):
await ctx.send(arg)
client.run('MY_TOKEN')
When I say "$say "hello world"" I don't get any error message, but I also don't get any response from the bot. Can someone tell me what the issue is, I'm sure it's a simple mistake. Thanks.
I figured out what I did wrong. I didn't need to include client and bot as separate things. I simply removed client = discord.Client(intents=intents) and the code ran fine (obviously I replaced every instance of 'client' with 'bot').
Discord bot (py) Dont't reply to messages on any servers but reply to direct dms
I build a discord Bot with python using discord.py an as I write the (example $hello), it reply only on private dms not on any servers messages. I olready gived the bot admin on the server but still not doing anything.
My py code is:
intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)
async def on_message(message):
if message.content.startswith('$hello'):
print(message.content)
await message.channel.send('Hello')
client.run(token)
Also it not write anything to the console if I send the message on a dc server.
There is no firewall rule what can cose the issue I olrady chacked it.
Make sure that the bot intents are toggled in the Developer Portal
If you had it enabled, you can try enabling all the intents and then putting this in the intents parameter
client = discord.Client(intents=discord.Intents().all())
The code will probably look like this
import discord
client = discord.Client(intents=discord.Intents().all())
async def on_message(message):
if message.content.startswith('$hello'):
print(message.content)
await message.channel.send('Hello')
client.run(token)
Just as a quick hint:
according to discords API reference to the "on_message()" function you need to set Intents.messages to True.
In your case this would be
intents.messages = True
I did not test this code, but it's what I read from discords docs.
In the future you might want to have a look at it https://discordpy.readthedocs.io/en/stable/
I hope I could help you with this quick answer :)
I'm playing around with the Discord API and noticed that I can't access the content of a message.
This is my code:
import discord
client = discord.Client()
#client.event
async def on_ready():
print(f'Logged in as {client.user}')
#client.event
async def on_message(message):
if 'My Name' in message.author.name:
print(f'Author: {message.author.name}')
print(f'Content: {message.content}')
print(f'Clean_Content: {message.clean_content}')
print(f'System_Content: {message.system_content}')
client.run(TOKEN, bot=False)
Note that the token and my username are kept private in this post for obvious reasons.
This is the output that I get, no matter the message:
Author: My Name
Content:
Clean_Content:
System_Content:
As you can see I have also tried the clean_content and system_content attributes. However, none of them show the actual message. I've also tried to use a bot account and that surprisingly worked, but I want this to work with my own account. Is the problem that Discord does not intent private clients to read messages or did I miss something fundamental?
As Matt mentioned, user bots are not supported. However, your problem may be related to intents instead. You might try using the following lines:
import discord
intents = discord.Intents.all()
client = discord.Client(intents=intents)
See also:
https://docs.pycord.dev/en/master/intents.html#privileged-intents
https://docs.pycord.dev/en/master/intents.html#message-content-intent
You can solve this by enabling the MESSAGE CONTENT INTENT for your bot in the Discord Developer Panel.
import os
TOKEN = os.environ['TOKEN']
import discord
# This example requires the 'message_content' privileged intent to function.
class MyClient(discord.Client):
async def on_ready(self):
print(f'Logged in as {self.user} (ID: {self.user.id})')
print('------')
async def on_message(self, message):
# we do not want the bot to reply to itself
if message.author.id == self.user.id:
return
if message.content.startswith('!hello'):
await message.reply('Hello!', mention_author=True)
intents = discord.Intents.default()
intents.message_content = True
def main():
client = MyClient(intents=intents)
client.run(TOKEN)
if __name__ == '__main__':
main()
This is an example with reply in the documentation
Discord selfbots are no longer supported, you might have to use discord.ext which has support for them still. It seems using discord.py message.content will always be empty.
Excuse me for my bad english :-)
I have a problem that a Discord bot I work on does not send private message to members after they join. I tried several solutions suggested in other Stack Overflow posts, but it doesn't work.
import discord
client = discord.Client()
#client.event
async def on_member_join(member):
print(f'Someone joined')
await member.send("You joined")
client.run("XXX")
But the function is never executed.
If I use the exact same code in a command like ?join, as in the following example, it works!
import discord
from discord.ext import commands
client = commands.Bot(command_prefix='?')
#client.command()
async def join(ctx):
member = ctx.message.author
print(f'Someone joined')
await member.send("You joined")
client.run("XXX")
So am I wrong thinking that on_member_join doesn't work anymore? What am I doing wrong?
Thanks for your time :-)
As of discord.py version 1.5+, Intents for bucket types of events must be stated by the bot prior to use. The event on_member_join needs the intent of discord.Intents.members to be set to True.
An easy way to do this is the following:
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix='?', intents=intents)
discord.py version 1.5 states that if the intents are not specified, it allows all intents except for Intents.members and Intents.presences.
More information on Intents can be found here: Gateway Intents in discord.py