trying to get online members list in a embed in discord.py - discord

how i can get online members list in a embed in discord.py?
i tried but it shows only one member at time:
#tasks.loop(seconds=60)
async def sendmessage():
channel = client.get_channel(channel_id)
for guild in client.guilds:
for members in guild.members:
if members.status != discord.Status.offline:
embed = discord.Embed(title="who is online ?" , description="" , color=0x00ff00)
embed.add_field(name="server :" , value=guild.name)
embed.add_field(name="online :" , value=members.name)
await channel.send(embed=embed)
get all the online members in a embed

You're currently looping over them and creating + sending a separate embed for every member.
for member in guild.members:
embed = Embed(...)
...
await channel.send(embed=embed)
Instead, create a list in the loop, join it to a string, and send it once below the loop.

Related

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)

discord.py sending message to channel name instead of ID

I am a beginner to this programming stuff, and I have a quick question. I am trying to make a logs channel for multiple servers, and I want to be able to look for a channel that has the word “logs” in it, so can you help me?
Code:
#client.event
async def on_command_completion(ctx):
channel = client.get_channel('829067962316750898')
embed = discord.Embed(colour=discord.Color.green(),
title="Command Executed")
embed.add_field(name="Command:", value=f"`,{ctx.command}`")
embed.add_field(name="User:", value=f"{ctx.author.mention}", inline=False)
embed.add_field(name="Channel:",
value=f"{ctx.channel} **( <#{ctx.channel.id}> )**")
await channel.send(embed=embed)
You need to get the channel. I suggest using discord.utils.get.
Also, try using {ctx.channel.mention} to mention the channel instead.
#client.event
async def on_command_completion(ctx):
channel = discord.utils.get(ctx.guild.text_channels, name='logs')
if channel is None:
return # If there is no logs channel, do nothing (or what you want to do)
embed = discord.Embed(colour=discord.Color.green(),
title="Command Executed")
embed.add_field(name="Command:", value=f"`,{ctx.command}`")
embed.add_field(name="User:", value=f"{ctx.author.mention}", inline=False)
embed.add_field(name="Channel:",
value=f"{channel.name} **( {channel.mention} )**")
await channel.send(embed=embed)
See the discord.TextChannel docs and discord.utils.get()

Member Count Channel discord.py

The program should work in such a way that a channel named "members" will display the number of members on the server, but the program does not give errors and does not work itself.
Thanks in advance!
async def on_member_join(member):
guild = member.guild
channel = get(guild.channels, name = 'members')
await channel.edit(name = f'Учатники: {guild.member_count}')
#bot.event
async def on_member_remove(member):
guild = member.guild
channel = get(guild.channels, name = 'members')
await channel.edit(name = f'Учатники: {guild.member_count}')
I am not really sure if you have it in your program but just to be sure, define what is the attribute named "channel" so it knows what to edit, you can use get_channel to do it and put the channels ID afterward inside it ( https://discordpy.readthedocs.io/en/latest/api.html?highlight=get_channel#discord.Client.get_channel)
Maybe just try and use it the old way and use name = "Учатники: " + str(guild.member_count) (The member_count gives you an output of int so you may need to turn it into a string before displaying it.
(I have not tested anything and this answer is based on experience and reading documents and also you might want to take a look at https://discordpy.readthedocs.io/en/latest/api.html?highlight=member_count#discord.Guild.member_count)

(Discord.js) Trying to get a channel id based on the name of the channel, and post a message in it

I'm trying to set up a server with a bunch of text channels named after users so that I can read their DMs to the bot. I can't figure out how to find if a text channel with that person's tag exists already, and I'm not sure if this is the correct code to do this.
Here is the code I'm trying to use:
try{
var txtChannel = client.guilds.cache.get(dmServerID).channels.find(channel => channel.name === (mesage.author.tag.replace('#','-')) && channel.type === "text");
}
catch(e){
client.guilds.cache.get(dmServerID).channels.create(message.author.tag.replace('#', '-'), 'text');
}
txtChannel.send(message.author.tag + ": " + message.content);
After running this code, it gives me an error that reads: Cannot read property 'send' of undefined.
I would also like to know if I am able to create the text channels inside of a category.
First of all, use let over var.
The error is caused by the fact that you declare txtChannel inside the try so it is not available outside that block of code.
Also, you forgot channels.cache (if you are using discord.js v12).
Here is what the code should look like:
let txtChannel = null;
// Already get the guild from the cache since we are gonna use it a bit
const guild = client.guilds.cache.get(guildID);
// Find a text channel that includes in the name the tag of the user
txtChannel = GUILD.channels.cache.find(c => c.type === "text" && c.name.includes(message.author.tag.replace('#','-'));
// If the channel doesn't exist we create the channel, using await to wait until the channel gets successfully created
if(!txtChannel) txtChannel = await GUILD.channels.create(message.author.tag);
// At the end we send the message in the gotten / created channel
txtChannels.send("the message to send");
Note that this code needs to be inside an async function, you can read more about that here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function

Trouble with bot.wait_for() Discord.py

So modern documentation on the bot.wait_for() coroutine is not super detailed, and I'm having trouble getting it to work with reactions. Would appreciate feedback.
Python 3 with Discord.py
## Test Role Add
#kelutralBot.command(name='testreaction')
async def testReaction(ctx):
member = ctx.message.author
message = await ctx.send("This is a test message.")
emojis = ['\u2642','\u2640','\u2716']
for emoji in emojis:
await message.add_reaction(emoji)
def check(reaction, user):
return user == message.author and str(reaction.emoji) == '\u2642'
try:
reaction, user = await kelutral.wait_for('reaction_add', timeout=60.0, check=check)
except asyncio.TimeoutError:
await ctx.send("Window has passed to self-assign pronouns. Please DM a mod if you would still like to do so.")
else:
print(reaction)
male = get(member.guild.roles, name="He/Him")
await member.add_roles(male)
print("Assigned " + member.name + " He/Him pronouns.")
Two things were wrong.
First, don't use Client and Bot in the same command. Bot is sufficient for both.
Second, Unicode Emoji for Discord are treated as '\U000#####', which was the biggest problem.
Once we solved that, everything worked as intended.
The problem here is that you are checking if the person who sent the reaction is the author of the message that contains the message, which is only satisfied by the bot that reacted. Also consider using a role ID instead (server settings > roles > left click role > right click role > copy ID). You also want to be consistent with kelutral or kelutral Bot throughout the command.
#kelutralBot.command(name='testreaction')
async def testReaction(ctx):
member = ctx.message.author
message = await ctx.send("This is a test message.")
emojis = ['\u2642','\u2640','\u2716']
for emoji in emojis:
await message.add_reaction(emoji)
def check(reaction, user):
return user == member and str(reaction.emoji) == '\u2642' # check against the member who sent the command, not the author of the message
try:
reaction, user = await kelutralBot.wait_for('reaction_add', timeout=60.0, check=check)
except asyncio.TimeoutError:
await ctx.send("Window has passed to self-assign pronouns. Please DM a mod if you would still like to do so.")
else:
print(reaction)
male = ctx.message.guild.get_role(ROLE_ID_GOES_HERE) # put your role ID here #
await member.add_roles(male)
print(f"Assigned {member.name} He/Him pronouns.")
Keep in mind your code only works for the "male" role, you have to implement a different check function to use it for everything.

Resources