Daniel Tam
Daniel Tam

Reputation: 926

How do I make my bot respond to a user mention?/How to make an afk command?

Basically what I want to do is learn how to make an afk command that responds mentions and tells the users how long since he has sent the afk message, and what they're currently doing. Just like Dyno's bot's afk command :).

@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} """) 

This is all I have because right now, I have no idea how to send like a timestamp of when the message was sent XD

UPDATE

@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...")

This is my second attempt.

Upvotes: 0

Views: 1003

Answers (1)

Silas Hayes-Williams
Silas Hayes-Williams

Reputation: 112

You're going to need to use the on_message event to check whether a message has a mention to inform the person mentioning that the user they have mentioned is afk.

The way I've created an afk command for my own bot is to create an empty afk dict and add users as keys to the dict when they run the afk command and have the value be a message/other details to send when they get mentioned:

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 

if you wanted to have more than just a message saved for when a user goes afk you could use 2d dictionaries:

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

and to access these other details you would do

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

In terms of timestamps, it would be wise to use the datetime module

Upvotes: 3

Related Questions