【问题标题】:Discord Bot: Why this bot can read DM but doesn't give the role in the server?Discord Bot:为什么这个bot可以读取DM但不给服务器角色?
【发布时间】:2023-01-26 18:54:17
【问题描述】:

我正在创建一个不和谐的机器人,如果用户在 BOT dm 中输入正确的电子邮件,并在电子邮件前加上“@”(如@example@gmail.com),它可以在 MIAO 服务器中提供示例角色

async def on_message(message):
    #Check if the message is a DM
    if isinstance(message.channel, discord.DMChannel):
        #Check if the message starts with "@"
      if message.content.startswith("@"):
          email = message.content
           # Check if the email is in the database
          if email in email_database:
            # If the email is in the database, then give the user the Example role
            server = message.guild
            role = discord.utils.get(message.guild.roles, name='Example')
            await message.author.add_roles(role)
            await message.channel.send('Email found in the database! You have been given the Example role!')
          else:
            # If the email is not in the database, then tell the user that the email was not found
            await message.channel.send('Email not found in the database!')

我确定电子邮件数据库(我在这些行之前插入代码)。

我怎么解决这个问题?我必须指定哪个服务器吗?

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    如果消息在 DM 中发送,则 message.guild 将变为 None。我假设您收到一条错误消息。您必须获得guild,然后是公会中的成员,然后以这种方式应用角色。

    @client.event
    async def on_message(message):
        #Check if the message is a DM
        if isinstance(message.channel, discord.DMChannel):
            #Check if the message starts with "@"
            if message.content.startswith("@"):
                email = message.content
                # Check if the email is in the database
                if email in email_database:
                    # If the email is in the database, then give the user the Example role
                    
                    # change `client` to `bot` or whatever your client/bot object is called
                    server = await client.fetch_guild(YOUR_GUILD_ID)
                    role = discord.utils.get(server.roles, name='Example')
                    # change this to `await server.fetch_member` if you keep getting None but you're _certain_ they exist
                    # shouldn't be necessary though as we're fetching the guild
                    server_member = server.get_member(message.author.id)
                    if not server_member:
                        # user doesn't exist in guild
                        return
                    await server_member.add_roles(role)
                    await message.channel.send('Email found in the database! You have been given the Example role!')
                else:
                    # If the email is not in the database, then tell the user that the email was not found
                    await message.channel.send('Email not found in the database!')
    

    【讨论】:

      猜你喜欢
      • 2020-09-02
      • 1970-01-01
      • 2022-01-16
      • 1970-01-01
      • 1970-01-01
      • 2020-07-13
      • 2020-08-09
      • 2020-06-29
      • 1970-01-01
      相关资源
      最近更新 更多