不和谐 如何为每个公会添加自定义角色

我正在处理名为 on_member_join 的事件,我想给新成员一个角色,每个公会的角色都不一样,所以我需要公会 ID。我有一个 json 文件,其中有我的公会 ID 和“自动角色”的角色 ID。我里面有错误role = roles[str(member.guild.id)]

错误是:await member.add_roles( role ) 文件“C:\Users\arman\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\member.py”,第 641 行,在 add_roles 等待req(guild_id, user_id, role.id, reason=reason) AttributeError: 'str' 对象没有属性 'id'

我的 JSON 文件例如:

{ "724249401962397767": "724868027241463859" }

#Auto role

@commands.Cog.listener()

async def on_member_join( self, member ):

    emb = discord.Embed( title = '**Here is new member**', colour = discord.Color.green() )

    channel = self.bot.get_channel( config.CHANNEL_ID )


    with open('roles.json', 'r') as f:

        roles = json.load(f)


    role = roles[str(member.guild.id)]


    await member.add_roles( role )


    emb.set_author( name = self.bot.user )

    emb.add_field( name = "**Say Hello to**", value = '**{}**'.format( member.mention ), inline=False )

    emb.set_thumbnail( url = self.bot.user.avatar_url )

    emb.add_field( name = "**Roles is**", value = '**{}**'.format( member.roles ).replace( '[' , '' ).replace( ']' , '' ).replace( '<' , '' ).replace( '>' , '' ).replace( "'" , '' ).replace( 'Role' , '' ).replace( 'id=' , '' ).replace( 'name=' , '' ).replace( '1' , '' ).replace( '2' , '' ).replace( '3' , '' ).replace( '4' , '' ).replace( '5' , '' ).replace( '6' , '' ).replace( '7' , '' ).replace( '8' , '' ).replace( '9' , '' ).replace( '0' , '' ).replace('"', '').replace("ud", ''), inline=False )


    await channel.send( embed = emb)


杨魅力
浏览 122回答 2
2回答

忽然笑

您的on_member_join方法是用一个discord.Member名为 的对象调用的member。该member对象有一个guild属性,请参阅https://discordpy.readthedocs.io/en/latest/api.html?highlight=member#discord.Member.guild该guild对象来自类discord.Guild该对象有一个名为Integer 的guild属性,请参阅https://discordpy.readthedocs.io/en/latest/api.html?highlight=member#discord.Guild.idid所以要回答这个问题:如何获得你的公会IDgid&nbsp;=&nbsp;member.guild.id您获取discord.Role对象的逻辑是错误的。此外,您不需要公会 ID 即可获得角色。你需要公会对象。roles&nbsp;=&nbsp;member.guild.roleshttps://discordpy.readthedocs.io/en/latest/api.html?highlight=member#discord.Guild.roles这些都是公会的角色。role&nbsp;=&nbsp;member.guild.get_role(123456)https://discordpy.readthedocs.io/en/latest/api.html?highlight=member#discord.Guild.get_role这会为您提供具有给定角色 ID 的角色。尝试阅读并应用文档。盲目编程、遵循教程或查看其他人的工作不会让你走得太远。很高兴让项目运行,但最后文档将包含您需要的所有知识。

桃花长相依

#Auto role@commands.Cog.listener()async def on_member_join( self, member ):&nbsp; &nbsp; emb = discord.Embed( title = '**Here is new member**', colour = discord.Color.green() )&nbsp; &nbsp; with open('messagech.json', 'r') as f:&nbsp; &nbsp; &nbsp; &nbsp; messagechs = json.load(f)&nbsp; &nbsp; messagech = messagechs[str(member.guild.id)]&nbsp; &nbsp; channel = self.bot.get_channel( int(messagech) )&nbsp; &nbsp; with open('roles.json', 'r') as f:&nbsp; &nbsp; &nbsp; &nbsp; roles = json.load(f)&nbsp; &nbsp; role = roles[str(member.guild.id)]&nbsp; &nbsp; role = int(role)&nbsp; &nbsp; role = discord.utils.get(member.guild.roles, id = role )&nbsp; &nbsp; await member.add_roles( role&nbsp; )&nbsp; &nbsp; with open('roles.json', 'w') as f:&nbsp; &nbsp; &nbsp; &nbsp; json.dump(roles, f, indent=4)&nbsp; &nbsp; emb.set_author( name = self.bot.user )&nbsp; &nbsp; emb.add_field( name = "**Say Hello to**", value = '**{}**'.format( member.mention ), inline=False )&nbsp; &nbsp; emb.set_thumbnail( url = self.bot.user.avatar_url )&nbsp; &nbsp; emb.add_field( name = "**Roles is**", value = '**{}**'.format( member.roles ).replace( '[' , '' ).replace( ']' , '' ).replace( '<' , '' ).replace( '>' , '' ).replace( "'" , '' ).replace( 'Role' , '' ).replace( 'id=' , '' ).replace( 'name=' , '' ).replace( '1' , '' ).replace( '2' , '' ).replace( '3' , '' ).replace( '4' , '' ).replace( '5' , '' ).replace( '6' , '' ).replace( '7' , '' ).replace( '8' , '' ).replace( '9' , '' ).replace( '0' , '' ).replace('"', '').replace("ud", ''), inline=False )&nbsp; &nbsp; await channel.send( embed = emb)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python