Daniel Tam
Daniel Tam

Reputation: 926

How do I make my bot respond to a user mention without on_message event?

Blockquote

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

I'm trying to make an afk command and this is all I got. I'm having trouble making my bot respond to a user who has just recently used this afk command. Basically I'm trying to make Dyno bot's afk command.

"The AFK module allows users to set an AFK status inside of your server. If a user sets an AFK status, Dyno will leave a message displaying their AFK status whenever the user is mentioned. Dyno will automatically add “[AFK]” to the beginning of your nickname whenever you set an AFK status (if it has the permissions to)."

This is the definition of Dyno's afk command. That is basically what I'm trying to do as well.

Upvotes: 0

Views: 914

Answers (1)

Karan
Karan

Reputation: 498

You are probably trying to copy other creators without having the knowledge of the actual library. You need to use your own logic and understand the concepts.

I don't how know dyno works, However, I have understood what actually you are trying to say and this snippet can give you an idea of how things work and how you can implement them in your code.

#STORING THE USER IN A LIST
afk_list = []
@bot.command()
async def afk(ctx):
    global afk_list
    # IF USER ID IS NOT FOUND IN THE LIST
    if not ctx.author.id in afk_list:
        #STORE THE ID IN THE LIST
        afk_list.append(ctx.author.id)
        #EDIT THE NAME OF THE AUTHOR
        await ctx.author.edit(nick = f'[AFK] {ctx.author.name}')
        await ctx.send(f'{ctx.author.mention} You have been marked as AFK.')
        return afk_list
    else:
        afk_list.remove(ctx.author.id)
        nick = ctx.author.name.replace('[AFK]','')
        await ctx.author.edit(nick = nick)
        await ctx.send(f'{ctx.author.mention} You are no longer AFK.')
        return afk_list

@bot.event
async def on_message(message):
    global afk_list
    try:
        #We check if the message starts with any mentionable content
        if message.content.startswith('<'):
            a = message.content
            a = a.replace('<','')
            a = a.replace('@','')
            a = a.replace('!','')
            a = a.replace('>','')
            a = int(a)
            #HERE We have replaced all the strings and converted it into Integar.
            if a in afk_list:
                #If the User in the LIST, The message will be sent.
                user = bot.get_user(a)
                await message.channel.send(f'{user.name} is currently AFK')
            else:
                pass
    except Exception:
        pass
        
    await bot.process_commands(message)

Upvotes: 2

Related Questions