Discord.py new member message - python

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.

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

Discord event on_member_join not working when a member joins the guild

I have an event in my discord bot that sends an embed to welcome a member when the join the guild. No errors are produced but the event does not seem to work for me.
Here is the code for the event:
#bot.event
async def on_member_join(member):
"""
The code in this event is executed every time a member joins the server
"""
embed = discord.embed(title=f'Welcome to {member.guild.name}',
description=f'{member.mention}, welcome to the server! \nMake sure to checkout the rules first. Enjoy your stay <3',
color=0x0061ff)
if member.guild.icon is not None:
embed.set_thumbnail(
url=member.guild.icon.url
)
await bot.get_channel(1047615507995562014).send(embed=embed)
I'm also using the following intents as well and have enabled them properly so I know that is not the issue with my code.
intents = discord.Intents.all()
intents.members = True
The reason you're getting an Error, is because the e in discord.embed is lowercase
embed = discord.embed(title=f'Welcome to {member.guild.name}',
description=f'{member.mention}, welcome to the server! \nMake sure to checkout the rules first. Enjoy your stay <3',
color=0x0061ff)
Correct version would, obviously, be:
embed = discord.Embed(title=f'Welcome to {member.guild.name}',
description=f'{member.mention}, welcome to the server! \nMake sure to checkout the rules first. Enjoy your stay <3',
color=0x0061ff)
your bot is probably missing a privileged intent,go to your bot on the discord developer portal, then turn the server members intent on

Discord bot (py) Dont't reply to messages on any servers but reply to direct dms

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 would like to make a discord bot that sends a message when a member goes online but I can't seem to make it work ? (python)

I would like to make it so when a member goes online my both will send a message in the server welcoming the member but I cannot find a way to do so if I could get the name of the user too it would help thank you.
Here is my code that is not working
Here's a snippet that currently works
import discord
intents = discord.Intents.default()
intents.members = True
intents.presences = True
client = discord.Client(intents=intents)
#client.event
async def on_member_update(before, after):
if after.status.name == 'online':
await client.get_channel(id=`channel_id`).send('Hello')
client.run(`your_token`)
on_member_update
Called when a Member updates their profile.
This is called when one or more of the following things change:
status
activity
nickname
roles
pending
This requires Intents.members to be enabled.
You also need to allow presences to do what you really want
Whether guild presence related events are enabled.
This corresponds to the following events:
on_member_update() (activities, status)
And finally do what this answer mentions

Discord.py rewrite get_member() function returning None for all users except bot

I made a stupid discord bot a few months ago to change my friends name every minute, but I updated it today and now the get_member function is returning none.
#client.event
async def on_ready():
print('bot is ready!')
status = discord.Activity(name="Sam be a loser", type=discord.ActivityType.watching)
await client.change_presence(activity=status)
name_change.start()
#tasks.loop(minutes=1)
async def name_change():
server = client.get_guild(id=584112137132048387)
user = server.get_member(376388277302984714)
global english_words
global wordNumber
wordNumber = wordNumber + 1
#changes nickname to current word
await user.edit(nick=f"{english_words[wordNumber]} lookin' ass")
print(f'Word number {wordNumber}: {english_words[wordNumber]}')
#updates number in file
file = open("currentWord.txt", "w")
file.write(str(wordNumber))
file.close
I tried the same function with every user in the server, but it returned none for all of them except when I put the bot's id in. I have no clue why this is happening since I didn't even edit this part of the code.
This is most likely due to the recent discord.py 1.5 update. You are required to configure your intents when you're declaring your Bot.
What are intents?
Version 1.5 comes with the introduction of intents. This is a radical change in how bots are written. An intent basically allows a bot to subscribe into specific buckets of events. The events that correspond to each intent is documented in the individual attribute of the Intents documentation. Here's an example:
import discord
from discord.ext import commands
intents = discord.Intents.default()
intents.members = True # Subscribe to the privileged members intent.
bot = commands.Bot(command_prefix='!', intents=intents)
You will also have to enable privileged intents for guild related events: Here
You can read more about intents: Here
You may be able to not use Intents, Privileged or otherwise, by not using get_member() and instead use
guild.fetch_member()

Categories

Resources