【问题标题】:discord.py how add role to memberdiscord.py 如何为成员添加角色
【发布时间】:2021-03-26 11:03:48
【问题描述】:

我无法解决问题。如何为调用 !role 命令的用户添加角色。 请帮忙。

import discord
from discord.ext import commands
from apex_legends import ApexLegends
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix='!', intents = intents)

@client.command()
async def rank(ctx, user_name,):
    rank = get_apex_rank(user_name) #return str role name
    await ctx.send(f"{rank}")# successfully receiving a response from the bot 
    member = ctx.author
    role = discord.utils.get(member.guild.roles, name=rank)
    await member.add_roles(role)
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'User' object has no attribute 'guild'
def get_apex_rank(name):
    try:
        player = apex.player(name)
        a = player.__dict__
        return a['_data']['metadata']["rankName"]
    except:
        return "Wrong name"

【问题讨论】:

  • 能否也请添加get_apex_range函数。
  • 另外,你是在调用dm的命令吗?
  • 请编辑问题,不要发布在 cmets 中,无法阅读
  • 你也没有回答我的第二个问题,你是用dm的命令吗?你启用intents.members了吗?
  • 我在不和谐中调用命令,我可以看到我得到 str "rank" = Bronze 4。你启用了 intents.members 吗? - 没有

标签: discord.py roles member


【解决方案1】:
@client.command():
async def role(ctx):
    role = discord.utils.get(ctx.guild.roles, name="enter role name") #enter role name here
    user = ctx.message.author
    await user.add_roles(role)

【讨论】:

  • 虽然此代码可以解决问题,including an explanation 说明如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提出问题的人。请edit您的答案以添加解释并说明适用的限制和假设。 From Review
猜你喜欢
  • 2021-07-26
  • 2021-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-19
  • 1970-01-01
  • 1970-01-01
  • 2019-08-11
相关资源
最近更新 更多