Rapptz / discord.py

An API wrapper for Discord written in Python.
http://discordpy.rtfd.org/en/latest
MIT License
14.92k stars 3.77k forks source link

feature request: add_role for a member #505

Closed ioandev closed 7 years ago

ioandev commented 7 years ago

Hi, there seems to be no way to add a role for a user when she types something in chat.

    elif message.content == '!dj':
        music_channel = get_channel("music")
        if music_channel is not None:
            member_permissions = music_channel.permissions_for(message.author)
            has_dj_role = member_permissions.send_messages

            #add the role
            if not has_dj_role:

                member.add_role("dj")
# ^^^^
                fmt = '{0.mention} you are now a dj!
                message = fmt.format(message.author)
                await client.send_message(music_channel, message)
Eternity71529 commented 7 years ago

http://discordpy.readthedocs.io/en/latest/api.html#discord.Client.add_roles and I suggest reading the docs before asking something like that -> http://discordpy.readthedocs.io/en/latest/api.html

ioandev commented 7 years ago

Great!

Thank you @Eternity71529

ioandev commented 7 years ago

Hi.. sorry but it doesn't seem to work.

My bot has permissions to change roles. It says in the documentation The Member object is not directly modified afterwards until the corresponding WebSocket event is received. i'm waiting 5s + and nothing happens. The code checks if the user has permission correctly (as far as i'v eseen), but add_roles and remove_roles doesn't seem to be doing anything.

my code:


#helper method
def get_channel(channel_name):
    all_channels = client.get_all_channels()
    channelToBroadcast = None
    for channel in all_channels:
        if channel.name == channel_name and channel.type == ChannelType.text:
            channelToBroadcast = channel

    return channelToBroadcast
def get_role(role_name, server):
    for role in server.roles:
        if role.name == role_name:
            return role

    return None

##in message listener:
    if message.content == '!dj':
        music_channel = get_channel("music")
        if music_channel is not None:
            member_permissions = music_channel.permissions_for(message.author)
            has_dj_role = member_permissions.send_messages
            print(has_dj_role)
            message_sent = None

            #add the role
            if not has_dj_role:
                dj_role = get_role("DJ", message.server)
                client.add_roles(message.author, [dj_role])

                fmt = '{0.mention} esti DJ si ai access la canalul #music!'
                to_send = fmt.format(message.author)
                message_sent = await client.send_message(message.channel, to_send)
            else:
                fmt = '{0.mention} esti deja DJ!'
                to_send = fmt.format(message.author)
                message_sent = await client.send_message(music_channel, to_send)

            await asyncio.sleep(5)
            await client.delete_message(message_sent)

    elif message.content == "!nodj":
        music_channel = get_channel("music")
        if music_channel is not None:
            member_permissions = music_channel.permissions_for(message.author)
            has_dj_role = member_permissions.send_messages
            print(has_dj_role)
            message_sent = None

            #
            if has_dj_role:
                dj_role = get_role("DJ", message.server)
                print(type(dj_role))
                client.remove_roles(message.author, [dj_role])

                fmt = '{0.mention} nu mai esti DJ!'
                to_send = fmt.format(message.author)
                message_sent = await client.send_message(message.channel, to_send)
            else:
                fmt = '{0.mention} nu esti DJ!'
                to_send = fmt.format(message.author)
                message_sent = await client.send_message(message.channel, to_send)

            await asyncio.sleep(5)
            await client.delete_message(message_sent)
ioandev commented 7 years ago

FrostLuma, Painezor and Jayden helped me solve this on discord.

Solution:

  1. use await client.add_roles, await client.delete_roles.
  2. pass it just the role, not a list of roles.