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

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

Related

get the number of users in a voice channel with my discord bot in python

I'm looking for a way to get the number of users in a voice channel and to get their username to use it in one of my discord bot command, I found that I can use ctx.guild.member to get the members of users in the server, but not in a specific channel, is there a method to do that ?
I tried this
from discord.ext import commands
bot = commands.Bot(intents=discord.Intents.all(),command_prefix = "/")
#bot.command()
async def teamup(ctx):
channel = ctx.author.VoiceChannel
Or this
bot = commands.Bot(intents=discord.Intents.all(),command_prefix = "/")
#bot.command()
async def teamup(ctx):
channel = ctx.message.author.voice.channel
None of them works, the first tells me that member objects has no attribute VoiceChannel
and the second one tells me that ctx.message.author.voice is a NoneType object and has no attribute channel
You're very close.
#bot.command()
async def teamup(ctx):
channel = ctx.message.author.voice.channel
ctx has an author property; this returns the user/member object for the individual that invoked the command. The user/member has a voice property that returns the VoiceState object of the user. This has a channel property which will return the voice channel that the user is in. Importantly, this will be None if the user isn't in a voice channel. So, putting it together:
#bot.command()
async def teamup(ctx):
voice = ctx.author.voice
if not voice.channel:
await ctx.send("You are not currently in a voice channel")
return
channel = voice.channel
# do what you want with the channel here
await ctx.send(f"You're in {channel.name}!")

How do I get a full list of members of a discord server using python?

I try to get a full list of members for the server that the bot is in, but I only end up getting the bot information.
Here is what I was doing.
#client.event
async def on_message(message):
if message.content.startswith('-members'):
for guild in client.guilds:
for member in guild.members:
print(member)
Looks like you're missing intents
You need discord.Intents.members set to True in order to get the information you seek.
intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents = intents)

DMing friends discord.py

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.

guild.get_member_named(name) is returning 'NoneType' inspite of existence of such member

I have enabled intents all through my code and developer portal;got necessary permissions for the bot to 'see' the other members but still the method returns NoneType for almost all users(only exception is Dyno)
Now I need a complete check through my program and the permissions and settings
if it doesn't get sorted after the check then the question topic gets adjusted accordingly
import discord
import os
from discord.ext import commands
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix='!',intents=intents)
#bot.event
async def on_ready():
print('We have logged in as {0.user}'.format(bot))
#bot.command()
async def ping(ctx,args1):
guild = bot.get_guild(ServerID)
member = guild.get_member_named(args1) #getting NoneType here
await ctx.send('member.mention') #Assuming the existence of member coz I know there is such one
bot.run(os.getenv('TOKEN'))
Try using
discord.utils.get(guild.members, name='theusernamehere') #replace"theusernamehere" with the person username
To get by id use this
discord.utils.get(guild.members, id='idhere') #replace "idhere" with the person id
or you can just use arg1: discord.Member

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.

Resources