Python discord.py在消息上添加角色

Python discord.py在消息上添加角色,python,python-3.x,discord,discord.py,Python,Python 3.x,Discord,Discord.py,我看到了很多关于这方面的问题,但没有一个对我有效,我不明白为什么这么简单的事情会这么复杂,alredy花了4个多小时在这方面,我想做一个基本的机器人,让新用户接受规则: 没有太多的解释,只是一个基本的机器人,当你在一个特殊的渠道中说接受时,它应该为你添加一个角色 import discord from discord.utils import get client = discord.Client() TOKEN = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX' @cli

我看到了很多关于这方面的问题,但没有一个对我有效,我不明白为什么这么简单的事情会这么复杂,alredy花了4个多小时在这方面,我想做一个基本的机器人,让新用户接受规则:

没有太多的解释,只是一个基本的机器人,当你在一个特殊的渠道中说接受时,它应该为你添加一个角色

import discord
from discord.utils import get

client = discord.Client()
TOKEN = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX'



@client.event
async def on_ready():
    #creates a message for users to react to
    guild = client.guilds
    channel = client.get_channel(836583535981608530)
    Text= "Type accept below if you understand and `accept` ALL the rules in <#836600011484785843>, in order to gain access to the server:"
    await channel.send(Text)

@client.event
async def on_message(message):
    if message.author == client.user:
        return
    
    channel = message.channel
    if channel.id == 836593532981608530 and message.content.lower() == ('accept'):
        await message.delete()
        user_role = discord.utils.get(message.guild.roles, name = "role name")
        new_member = message.author
        new_member.add_role(user_role, reason = "new member")
    elif channel.id == 836593532981608530:
        await message.delete()
导入不一致
从discord.utils导入获取
client=discord.client()
令牌='xxxxxxxxxxxxxxxxxxxxxxxx'
@客户端事件
_ready()上的异步定义:
#创建一条供用户响应的消息
guild=client.guilds
通道=客户端。获取通道(836583535981608530)
Text=“如果您了解中的所有规则并“接受”,请在下面键入accept,以便访问服务器:”
等待频道发送(文本)
@客户端事件
异步def on_消息(消息):
如果message.author==client.user:
返回
channel=message.channel
如果channel.id==836593532981608530和message.content.lower()==('accept'):
等待消息。删除()
user\u role=discord.utils.get(message.guild.roles,name=“role name”)
新成员=message.author
新成员。添加角色(用户角色,reason=“新成员”)
elif channel.id==836593532981608530:
等待消息。删除()

您的命令中的问题是,您没有等待
库工作必须等待的
语句

那么,让我们开始吧

需要等待的声明: 如何修复此错误? 您需要等待它,就像您在代码中等待其他语句一样

只需将行更改为:

await new_member.add_roles(user_role, reason="new member")
这将解决您面临的问题

你为什么需要等待一些陈述? 阅读以下链接中的文档,了解为什么需要等待一些语句。这将有助于您了解将来必须等待哪些命令


希望能有帮助。如果您仍然有任何问题,请随时在评论中询问我。:)


谢谢你需要等待声明。你会有一个错误,没有等待声明。我的回答会解释它。仍然不起作用,说:AttributeError:“Member”对象没有属性“add_role”哦,糟糕。即
添加角色
而不是
添加角色
。我真的很抱歉!我没注意到。现在就改了。请检查一下。效果不错!!!我非常接近。谢谢你的时间:)不客气!如果我能帮忙,请接受这个答案。:)
await new_member.add_roles(user_role, reason="new member")