#How to get a member by user id?

1 messages · Page 1 of 1 (latest)

kind canopy
#

do you have both the guild and members intents?

minor plover
#

I think so, lemme check

lime drift
kind canopy
#

^

minor plover
#

Lemme check too ^^

lime drift
#

?tag intents

rustic flareBOT
#

https://docs.pycord.dev/en/master/intents.html
https://discord.com/developers/docs/topics/gateway#gateway-intents

import discord
from discord.ext import commands

# Get specific intents for fine control
intents = discord.Intents()
intents.emojis = True
intents.guilds = True
intents.messages = True  # Required for prefix commands!
...
# Get all non-priveliged intents; this excludes presences, members and message_content 
intents = discord.Intents.default()

# Set priveliged intents: these must be enabled on dev portal
intents.members = True
intents.presences = True
intents.message_content = True  # Required for prefix commands >= 2.0.0b5

# Get all intents; all intents must be enabled on dev portal.
intents = discord.Intents.all()

# Apply intents when creating your bot
bot = commands.bot(prefix="?", intents=intents)
Discord Developer Portal

Integrate your service with Discord — whether it's a bot or a game or whatever your wildest imagination can come up with.

kind canopy
#

you need all

#

not default

minor plover
#

alright lemme test

#
bot = discord.Bot(intents=discord.Intents.all())```
#

?

kind canopy
#

yeah

minor plover
#

alright lemme try

#

ill tell you about the result in a min

#

nop,

#

Error :

#

Code :

kind canopy
#

int userid?

minor plover
#

yeah cuz basically storing the user id in a db (stored as str)

kind canopy
#

yeah but you removed the int conversion

minor plover
minor plover
kind canopy
#

in your code...

#

before you did int(userid) and now you aren't

minor plover
#

@kind canopy still no

#

oh

#

wait

#

2

#

min

kind canopy
#

yeah

minor plover
#

OK

#

FIXED

#

ty<3

kind canopy
#

allgood

minor plover
#

@kind canopy wait a min

#

dont close the ticket

#

rn

kind canopy
#

well that ain't my job

#

you gotta close when you're done dab

minor plover
#

oh alright

kind canopy
#

await

minor plover
#

how can i await this, when im not in async function

kind canopy
#

you don't...

minor plover
#

well...

#

i get the error

kind canopy
#

why are you trying to do it out of async

lime drift
#

^

minor plover
kind canopy
#

there are several other places it will work

#

can't use an event or task loop?

minor plover
#

while True:

kind canopy
#

please

#

don't

minor plover
#

thank you

#

ill check it out

#

im really not familliar

kind canopy
#

it's fine

minor plover
#

pycord is still new to me

lime drift
#

try fetch_member

minor plover
#

its the server request that return None

lime drift
#

than you dont have a guild

minor plover
#

it is

#

the bot is litteraly in

lime drift
#

for what do you need this?

minor plover
#

@kind canopy do you have any cause of this error?

minor plover
lime drift
minor plover
#

fetch a guild, then fetch a certain member (by user id) then give him a roles

lime drift
#

in a task?

minor plover
#

yes

#
@tasks.loop(seconds=2)
async def role():
    roletable = json.load(open('./db/roles.json'))


    for userid in roletable:
            #print(userid)
        userdata = roletable.get(userid)

        for guild in userdata:
            guildz = userdata.get(guild)
            for guild in guildz:
                for role in guildz.get(guild):
                        print(guild)
                        server = bot.get_guild(int(guild))
                        member = server.fetch_member(member_id=int(userid))
                        if member:
                            print('OMG WE FOUND IT FINALLY')
                            member.add_roles(1071892798288175195)
@role.after_loop
async def afterloop():
    pass


role.start()```
#

@kind canopy got any solution to this?

kind canopy
#

that for loop chain is very confusing

#

are you sure you didn't mix up the IDs

minor plover
#

no

#

im sure

#

i checked them

#

lemme try to fetch it with the id

kind canopy
#

and printing bot.guilds is fine?

minor plover
#

and not the loop variables

kind canopy
#

fetching is pointless because a fetched guild doesn't include members

#

well i guess if you fetch the member then it doesn't matter anyway

minor plover
minor plover
#

@kind canopy

kind canopy
#

does await bot.fetch_guild work in that scenario?

minor plover
#

yep

minor plover
#

ok so basically, fetching is working but adding the roles return me this error

#

2sec

#

ok

#

i got it fixed