How to get server details of a member - python-3.x

How to get details of a member using his UserID. like example i want to know in which server(name and ID) he is member of were ever my bot is added.

It's as simple as looping through all of the servers the bot has access to and trying to retrieve the Member object from each server for that ID.
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
#bot.command(pass_context=True)
async def getUser(ctx, id):
# id = int(id) # On the rewrite branch, ids are ints
for server in bot.servers:
member = server.get_member(id)
if member:
await bot.say(f"Server Name: {server.name}")
await bot.say(f"Server ID: {server.id}")
bot.run("TOKEN")

Related

Discord.py API get user data from IP

I've been looking into the Discord API source code and I was wondering if you could get a user's data. Not just returning their username. There is nothing that I could find in the API's docks.
This is the furthest I got:
data = await discord.client.Client.fetch_user_profile(discord.client.Client.fetch_user_profile, id)
But I keep on getting the error:
CommandInvokeError: Command raised an exception: AttributeError: 'function' object has no attribute '_connection'
Caused by a what I think may be a function and a variable having the same name. Are there any ways to fix this, or any other methods to get a users data that works. Preferably without having to change the source code. Thanks for any help in advance.
And just in case you need it, here is the entirety of my code:
#import libarys
import discord
from discord.ext import commands
import os
#setup bot
bot = commands.Bot(command_prefix='&', owner_id=MyID, case_insensitive=True)
#bot.event
async def on_ready():
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name='for &help'))
print(f'\n\nLogged in as: {bot.user} - {bot.user.id}\nVersion: {discord.__version__}\n')
#find user data command
#bot.command()
async def find(ctx, id: int):
member = await ctx.guild.fetch_member(id)
data = await discord.client.Client.fetch_user_profile(discord.client.Client.fetch_user_profile, id)
print(member, data)
#connect bot
bot.run(os.environ.get('TOKEN'), bot=True, reconnect=True)
EDIT: This code is just a mock up to get a working concept, I don't care to make it more refined yet.
This is a really bad code, I don't even know how you came up with that code. You're not supposed to refer to the class itself but to the instance, your code fixed
#bot.command()
async def find(ctx, id: int):
member = await ctx.guild.fetch_member(id)
data = await bot.fetch_user_profile(id)
print(member, data)
Also note that the fetch_user_profile method it's deprecated since version 1.7 and bot accounts cannot use this endpoint
Reference:
Client.fetch_user_profile

discord.py - How to send a private message to a server owner?

I am trying to make my bot dm the server owner whenever it joins a new server. I have tried this:
#client.event
async def on_guild_join(guild):
user = await client.fetch_user(guild.owner)
await client.send_message(user,"hi there!")
But it gives this error message:
In user_id: Value "myname" is not snowflake
I can't figure out how to get the user id of the guild owner, i looked through the documentation but i couldn't find anything.
Here is a solution. You can just call the owner argument on a Guild:
#client.event
async def on_guild_join(guild):
owner = guild.owner
await owner.send("hi there!")

How to create a portfolio command in discord.py?

So, I need to make a !vx portfolio command for my VX Helper Bot, I want it to send a link of the member of my team, example - !vx portfolio videro will send a link to videro's portfolios, I cant figure out how to do so, however, I know how to make !vx portfolio #Videro but I dont want the user to tag the member to get his/her portfolio, any help would be really appreciated.
Thanks!
I am using this but its not working-
async def portfolio(ctx, team_member):
videro="videro"
harxu="harxu"
team_member = [videro,harxu,]
videro_embed = discord.Embed(title=f"VX Videro", description=f"Here is Videro's Portfolio-"
f"\n:point_right: https://www.twitter.com/viderodzns", colour=0x40cc88)
while True:
if team_member=='videro':
await ctx.send(embed=videro_embed)
Here's one way to do it: we have two commands, register and portfolio. register saves a url, and portfolio will use those saves urls to create embeds.
from discord import Member, Embed
urls = {}
#bot.command()
async def register(ctx, member: Member, *, url):
urls[str(member.id)] = url
#bot.command()
async def portfolio(ctx, member: Member):
id = str(member.id)
if id not in urls:
await ctx.send(f"Unrecognized user {member.display_name}")
return
embed = Embed(title=f"VX {member.display_name}",
description=f"Here is {member.display_name}'s Portfolio-\n:point_right: {urls[id]}", colour=0x40cc88)
await ctx.send(embed=embed)

discord.py get member instance from name#discriminator

I have a string with the name of an user user = 'somename#1111' how can I mention him? (I get the name from google sheets that's why it's a string)
I tried with:
#client.command()
async def mention(ctx):
user : discord.Member = 'somename#1111'
await ctx.send(f"{user.mention} hello!")
But it doesn't work
Although this method is valid, I'd recommend switching to saving user IDs instead of usernames + discriminators, as user IDs are constant, but users are able to change their names and discriminators.
#client.command()
async def mention(ctx):
user = discord.utils.get(ctx.guild.members, name="somename", discriminator="1111")
await ctx.send(f"{user.mention} hello!")
And with the ID:
#client.command()
async def mention(ctx):
user = ctx.guild.get_member(112233445566778899) # replace user ID there
try:
await ctx.send(f"{user.mention} hello!")
except:
await ctx.send("Sorry, that user has left the server!")
References:
utils.get()
User.discriminator
Guild.get_member()
Guild.get_member_named() - "Returns the first member found that matches the name provided."
I haven't used user.mention, but what I do is I get the user's id, then mention them like this:
await ctx.send(f'<#!{user.id}> hello!')

How to make a bot DM a list of people? (Discord.py) (Rewrite)

So this sends a DM to whoever I #mention.
#bot.command(pass_context=True)
async def pm(ctx, user: discord.User):
await user.send('hello')
How could I change this to message a list of IDs in let's say a text file, or a list variable containing user IDs?
In other words, how can I message multiple people with one command?
You can use Client.get_user_info to get the User class for a specified id value, if it exists.
Here is an example of how this can be done.
#bot.command()
async def pm(ctx):
user_id_list = [1, 2, 3] # Replace this with list of IDs
for user_id in user_id_list:
user = await bot.get_user_info(user_id)
await user.send('hello')
Also note that you do not need pass_context=True as context is always passed in the rewrite version of discord.py. See here: https://discordpy.readthedocs.io/en/rewrite/migrating.html#context-changes
If you want to message multiple people from the command, you can use the new Greedy converter to consume as many of a certain type of argument as possible. This is slightly different than the *args syntax because it allows for other arguments of different types to come after it:
from discord.ext.commands import Bot, Greedy
from discord import User
bot = Bot(command_prefix='!')
#bot.command()
async def pm(ctx, users: Greedy[User], *, message):
for user in users:
await user.send(message)
bot.run("token")
Useage:
!pm #person1 #person2 #person3 This is my message!

Resources