属性错误问题“用户”对象没有属性“角色”

时间:2019-05-17 14:40:36

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

我在检查用户的角色时遇到了一个小问题。我目前正在

if member_role in message.author.roles] AttributeError: 'User' object has no attribute 'roles'

在检查消息作者是否具有他们角色时,似乎无法通过roles参数传递message

感谢帮助。

这是我正在使用的代码:

   async def on_message(self, message):
        if not isinstance(message.channel, discord.DMChannel) or 
message.author.id == self.bot.user.id:
            # not a DM, or it's just the bot itself
            return

    channel = self.bot.get_channel(578731262550736910)
    if not channel:
        print("Mail channel not found! Reconfigure bot!")
    time = datetime.utcnow()
    guild = self.bot.get_guild(555844758778544158)
    member_role = get(guild.roles, name='Members')
    muted_role = get(guild.roles, name='Modmail Muted')

    content = message.clean_content
    verified = [member for member in guild.members 
               if member_role in message.author.roles]

    muted = [member for member in guild.members 
               if muted_role in message.author.roles]

    if muted:
        await message.channel.send("You're not allowed to use modmail.")
        return

    if verified:
        embed = discord.Embed(title=" You've got modmail!")
        embed.add_field(name="Sent by:", value=f"{message.author.mention} ({message.author.id})" ,inline=False)
        embed.add_field(name="Message:", value=content[:1000] or "blank")
        embed.set_footer(text="Mods: React with icon below this message to notify us that you're dealing with this modmail. (First reaction    already set by the bot)")
        if message.attachments:
            embed.add_field(name="Attachments", value=", ".join([i.url for i in message.attachments]))
        if len(content[1000:]) > 0:
            embed.add_field(name="Message (continued):", value=content[1000:])
        msg = await channel.send(embed=embed)
        await msg.add_reaction('')
        try:
            embed = discord.Embed(title=f"Thanks {message.author.display_name}! your message has been sent to the Mods.", timestamp=time)
            embed.set_footer(text="Message Sent")
            await message.add_reaction('')
            await message.channel.send(embed=embed)
        except discord.ext.commands.errors.CommandInvokeError:
            await message.channel.send('')
        else:
            await message.channel.send("Only members can use modmail.")

1 个答案:

答案 0 :(得分:0)

@Patrick Haugh在我使用get_member的回复中建议。

user_id = message.author.id
author = guild.get_member(user_id)