如何使我的漫游器响应用户提及?/如何进行afk命令?

时间:2020-07-21 04:15:12

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

基本上,我想做的是学习如何创建afk命令来响应提及并告诉用户自他发送afk消息以来已经有多长时间了,以及他们目前在做什么。就像Dyno机器人的afk命令:)。

@client.command()                                                                          
async def afk(ctx, activity=None, minutes=None):                                           
    if ctx.author.mention and activity and minutes:                                        
        time = await asyncio.sleep(minutes)                                                
        await ctx.send(f"""{ctx.author} is afk. Reason: {activity}. Time Left: {time} """) 

这就是我的全部,因为现在,我不知道如何像发送XD消息的时间戳一样发送消息

更新

@client.command()
async def afk(ctx, activity=None):
    if ctx.author.mention:
        await ctx.send(f"""{ctx.author.mention} is currently afk. Reason: {activity}""")

    else:
        print("A user is afk...")

这是我的第二次尝试。

1 个答案:

答案 0 :(得分:2)

您将需要使用on_message事件来检查消息是否有提及,以告知提及他们所提及的用户的人。

我为自己的机器人创建afk命令的方式是创建一个空的afk字典,并在用户运行afk命令时将用户添加为dict的键,并且将值设置为消息/其他详细信息,以在发送时他们被提及:

afkdict = {}

@client.command()
async def afk(ctx, message):
    global afkdict

    #remove member from afk dict if they are already in it
    if ctx.message.author in afkdict:
        afkdict.pop(ctx.message.author)
        await ctx.send('you are no longer afk')


    else:
        afkdict[ctx.message.author] = message
        await ctx.send(f"You are now afk with message - {message}")

@client.event
async def on_message(message):
    global afkdict
        
    #check if mention is in afk dict
    for member in message.mentions: #loops through every mention in the message
        if member != message.author: #checks if mention isn't the person who sent the message
            if member in afkdict: #checks if person mentioned is afk
                afkmsg = afkdict[member] #gets the message the afk user set
                await message.channel.send(f" {member} is afk - {afkmsg}") #send message to the channel the message was sent to 

如果您想保存的不仅仅是用户访问afk时的消息,则可以使用2d词典:

async def afk(ctx, arg1, arg2):
    afkdict[ctx.message.author] = {arg1:arg1, arg2:arg2}

并访问其他详细信息

afkdict[member][arg1]  #gets arg1 from 2d dictionary where key is member

就时间戳而言,使用datetime module

是明智的