DMing friends discord.py - discord

What I want to do is when the bot starts up, grab the friends list and DM everyone on it. I can't use ctx as on_ready doesn't support it. My code so far:
#bot.event
async def on_ready(msg):
for x in bot.user.friends:
e = x.id
await x.send('hi')

Discord bots can't have friends, but you can have a list of people that you can DM a message to. This is an example:
#bot.event
async def on_ready():
for friend in [listofuserids]:
await friend.send("hi")
listofuserids should be a list of your friends' Discord IDs. For more information on how to get a user's Discord ID, read this.

Related

Welcome message Discord.py

I don't have much experience in discord.py, but I'm making a bot. Right now I added the welcoming message:
#client.event
async def on_member_join(member):
channel=client.get_channel(channel_ID)
emb=discord.Embed(title="NEW MEMBER",description=f"Thanks {member.name} for joining!")
await channel.send(embed=emb)
but I want to mention the user who joined as this picture
May you help me doing this? Thanks!
Use member.mention
This returns a string which allows you to mention the member.
#client.event
async def on_member_join(member):
channel=client.get_channel(channel_ID)
emb=discord.Embed(title="NEW MEMBER",description=f"Thanks {member.mention} for joining!")
await channel.send(embed=emb)
You should however keep in mind that because of caching, the mention will most likely look something like this <#!123456789>.
You need the code to look like this:
#client.event
async def on_member_join(member):
if member.guild.id !=Guild.id:
return
channel = client.get_channel(channel_id) # replace id with the welcome channel's id
await channel.send(f"{member.mention} has arrived!, check out our announcments channel for server and bot announcements!")
await member.send(f"Thank you for joining {member.guild.name}!")
This line right here is you only want people that join YOUR server to send a msg, if you don't have any server with your bot in it, will say the msg still
if member.guild.id !=Guild.id:
return
{member.mention} is what mentions the user if you are using {} in the sending part you need the f function
Next code will make the code call the {}
await channel.send(f"{member.mention}")
This calls the guild name:
{member.guild.name}
I am not sure about client.get_channel, but you sure can try this:
#client.event
async def on_member_join(member):
guild = client.get_guild(serverID)
channel = guild.get_channel(channelID)
emb = discord.Embed(title="NEW MEMBER",description=f"Thanks {member.name} for joining!")
await channel.send(member.mention, embed=emb)

discord.py how to make the bot ban someone if they have a certain word in their name

I was wondering how I could ban someone if they had someone in their name. For example, I have people who join with the n word in their name and I wanted to know how I could ban them for having that in their name.
You can use the discord.Member object given by on_member_join to get the user's name, and filter names that way.
import discord
intents = discord.Intents.default()
intents.members = True #So you can use on_member_join event
client = discord.Client(intents=intents)
banlist = []
#client.event
async def on_member_join(member):
if any(banned in member.name for banned in banlist):
await member.ban()
return

Is there a way to make a discord bot that sends a user a message the mentioned number of times?

I've been trying to make a discord bot with discord.py. I know how to make the bot send a message to the specified user, but I was wondering, is there a way to send the message a given number of times? If so, how can I do it?
As an example, if the user types !dm #discorduser hello 5, the bot will send "hello" to the specified user 5 times.
So far, my code is:
import discord
from discord.ext import commands
client = commands.Bot(command_prefix='.')
#client.event
async def on_ready():
print('Bot is ready.')
#client.command()
async def spam(ctx, member: discord.Member, *, content):
channel = await member.create_dm()
await channel.send(content)
client.run('bot token')
Here's my answer for the question, from my understanding you're trying to Direct Message a user x amount of times, and x can be changed depending on what the user wants.
#client.command()
# the arguments are: member, the amount of times you want to DM, and what you want to DM them.
async def spam(ctx, member : discord.Member, amount=1, content=None):
# this loop will keep doing the script below the amount of 'amount' times.
for i in range(amount):
channel = await member.create_dm()
await channel.send(content)

Discord.py welcome bot on_member_join event not getting callded

I've been interested in working with discord bots lately, and from what I'm seeing this code should work but it is not...
I'm simply just playing around with the API because it's fun so I'm pretty new with this. I just want the bot to welcome someone when they join.
import discord
client = discord.Client()
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
channel = client.guilds[0].get_channel(CHANNEL ID)
await channel.send("Bot online")
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$hello'):
await message.channel.send('Hello!')
#client.event
async def on_member_join(member):
print(f'{member.name} has joined the server')
channel = client.guilds[0].get_channel(CHANNEL ID)
print(channel)
await channel.send(f'{member.name} has joined the server')
#client.event
async def on_member_remove(member):
print(f'{member.name} has left the server')
channel = client.guilds[0].get_channel(CHANNEL ID)
print(channel)
await channel.send(f'{member.name} has left the server')
client.run('TOKEN HERE')
instead of member.name use member and if that dosnt work add this where you have your variables intents = discord.Intents(messages = True, guilds = True, reactions = True, members = True, presences = True)
client = commands.Bot(command_prefix = '*', intents = intents)
FOR discord.py >= 1.5
1.5 adds support for Gateway Intents, by default your bot doesn't have access to guild members like it did in previous versions. If your bot is in less than 100 servers then you can enable these intents without verification. You should see these settings at the bottom of your Bot page on the Discord Developer Portal. If you enable both, then you need to change your Client (or commands.Bot) instantiation as such:
intents = discord.Intents.all()
client = discord.Client(intents=intents)
When I test this, the event is firing, but I'll bet your issue lies in the line:
channel = client.guilds[0].get_channel(CHANNEL ID)
It will be far more reliable to just use use client.get_channel to ensure you are actually grabbing the intended channel, all channel IDs on discord are unique so you do not need to use a guild object. Also CHANNEL ID will not be a valid variable, but I am guessing you just redacted it.
You forgot the brackets for the decorators.
#client.event
should be
#client.event()
EDIT: Not it, apparently. Will update this answer when OP replies with more information.

How to check if my discord bot already dmed a person and it wont dm the person again (discord.py)

I have a discord bot that handles with alts, i'm looking for a way that my bot knows if he dmed the person already before (explaining why he was kicked) and it wont dm them again. My function is like this:
#client.event
async def on_member_join(member):
channel = member.guild.text_channels[0]
if something
await channel.send(f"**{member.display_name}** was kicked")
await member.send("**Hi, your account was kicked due to reason** \n"
"**please try again later!**\n"
f"**{member.guild.name}.**")
await member.kick(reason=None)
else:
pass
My problem is that every time someone is kicked my bot dms them and I want it to dm the user kicked only once in their lifetime (without saving which user was dmed before).
would like to get help :)
You could have a look at this but you should at least save their id's to a text file.

Resources