Get all connected members of discord channel - python

I'm working on a move command, which moves members with the role "Officer" into another channel. But I'm struggeling to get all members in the channel. The most logical solution would be:
When I print(members) I just get [] although I was connected to the channel.
I tried the .keys() solution too but i dont get the object back, but my ID in a dictionary. Then I tried get_member but the result is always None.
guildG is a global variable and set in on_ready() as follows:
My intents:

Added the intents to the bot constructor. Thats the trick :)

Related

Member Avatar doesn't appear in the welcome embed

I am trying to make my bot send a welcome message when someone joins a specific server.
Code:
if member.guild.id == 928443083660607549:
new = nextcord.utils.get(member.guild.roles, name="new")
channel = bot.get_channel(996767690091925584)
embed = nextcord.Embed(title="welcome to ikari!", description="・make sure to read the rules in <#928443083698360397> \n ・for more questions refer to <#928507764714651698>", color=0x303136)
embed.set_author(name=f"{member.name}#{member.discriminator}", icon_url=member.display_avatar_url)
embed.set_thumbnail(url=member.guild.icon.url)
await channel.send(f"{member.mention}!", embed=embed)
await member.add_roles(new)Error:
Error:
AttributeError: 'Member' object has no attribute 'display_avatar_url'
Discord.py v2 changed the variables. Meaning that .avatar_url is now .avatar.url. .icon_url is now .icon.url. Hence meaning that member.display_avatar.url is what you're looking for.
This also means that stuff like .avatar_with_size(...) for example have now been changed to .avatar.with_size(...). Just for future reference.
Discord.py now has different variables for display avatars etc.
Just edit this line:
embed.set_author(name=f"{member.name}#{member.discriminator}", icon_url=member.display_avatar_url)
to
embed.set_author(name=f"{member.name}#{member.discriminator}", icon_url=member.display_avatar.url)
If this answer has worked, please mark it as the correct answer! :)

Getting a discord id from username

I am making a bot that has a database of things and sometimes someone would want to see what things a different person has, this would be possible by using discord id, but I just don't was everyone sending their discord ids around.
I was trying to use something like this:
def get_user_id(name_en, tag_en, guild_id):
guild = client.get_guild(guild_id)
user = discord.utils.get(guild.members, name= "Atom" , discriminator= "#1803")
print(user)
But that just outputs an empty message.
So is there a way to get a discord id from a username?
Also is this possible with making the discord bot in a different programming language? (if not possible in discord.py)
Thanks
You can use Guild.get_member_named()
The name can have an optional discriminator argument, e.g. “Jake#0001” or “Jake” will both do the lookup. However the former will give a more precise result. Note that the discriminator must have all 4 digits for this to work.
def get_user_id(name_en, tag_en, guild_id):
guild = client.get_guild(guild_id)
user = guild.get_member_named(name_en)
print(user)

value error while trying to join a channel using telethon

I have a code that I need to join a given channel in but getting the following error
telethon.errors.rpcerrorlist.ChannelInvalidError: Invalid channel object. Make sure to pass the right types, for instance making sure that the request is designed for channels or otherwise look for a different one more suited (caused by JoinChannelRequest)
my code :
group_input = InputChannel(group.id, group.access_hash)
client(JoinChannelRequest(group_input))
here I'm using inputChannel because i looked in the joinChannelRequest and it asked for "TypeInputChannel"
and it's a union of [InputChannelEmpty,InputChannel,InputChannelFromMessage]
another things that I tried:
1-
client(JoinChannelRequest(group.id))
2-
client(JoinChannelRequest(group))
3-
group_input = InputPeerChannel(group.id, group.access_hash)
client(JoinChannelRequest(group_input))
The simplest way you can do is to retrieve the username of the public channel
Suppose the username of the channel is #The_Channel
Get the entity
entity = client.get_entity("t.me/The_Channel") #omit #
Then invoke this function
client(JoinChannelRequest(entity))

(discord.py) Getting a list of all of the members in a specific voice channel

So I'm attempting to write a raiding bot for my raiding discord using the discord.py library in python. This scripts is supposed to be forming a list of members in the voice channel for raiding. For some reason, this script is not working. Whenever memids is printed, it just prints an empty list.
If anyone is familiar with discord.py and could tell me why this isn't working, please do. It's really troubling me and I have tried everything in my knowledge to fix it.
#find raiding
voice_channel = discord.utils.get(ctx.message.server.channels, id = '440014722587426816')
#finds the members
members = voice_channel.voice_members
memids = []
for member in members:
memids.append(member.id)
print(memids)
If you know the id of the channel you can do it this way. Works for me :D
channel = client.get_channel(1234567890) #gets the channel you want to get the list from
members = channel.members #finds members connected to the channel
memids = [] #(list)
for member in members:
memids.append(member.id)
print(memids) #print info
I faced the same problem. voice_channel.members would return either empty or incomplete lists some times.
The docs say:
voice_states
Returns a mapping of member IDs who have voice states in this channel.
Note: This function is intentionally low level to replace members when the member cache is unavailable.
https://discordpy.readthedocs.io/en/latest/api.html#voicechannel
I guess the members can't be trusted to return accurate connected member list consistently.
I solved this problem with the following code:
member_ids = voice_channel.voice_states.keys()
You need to enable "SERVER MEMBERS INTENT:
If your bot tracks server members or downloads the entire member list, you may need the server members intent to receive member events and the member list." on the discord developer page on the bot tab.
There isn't much to go on from your question. I believe your problem is that the id that you provided to utils.get(...) isn't the correct id of the voice channel. This is probably the reason to why you're always getting an empty list.
voice_members
A list of Members that are currently inside this voice
channel. If type is not ChannelType.voice then this is always an empty
array.
If you're not fully sure about the voice channel's actual id, I suggest you search by the name and type (discord.ChannelType.voice):
voice_channel = discord.utils.get(ctx.message.server.channels, name="channelname", type=discord.ChannelType.voice)
I you know the channel id, I suggest using
voice_channel = client.get_channel(channel_id)
instead (documentation here). If you're using discord.py-rewrite, you can also use:
voice_client = ctx.guild.get_channel(channel_id)
if the channel you're looking for is in the context guild (documentation here).

Get all of the users from a specific channel Slack API

I am trying to make a Slack Bot using python, and I have a issue, I am not able to get the users from a specific channel, I only succeed if I take all of the users. Basically I want only those from (eg. random channel).
Until now I tried to get the team's ID from every user and compare it to a channel ID, but that failed because everyone is having the same ID and I can't figure out why.
Here is the snippet of the code:
def users_of_the_channel():
global slack_client
#this is the variable which is initialized with SlackClient(BOT_TOKEN)
api_call = slack_client.api_call( "users.list",channel="C0XXXXXXX")
if api_call.get('ok'):
channels = api_call.get('members')
for channel in channels:
print ("this is cool : ", channel['team_id'])
The issue I believe is that when I initialize the api_call variable I call the function with the users.list argument, I tried with usergroups.list and usergroups.users.list but with no success.
Basically to keep it short I need the list with the users from a channel, and the documentation hasn't helped me.
users.list does not take channel id as input, which you are providing in your API call.
Slack provide different bot token for different teams.
Simply call api with bot token of team of which you require user list and you will get the list of members.
client = SlackClient(SLACK_BOT_TOKEN)
request = client.api_call("users.list")
if request['ok']:
for item in request['members']:
print item['name']
For more details about optional arguments you can provide with api call, refer documentation(https://api.slack.com/methods/users.list).
You probably can use the new conversations API to retrieve that (specifically conversations.members endpoint):
https://api.slack.com/methods/conversations.members

Categories

Resources