discord.py:当我试图让我的机器人根据命令赋予角色时,我得到了一个attributeerror

hxzsmxv2  于 2021-09-08  发布在  Java
关注(0)|答案(1)|浏览(255)

如何让我的机器人通过命令赋予角色?没有人问这个问题,也没有人有答案,我搜索了一个多小时。我想要的是用户说“给我成员”,然后机器人给成员角色。代码如下:

import discord
import os
import random

client = discord.Client()

@client.event
async def on_ready():
    print ('logged in')

@client.event
async def on_message(message):
    if message.author == client.user:
        return
    if message.content == 'give me member':
        role = discord.utils.get(message.author.server.roles, id="863082485851750431")
        await bot.add_roles(message.author, role)
client.run('EnterToken')

我得到这个错误:

Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\USER\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args,**kwargs)
  File "C:\Users\USER\Desktop\Dbot2.py", line 19, in on_message
    role = discord.utils.get(message.author.server.roles, id="863082485851750431")
AttributeError: 'Member' object has no attribute 'server'

一旦你有答案,请给我代码,我可以复制和粘贴(不包括令牌)

xoshrz7s

xoshrz7s1#

server 是旧方法,不再受支持,并且 bot.add_roles 不正确,因为 bot 甚至没有定义,它是旧的,应该是这样的。
您应该始终检查代码的日期

role = discord.utils.get(message.guild.roles, id=863082485851750431)
await message.author.add_roles(role)

文件:
Guild.roles Message.guild

相关问题