Willwell
Willwell

Reputation: 166

Discord.py AFK Command

So me and a friend have worked on this AFK code, and we got everything done besides mentions. The bot is supposed to check if someone mentioned is AFK, and if they are it should send a message on why they were afk.


@client.command()
async def afk(ctx, *, reason=None):
    embed = discord.Embed(timestamp=ctx.message.created_at, color=0x00ffff)
    embed.set_footer(text="Olympia Gaming")
    post = {
        "member": ctx.message.author.id,
        "reason": reason,
        "date": str(ctx.message.created_at)
        }
        
    if reason == None:
        collection.insert_one(post)
        embed.add_field(name=f"{ctx.author.name} is now AFK", value="Reason: No reason specified.")
        await ctx.send(embed=embed)
    else: 
        collection.insert_one(post)
        embed.add_field(name=f"{ctx.author.name} is now AFK", value=f"Reason: {reason}", inline=False)
        await ctx.send(embed=embed)

@client.event
async def on_message(message):
    embed = discord.Embed(timestamp=message.created_at, description="You have been removed from AFK.", color=0x00ffff)
    results = collection.find({"member": message.author.id}) 
    for result in results:
        collection.delete_one(result)
        await message.channel.send(embed=embed)

This code above is working.

    if message.mentions
    results = collection.find({"member": message.author.id}) 
    for result in results:
        collection.delete_one(result)
        if message.content == result:
            await message.channel.send(f"This person is currently AFK")
        

    await client.process_commands(message)  

I am not getting any errors, but I also want the bot to show the reason they were afk when they where mentioned. Can someone please help?

Upvotes: 1

Views: 3160

Answers (1)

Skirmish Playz
Skirmish Playz

Reputation: 49

This is a little bit late and I can't tell if the bottom snippet works, but if it does, here we go.

First we need to get the reason into the message. We do this by doing,

#in the the chat box we need to add the to curly brackets
    if message.mentions
    results = collection.find({"member": message.author.id}) 
    for result in results:
        collection.delete_one(result)
        if message.content == result:
            await message.channel.send(f"This person is currently AFK {}")
        

    await client.process_commands(message)

Next, we add the reason

    if message.mentions
    results = collection.find({"member": message.author.id}) 
    for result in results:
        collection.delete_one(result)
        if message.content == result:
            await message.channel.send(f"This person is currently AFK {reason}")
        

    await client.process_commands(message)  

and last, customize!

    if message.mentions
    results = collection.find({"member": message.author.id}) 
    for result in results:
        collection.delete_one(result)
        if message.content == result:
            await message.channel.send(f"This person is currently AFK, Reason? {reason}")
        

    await client.process_commands(message)

So that's it, in the future please refer to the the discord.py docs or even the python docs next time!

Python Docs: https://docs.python.org/3/ Discord.py Docs: https://discordpy.readthedocs.io/en/latest/

I hope I helped, please tell me if this is already fixed and about any issues!

Upvotes: 1

Related Questions