【问题标题】:discord.py add role on messagediscord.py 在消息上添加角色
【发布时间】:2021-07-20 21:53:50
【问题描述】:

我看到了很多关于这个的问题,但没有一个对我有用,我不明白为什么听起来这么简单的东西那么复杂,已经花了 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()

【问题讨论】:

  • 您需要等待声明。您将收到未等待声明的错误。我的回答会解释的。

标签: python python-3.x discord discord.py


【解决方案1】:

您的命令中的问题是您没有 awaited 语句必须是 awaited 才能使库工作。

那么,让我们开始吧。

需要等待的语句:

new_member.add_role(user_role, reason = "new member")

如何解决这个错误?

您需要 await 它,就像您在代码中等待其他语句一样。

只需将行更改为:

await new_member.add_roles(user_role, reason="new member")

这将解决您面临的问题。

为什么需要await一些语句?

阅读以下链接中的文档,了解为什么需要将某些语句设为 awaited。这将帮助您弄清楚将来哪些命令必须是 awaited


希望对您有所帮助。如果您还有任何问题,请随时在 cmets 中问我。 :)

谢谢! :D

【讨论】:

  • 还是不行,说:AttributeError: 'Member' object has no attribute 'add_role'
  • 哦,哎呀。那是add_roles 而不是add_role。真的对不起!我没注意到。
  • 已更改,请检查。
  • 成功了!!!我离得太近了。谢谢你的时间:)
  • 不客气!如果我有帮助,请接受这个答案。 :)
猜你喜欢
  • 2021-09-21
  • 2021-07-06
  • 1970-01-01
  • 1970-01-01
  • 2021-06-05
  • 2019-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多