discord.py join message embed not getting sent - python

I was just trying to code a welcome message for new members who join the server. I wanted to send an embed everytime a new member joins. However, the embed is not getting sent. Could someone please help me out?
This is my code:
async def on_member_join(member):
mention = member.mention
guild = member.guild
embed = discord.Embed(title="**New Member Joined!**", description=f"{mention} joined {guild}!", color = discord.Colour.purple())
embed.set_thumbnail(url=f"{member.avatar.url}")
channel = discord.utils.get(member.guild.channels, id=Channel_ID)
await channel.send(embed=embed)
Thanks!

In the new version of discord.py(1.5.x), there're some updates about Intents. Intents are similar to permissions, you have to define Intents to get channels, members and some events etc. You have to define it before defining the client = discord.Bot(prefix='').
import discord
intents = discord.Intents().all()
client = discord.Bot(prefix='', intents=intents)
Also, you have to activate Intents from your bot application in Discord Developer Portal.
If you want to get more information about Intents, you can look at the API References.

Related

Trying to Tag random users in a discord server with a command

This is my code, but no matter what I try it's only tagging the bot? Do you know what I'm doing wrong? I've spent hours on this and I've not been able to figure it out.
#client.command(pass_context=True)
async def test(ctx):
user = random.choice(ctx.message.channel.guild.members)
await ctx.send(f'{user.mention} Youre the best')
I'm trying to get it to tag any random user.
Most likely, you did not give the bot permission to receive users information (SERVER MEMBERS INTENT) on the site Discord Developers in the Privileged Gateway Intents section.
code is only mentioning the bot, which is executing the command!
Try this:
#client.command(pass_context=True)
async def test(ctx):
members = [member for member in ctx.message.channel.guild.members if not member.bot]
if not members:
await ctx.send("There are no non-bot members in this guild.")
return
user = random.choice(members)
await ctx.send(f'{user.mention} You're the best')
As #DK404 has already mention, you probably didn't enable the Server Members Intent on Discord Developers.
Also check if you have enabled the intent for your bot too. You can do that by adding the intents parameter to your bot instance:
client = discord.Client(..., intents = discord.Intents.all())
After you do that try running the bot again.
By the way ctx.message.channel.guild.members is the same as ctx.guild.members

How do you give every member in a discord server a role at one time discord.py

I'm trying to give all members in my server a role with this command, but it doesn't seem to work. It doesn't give the role to all the members, but just to the client. It also doesn't show any errors in the console.
#client.command()
async def assignall(ctx, *, role: discord.Role):
await ctx.reply("Attempting to assign all members that role...")
for member in ctx.guild.members:
await member.add_roles(role)
await ctx.reply("I have successfully assigned all members that role!")
Ensure that you have enabled the GUILD_MEMBERS Intent from your Discord Developer Portal and initialized your client with the proper intents.
from discord import Intents
from discord.ext.commands import Bot
client = Bot(intents=Intents.all())
•Go to https://discord.com/developers/applications
•Select your bot
•Go to the bot section
•Scroll down and enable all the intents(Technically you need only server members' intent but if you do all it will help you)
•Replace your client=commands.Bot(command_prefix="your_prefix") to
intents = discord.Intents.default()
intents.message_content = True
client=commands.Bot(command_prefix="your_prefix",intents=intents)

Discord.py new member message

So I have this to make a welcome message for new members.
#bot.event
async def on_member_join(member):
channel = bot.get_channel('channel_id')
await channel.send("insert message")
Does this work on bots? And will this work if the person joining is a real user?
Note: that bot.get_channel() takes int not str, if you've passed int then kindly check if you've enabled Intents for your bot in developers page and in your code.
On developers page: https://discord.com/developers/applications/{bot_id}/bot Enable server members intent in Privileged Gateway Intents.
On your code:
intents = discord.intents.default()
intents.members = True
bot = commands.bot(command_prefix=prefix, intents=intents)
Yes, it will respond to bots. Bots are actually a normal user, they just have a special status.
If you want to filter out bots, then just look at the member object, it contains a property that will tell you if it is a robot - member.bot.
So then you can write something like this:
#bot.event
async def on_member_join(member):
if not member.bot:
channel = bot.get_channel('channel_id')
await channel.send("insert message")
Edit PS:
I recommend making a test discord server if you don't have it yet, and you can test it directly.
And to test people's connections, simply leave it open to people without an account, and open the server invitation in an anonymous browser window.

discord.py can't change the channel's name

So recently I've tried to create a bot that could change the voice channel's name based on how many members are on the server, but I end up getting this error: await memberchannel.edit(name = ">> 👥ᴍᴇᴍʙᴇʀꜱ: " + guild.member_count) AttributeError: 'NoneType' object has no attribute 'edit'
Here's my code, I cannot figure out how to access the .edit attribute:
async def on_member_join(member):
guild = member.guild
memberchannel : discord.VoiceChannel = get(guild.channels, id=MemberCounterChannel)
await memberchannel.edit(name = ">> 👥ᴍᴇᴍʙᴇʀꜱ: " + guild.member_count)
I did the same thing in the on_member_remove function.
Events that use on_member_join or others related to member events, must require member intents to be enabled. This works to allow these events to run as they are private and should be used carefully.
Intents can be enabled from Discord developer portal, from there you only need to make sure you have enabled Member in intents within the "Bot" category. You'd then need to define and use the intents in your bot code in the section you define your bot or client:
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix='your bot prefix', intents=intents)
After you have enabled intents, the member events would work. With your code, memberchannel also isn't defined, nor is it a Discord parameter.
This would now get the channel you want to edit by it's ID and edit whenever a user joined to the current member count of the guild the user joined.
async def on_member_join(member):
member_channel = client.get_channel(channel id)
await member_channel.edit(name=f"Members: {member.guild.member_count}")
I had to toggle all of the intents in my application using discord developers panel and left the code like this (ConfigFile is a json file containing all of the variables):
memberchannel: discord.VoiceChannel = get(guild.channels, id=ConfigFile["MemberCounterChannelID"])
membercount = guild.member_count - 1
await memberchannel.edit(name="{0} {1}".format(ConfigFile["MemberCounterChannelName"], membercount))
Also I did the membercount = guild.member_count - 1 part because I only use this bot on my server and it's not made for commercial use so I don't really need it to actually count member-bots

Discord.py bot doesn't dm new members

I'm using the event on_member_join to attempt to dm new members, but when I tested with my alt, it didn't send a message.
#bot.event
async def on_member_join(member):
embed = discord.Embed(title="Welcome to my server!", description=None, color = discord.Color.magenta())
embed.add_field(name="To get started:", value="•Invite some friends!\n•Check out some of the channels and get engaged with the community!\n•Have fun!", inline=True)
channel = bot.get_channel(803703488499810326)
userDM = member.create_dm()
await userDM.send(embed=embed)
You don't need to do member.create_dm() because when you DM a user it's not needed.
You also don't need to define description to None, you simply don't even need to specify a description.
In order for the bot to even find the members, you need members intent turned on in your bot. You can do that using the image below.
Now, you just need to provide the code in the bot so that the bot can use the intents.
Here is the code that you need to add to your main bot file.
intents = discord.Intents.default() # Gets the default intents from discord.
intents.members = True # enables members intents on the bot.
bot = commands.Bot(command_prefix=prefix, intents=intents)
After all of that here is your fixed code.
#bot.event
async def on_member_join(member):
embed = discord.Embed(title="Welcome to my server!", description=None, color = discord.Color.magenta())
embed.add_field(name="To get started:", value="•Invite some friends!\n•Check out some of the channels and get engaged with the community!\n•Have fun!", inline=True)
await member.send(embed=embed)
I hope this helped, Have a nice day, Best of luck on your bot.
Here is a Discord Server for Beginners.
🔗 Discord.py For Beginners : https://discord.gg/C8zFM3D2rn

Categories

Resources