I want to create a script that given a server id, it should return all the member of that discord server. and given a user authentification token, it should check if he is a member of that server.
I have been looking how to do this but in vain! I have tried those 3 questions but they don't give me any information on what I want to do.
Here is what i have tried according to the documentation and get started tutorial:
import discord
import asyncio
import os
client = discord.Client()
email = os.getenv('Email')
password = os.getenv('Password')
server = discord.Server(id='416940353564704768')
@client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
print('get all channel a client belong to ')
if server.members:
for member in server.members:
print('name{}'.format(member.user.name) )
else:
print('any')
client.run(email, password)
But it always prints any. Any helps will be appreciated, it looks like I'm missing something on how discord works.
A couple things to point out:
You need to get the server from the client. You can't just do discord.Server(id="111111")
. Instead, use the get_server method. client.get_server(id="11111")
.
Put the get_server
method in the on_ready
event. This ensures the client
is connected to discord and has received its server list and all other data.
A Member
object is just a subclass of User
; therefore, you can't do Member.user.name
because neither Member
nor User
have a user
property. You only need to do Member.name
.
Lastly, I wouldn't recommend you to use a self bot. That is, using your own discord account as though it were a bot. You should use an actual bot account with its token.
Ultimately, your code should look like this.
import discord
import asyncio
import os
client = discord.Client()
email = os.getenv('Email')
password = os.getenv('Password')
@client.event
async def on_ready():
server = client.get_server(id="416940353564704768")
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
print('get all channel a client belong to ')
if server:
for member in server.members:
print('name: {}'.format(member.name) )
else:
print('any')
client.run(email, password)
As for your second question, I'm not sure what you mean by "given a user authentification token, it should check if he is a member of that server.". If you're given their login token, I don't exactly know how you'd use that to check if they're in a sever without logging into that account. And if you do plan on logging in to the account, I believe you'll need to do that in a separate script. However, you can easily check if the logged in client is in a server:
@client.event
async def on_ready():
server = discord.utils.get(client.servers, id="416940353564704768")
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
if server:
print("Client is a member of: {}".format(server.name))
else:
print("Client is not a member")