Welcome message - python

I tried to greet users on the server but nothing works.
This my code:
#client.event
async def on_member_join(member):
await client.get_channel(918604938085548064).send(f"{member.name} has joined")
I also tried code like this:
#client.event
async def on_member_join(member):
# check if the guild the member is joining is equal to your guild's id
if member.guild.id != GUILD ID:
return
# if it's the same guild id, it continues
embed = discord.Embed(color=0x6c5ce7)
# etc, other code
I thought that this code was enough, but I never received a message or an error in the console.
Console output:

If you don't get any message or an error then it's probably happening because you didn't enable Privileged Gateway intents and your bot doesn't get the member. As you can see in the docs - on_member_join requires intents.Members.
Enable Server Members Intent on the Discord Developer site in the Bot section.
Then, add something like this to your code:
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix="!", intents=intents)

Related

discord bot does not print message.content

I am using the following code to print the latest message on a specific discord channel, but it always brings an empty string.
from termcolor import colored
import discord
intents = discord.Intents.default()
intents.members = True
intents.messages = True
client = discord.Client(intents=discord.Intents.all())
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
#client.event
async def on_message(message):
# Check if the message was sent in the specific channel you want to read from
if message.channel.id == CHANNELID: # replace CHANNEL_ID with the ID of the channel you want to read from
print(colored(message.content, 'green'))
client.run('TOKEN')
Any ideas?
Message Content Intent is correctly enabled on the application, and the bot has read message and read message history permissions on the channel.
this is the case if your bot dont have the intents setup.
Go to the Discord Developer Portal and to your Bot, then go to Bot and go to the Privilege Gateway Intents and check the Message Content Intent
you have already specified the intents in your code so this should do it

discord.py :-My Bot is jut not responding and either no error is there in compiler

I just started learning discord.py but my bot isn't responding even though I followed the documentation and no error messages. The bot is online but doesn't respond to my messages
I was attempting to do this from a tutorial
import os
import discord
client = discord.Client(intents=discord.Intents.default())
#client.event
async def on_ready():
print('We have logged in as {0.user}'
.format(client))
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$hello'):
await message.channel.send('Hello!')
client.run(os.environ['TOKEN'])
Please Someone Help me! :(
Your bot can't read the content of the messages sent in a guild because it is missing the message_content intent.
You gave your bot only the default intents, which doesn't include the message_content one.
client = discord.Client(intents=discord.Intents.default())
Here is how you fix it:
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
In case your bot isn't still able to read the content of the guild messages, you need also to turn on the message content intent in the Discord Developer Portal:
Select your app
Go to the "Bot" tab
Turn the "message content intent" on
Now your bot should be able to read the content of the messages sent in guilds.
Here you can read more about Intents

Welcoming new members not working with discord.py

I'm trying to setup a simple discord.py enabled bot and having trouble welcoming new members. I have the following code and when a new member joins, the bot is able to process the default welcome message Discord sends but doesn't process anything in the on_member_join() function. I've enabled intents within Discord (both gateway and member) and still can't figure out why it won't process a new member joining. I've also tested with a brand-new created member and still won't trigger.
import discord
intents = discord.Intents.default()
intents.members = True
client = discord.Client()
#client.event
async def on_ready(): # When ready
print('We have logged in as {0.user}'.format(client))
#client.event
async def on_member_join(member):
print('Someone new!')
await member.send("Welcome")
#client.event
async def on_message(message): # On every message
if message.author == client.user: # Cancel own message
return
if message.content.startswith('?'):
await message.channel.send('Command')
client.run(DISCORD_TOKEN)
This is not related to using Client instead of Bot.
You created the intents variable, but you're never using it. You're supposed to pass it into your discord.Client(), which you aren't doing, so the members intent will always be disabled.
# Original question
intents = discord.Intents.default()
intents.members = True
client = discord.Client() # <-- You're not passing intents here! The variable is never used so intents are disabled
That's also why your answer fixes it: because you're actually using your intents (...intents=intents...).
# Your "fix"
client = commands.Bot(command_prefix = "!", intents = intents) # <-- Notice the intents
Using discord.Client or commands.Bot has no influence on this: commands.Bot without passing intents wouldn't do anything either.
# This would cause the exact same issue, because intents aren't used
client = commands.Bot(command_prefix = "!")
Passing your intents into the Client would also work, just like that fixes the problem for your Bot.
client = discord.Client(intents=intents)
EDIT: See answer. Changing the below correctly passed intents (with members = True) to the bot.
Unsure why but I needed to use bot commands in order to resolve the issue.
Added:
from discord.ext import commands
and changed
client = discord.Client()
to
client = commands.Bot(command_prefix = "!", intents = intents)
I think this is what was required to 'fully enable' intents, but I'm not fully sure why this corrected the issue.

Discord Bot can only see itself and no other users in guild

I have recently been following this tutorial to get myself started with Discord's API. Unfortunately, when I got the part about printing all the users in the guild I hit a wall.
When I try to print all users' names it only prints the name of the bot and nothing else. For reference, there are six total users in the guild. The bot has Administrator privileges.
import os
import discord
TOKEN = os.environ.get('TOKEN')
client = discord.Client()
#client.event
async def on_ready():
for guild in client.guilds:
print(guild, [member.name for member in guild.members])
client.run(TOKEN)
Enable the server members intent near the bottom of the Bot tab of your Discord Developer Portal:
Change the line client = discord.Client() to this:
intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)
This makes your bot request the "members" gateway intent.
What are gateway intents? Intents allow you to subscribe to certain events. For example, if you set intents.typing = False your bot won't be sent typing events which can save resources.
What are privileged intents? Privileged intents (like members and presences) are considered sensitive and require verification by Discord for bots in over 100 servers. For bots that are in less than 100 servers you just need to opt-in at the page shown above.
A Primer to Gateway Intents
Intents API Reference
As of discord.py v1.5.0, you are required to use Intents for your bot, you can read more about them by clicking here
In other words, you need to do the following changes in your code -
import discord
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD = os.getenv('DISCORD_GUILD')
intents = discord.Intents.all()
client = discord.Client(intents=intents)
#client.event
async def on_ready():
for guild in client.guilds:
if guild.name == GUILD:
break
print(
f'{client.user} is connected to the following guild: \n'
f'{guild.name} (id: {guild.id})'
)
# just trying to debug here
for guild in client.guilds:
for member in guild.members:
print(member.name, ' ')
members = '\n - '.join([member.name for member in guild.members])
print(f'Guild Members:\n - {members}')
client.run(TOKEN)

Discord.py on_member_join not working, no error message

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

Categories