如何使用discord.py使用角色ID向成员添加角色?

问题描述 投票:0回答:1

如何通过角色 ID 向我通过 Discord.py 提到的人添加角色

我尝试了许多进入我的搜索的命令,但每个命令都给我留下了一个错误:

客户端中的文件“C:\Users\mxgak\OneDrive\Documents\Own Coded DC Tools\Stone MM Bot V1\main.py”,第 350 行 等待成员.add_roles(角色) 文件“C:\Users\mxgak\AppData\Local\Programs\Python\Python311\Lib\site-packages\discord\member.py”,第 777 行,在 add_roles 中 等待请求(guild_id,user_id,role.id,reason=reason) ^^^^^^^ AttributeError:“NoneType”对象没有属性“id”

discord discord.py roles
1个回答
0
投票
import discord
from discord.ext import commands

intents = discord.Intents.default()
intents.members = True

bot = commands.Bot(command_prefix='!', intents=intents)

@bot.event
async def on_ready():
print('Bot is ready.')

 @bot.command()
 async def giverole(ctx, member_id: 
 int, role_id: int):
 member = 
 
discord.utils.get(ctx.guild.members, 
 id=member_id)
 role = 
 discord.utils.get(ctx.guild.roles, 
 id=role_id)

if member is None:
    await ctx.send("Member not found.")
    return

if role is None:
    await ctx.send("Role not found.")
    return

await member.add_roles(role)
await ctx.send(f"Role {role.name} given to {member.display_name}")

   bot.run('YOUR_TOKEN')

我知道你可以用这个作为例子||作为模板

© www.soinside.com 2019 - 2024. All rights reserved.