Mihai
Mihai

Reputation: 15

Is there a way to give roles without a command?

I have a database with discord id, seconds and more A plugin from a game server is measuring player activity and feed it inside the database

I was wondering if there is a way to make my bot give a role to that specific discord id as long as his activity is bigger than some value

ive tried this way but it doesn't work

@tasks.loop(seconds=10)

async def checkDB():
    delete_oldDate()

    list =  get_db_list('players_activity', '*')
    for i in list:
        if str(i[3]) != 'None' and int(i[2]) >= min_activity:
            user = i[3]
            role = discord.utils.get(user.guild.roles, giveawayRole_ID)
            await client.add_role(user, role)
        else:
            print("")

any ideas?

Upvotes: 0

Views: 50

Answers (1)

stijndcl
stijndcl

Reputation: 5650

Adding roles inside and outside of a command works the same way. client.add_role doesn't exist, which is why it doesn't work. The correct function is Member.add_roles(list_of_roles).

Going off of context, I'm gonna assume your user is already a discord.Member instance, as you're calling it's guild attribute. You can also use Guild.get_role(role_id) to get the discord.Role instance of the role.

user = i[3]
role = user.guild.get_role(giveawayRole_ID)
await user.add_roles([role])

EDIT:

Apparently your user was not a discord.Member instance yet, so you'll have to get that first. You should've gotten an error from that though, which you didn't mention. In case user is a string, user.guild shouldn't work at all either.

First of all, instead of storing their Discord ID as Mihái#8090, you should store their actual ID, which is a number. You can get this by using Member.id whenever they use a command (to put them in the database), or right-clicking them and using Copy ID in Discord.

Seeing as you always want to use the same Guild, you should already have the Guild's ID stored somewhere as well.

guild_id = 000000000000 # Whatever the ID of your Guild is
guild = client.get_guild(guild_id)
member = guild.get_member(int(i[3]))
role = guild.get_role(giveawayRole_ID)
await member.add_roles([role])

So first get the Guild instance, then the user's Member instance in that guild, then the Role instance, and then give the member the role.

Upvotes: 1

Related Questions