简体   繁体   中英

Discord.py - Add role to user

import discord
from discord import Embed, Color


def ex(args, message, client, invoke):
    if (message.mentions.__len__() > 0):
        for member in message.mentions:
            server = message.server.name
            role = discord.utils.get(server.roles, name="MMDev")
            yield from client.add_roles(member, role)

This is my code. I have tried to add the mentioned user the role "MMDev". This is the error:

Traceback (most recent call last):
  File "venv\lib\site-packages\discord\client.py", line 307, in _run_event
    yield from getattr(self, event)(*args, **kwargs)
  File "main.py", line 91, in on_message
    yield from commands.get(invoke).ex(args, message, client, invoke)
  File "cmdmute.py", line 9, in ex
    role = discord.utils.get(server.roles, name="MMDev")
AttributeError: 'str' object has no attribute 'roles'

It´s Python 3.5

message.server.name is the name of the server, you instead want the Server object itself.

def ex(args, message, client, invoke):
    if (message.mentions.__len__() > 0):
        for member in message.mentions:
            role = discord.utils.get(message.server.roles, name="MMDev")
            if role:
                yield from client.add_roles(member, role)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM