discord.py检查提到的ID和ID列表

时间:2018-11-07 17:13:23

标签: python discord discord.py

如果我键入-afk,则我的机器人会保存我的ID;当另一个用户提到我时,我的机器人会发送消息,表明我是afk

问题是,当我尝试检查if中提到的id和id时,我的afk_list语句不起作用。

问题出在这里

    async def on_message(self, message):
    mentioned = message.raw_mentions

    if mentioned in afk_list:
        print('Found!!')
        await self.client.say('User is AFK')

这是我的完整代码:

import discord
from discord.ext import commands
import datetime

afk_list = []

class Manage:

def __init__(self, client):
    self.client = client

async def on_message(self, message):
    mentioned = message.raw_mentions

    if mentioned in afk_list:
        print('Found!!')
        await self.client.say('User is AFK')

@commands.command(pass_context = True)
async def afk(self, ctx):
    server = ctx.message.server
    channel = ctx.message.channel
    author = ctx.message.author
    date = datetime.date.today

    embed = discord.Embed(
        colour=discord.Colour.red()
    )
    afk_list.append(ctx.message.author.id)
    embed.add_field(name='**User {} is currently AFK**'.format(author), value='Since ', inline=False)

    await self.client.send_message(channel, embed=embed)

@commands.command()
async def afklist(self):
    #await self.client.say('AFK ID list: ')
    #for name in afk_list:
    #    print('List: {}'.format(name))
    #    await self.client.say(name)
    print(afk_list)

def setup(client):
   client.add_cog(Manage(client))

谢谢!我希望有人能帮助我。

1 个答案:

答案 0 :(得分:0)

Message.raw_mentions返回一个列表。您可以使用any(id in afk_list for id in mentioned)查看该列表中是否还包含afk_list中的任何内容。如果改用Message.mentions,则还可以指定哪些用户是afk。

async def on_message(self, message):
    mentioned = message.mentions

    for user in mentions:
        if user.id in afk_list:
            await self.client.send_message(message.channel, '{} is AFK'.format(user.display_name))

您也不能在命令之外使用Bot.say