Member always appears offline - python

I want to make a profile command that shows member status(online, offline, etc.).
#client.command(passContent=True)
#commands.has_role("🍿║Участники")
async def профиль1(ctx, member: discord.Member = None):
await ctx.send(f'your status is {ctx.author.status}')
When someone uses this command he always appears offline. ("your status is offline")

You need to use Intents!
Intents are new in version 1.5 of discord.py
Activate Intents on the discord developer portal
Add this to the top of your code
intents = discord.Intents().all()
client = commands.Bot(command_prefix = '?',intents=intents)
# OR
bot = commands.Bot(command_prefix='?', intents=intents)
Then everything should work fine.
You can also join the discord.py server via this link

Related

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 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

Is there a way to list all members of a discord server with discord.py

I want to retrieve a list with all people which are part of the discord server.
import discord
from discord.ext import commands
TOKEN = 'my_token'
bot = commands.Bot(command_prefix = '!')
#bot.command()
async def members(ctx):
for member in ctx.guild.members:
await ctx.send(member)
bot.run(TOKEN)
Whenever using !members I just get a List of all members which are currently in a voice channel or in some way active on the server where but I'd like to get those just shown as online or even offline.
Does someone have any idea?
Since last updates you have to enable intents. Enable them from the dev portal and code it self.
intents = discord.Intents().all()
bot = commands.Bot(command_prefix="$", intents=intents)
Remove that line with server = bot.get_guild(SERVER_ID)
Guild.members will return a list of members in the guild.
Since >=1.5.0 requires elevated intents for Member/Presence events, you must enable the Member intents in the developer portal and in your code.
You're also checking for the number of members in which the command is invoked, so you would want to use the guild_only() decorator and remove the line with bot.get_guild()
Reason for having the guild_only() decorator is because if you invoke !members in a DM, guild would be None and will raise an error saying that 'NoneType' object has no attribute called 'members'

discord.py join message embed not getting sent

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.

Why won't Discord bot recognize commands?

I'm making a bot for a pokemon server, and I'm trying to make a command that will give the 'Gym Leader' role to another user. I try using the command, and using the test command, but there is no response in the server nor the shell.
import os
import discord
from dotenv import load_dotenv
from discord.ext import commands
from discord.utils import get
bot = commands.Bot(command_prefix='b!', case_insensitive=True)
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
client = discord.Client()
#client.event #works
async def on_ready():
print(f'{client.user.name} has connected to Discord!')
channel = client.get_channel(697500755766018098)
#client.event #works
async def on_member_join(member):
await member.create_dm()
await member.dm_channel.send(
f'Hi {member.name}, welcome to Pokémon Beast Ball!\n\nThis server utilizes Pokecord and Mewbot.\n\nSay \'pkhelp\' in the server to learn about Pokecord commands.\nSay \';help\' in the server to learn about Mewbot commands.'
)
#bot.command() #doesn't work
async def test(ctx):
print("test recieved")
await ctx.send(ctx)
#bot.command(pass_context=True) #this is the command that really needs help
async def newleader(ctx: discord.User=None):
print("command recieved")
if not user:
await ctx.send("Invalid")
print("1")
else:
role = discord.utils.get(ctx.guild.roles, name="Gym Leader")
role2 = discord.utils.get(ctx.guild.roles, name="Purple UniTurtle Man")
if role in ctx.author.roles or role2 in ctx.author.roles:
print("2")
await ctx.send(f'A new Gym Leader has been appointed')
await user.add_roles(role)
await bot.remove_roles(ctx.author, role)
else:
print("3")
await ctx.send("You do not have permission to use this command")
client.run(TOKEN)
You are mixing bot and client and your client = discord.Client() is stepping on your bot = commands.Bot(...) statement. Since you want to do commands and events you only use the commands.Bot(...) statement.
Remove the client = discord.Client() statement and change your #client.event decorators to #bot.event.
Also if you want to reference the command context in your test command update it with the ctx parameter async def test(ctx):.
That will get you started using your commands and entering b1test will now work.
Please note that the case_insensitive=True on the commands declaration refers to the command name and not the prefix.
Did you check :
bot connection → make a "on_connect" event (not exactly the same as "on_ready") to see if your bot successfully connect to your server (before receiving data from discord). If not, try to add your bot again to your server and check if all tokens are goods.
bot permissions (if your bot have the right to write in channel, read messages from channels, manage roles) → if your bot cannot read messages, he can't read commands !
role priority (you can't manage roles highers thant yours) → go to "server settings" > "roles" > put your bot role above the 'Gym Leader' role (or at the top of the list if you don't care).
The problem isn't actually what the selected answer suggests. There is probably no reason to use both commands.Bot and discord.Client but using both won't lead to that issue.
The issue is because you are only running the client, not the bot. You need to also run the bot instance if you want it to function.
If you are not trying to do something specific, then just using either bot or client will suffice anyway, so that part of the selected answer was helpful in avoiding the issue at least.

Categories

Resources