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

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

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)

How to create a event where it announces if there is a certain amount of members? (Discord.py)

So basically, my server is about to hit 100 members, and I wanna make a #client.event (if not event then command) where it detects when 100 members is hit, and if it is, it announces it in my announcement channel. I've searched everywhere for it but i've never gotten a result! Does anyone know?
You would really only need to check if the guild has 100 members if a new member joins (or leaves, but that would be a bit depressing). For that, you can use the on_member_join() event reference.
Make sure that member intents are enabled, both in the Discord Developer Portal (in the section called Bot, under Privileged Gateway Intents), as well as in your script.
import discord
from discord.ext import commands
# Enable member intents
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix="!", intents=intents)
CHANNEL_ID = 1234567890
#bot.event
async def on_member_join(member):
channel = await bot.fetch_channel(CHANNEL_ID)
if member.guild.member_count == 100:
await channel.send("100 Members! :partying_face:")
CHANNEL_ID would be the ID of the channel to send the celebratory message in.

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

'Member' object has no attribute 'hasPermission'

i wanted to make a bot to discord for delete messages easly. it works 3 months ago but now i got an error " 'Member' object has no attribute 'hasPermission' ". thanks for everyone to share opinion. have a nice day.
import discord
from discord.ext import commands
from discord.ext.commands import Bot
import asyncio
bot = commands.Bot(command_prefix = 'botcuk')
#bot.event
async def on_ready():
await bot.change_presence(activity=discord.Streaming(name="admin biseyler deniyor", url='https://www.youtube.com/watch?v=fw7L7ZO4z_A'))
if message.content.startswith('botcuksil'):
if (message.author.hasPermission('MANAGE_MESSAGES')):
args = message.content.split(' ')
if len(args) == 2:
if args[1].isdigit():
count = int(args[1]) + 1
deleted = await message.channel.purge(limit = count)
await message.channel.send('{} mesaj silindi'.format(len(deleted)-1))
It is throwing 'Member' object has no attribute 'hasPermission' this error because 'Member' does not have an attribute named 'hasPermission'. To fix your problem, I have re-written your code and also given some explanation below the code:
import discord
from discord.ext import commands
from discord.ext.commands import Bot
import asyncio
bot = commands.Bot(command_prefix = 'botcuk')
#bot.event
async def on_ready():
await bot.change_presence(activity=discord.Streaming(name="admin biseyler deniyor", url='https://www.youtube.com/watch?v=fw7L7ZO4z_A'))
#client.event # we need to add and check for a client event
async def on_message(): # we need to execute the command when we recieve an message so we define a function called 'async def on_message():'
if message.content.startswith('botcuksil'):
if message.author.guild_permissions.manage_messages: # we need 'author.guild_permissions.(permission)' intead of 'if (message.author.hasPermission('MANAGE_MESSAGES'))'
args = message.content.split(' ')
if len(args) == 2:
if args[1].isdigit():
count = int(args[1]) + 1
deleted = await message.channel.purge(limit = count)
await message.channel.send('{} mesaj silindi'.format(len(deleted)-1))
I have not tested this yet, but according to me, your problem: 'Member' object has no attribute 'hasPermission' should be solved.
What you have done was message.author.hasPermission('MANAGE_MESSAGES') but message.author has no attribute like hasPermission.
Summary of what I have done:
I have added a client event (#client.event) and an async function on_message().
What it does is it checks if it is getting an message or not. If it detects a new message in any of the servers it is invited to, it triggers the code in on_message().
As I said: What you have done was message.author.hasPermission('MANAGE_MESSAGES') but message.author has no attribute like hasPermission.
Instead of writing that, we can use message.author.guild_permissions.manage_messages. It checks if the message author's server's permissions include "manage_messages". Hope my solution fixes your problem.
Always happy to help!
-sqd mountains

Resources