#πŸ”’ Bot isn't cleaning the AFK Status after sending a message.

56 messages Β· Page 1 of 1 (latest)

marsh mulch
#
    @commands.command(name="afk")
    async def afk(self, ctx, *, afk_message: str = "No reason provided."):
        if ctx.author.id not in self.afk_users:  
            self.afk_users[ctx.author.id] = afk_message
            embed = discord.Embed(title=f"{ctx.author.display_name} is now AFK", description=f"> Reason: {afk_message}")
            embed.set_footer(text='Type ",remove" to remove the AFK status.')
            await ctx.send(embed=embed)
        else:
            embed = discord.Embed(title="Error", description="You are already AFK.")
            await ctx.send(embed=embed)

    @commands.Cog.listener()
    async def on_message(self, message):
        if not message.author.bot:
            if message.author.id in self.afk_users:
                afk_info = self.afk_users[message.author.id]
                embed = discord.Embed(title=f"> Welcome back, {message.author.display_name}!", description=f"Your AFK status has been removed.\n\nOriginal message: {afk_info}")
                await message.channel.send(embed=embed)
                del self.afk_users[message.author.id]

            for mention in message.mentions:
                if mention.id in self.afk_users:
                    afk_info = self.afk_users[mention.id]
                    embed = discord.Embed(title=f"{mention.display_name} is currently AFK", description=f"> Reason: {afk_info}")
                    embed.set_footer(text='Type ",remove" to remove the AFK status.')
                    await message.channel.send(embed=embed)```

Here is my code. There is the cog listener, but when the AFK user sends a message, it doesn't remove the AFK status. I have a command named ",remove" for it but I really want to get rid of it and make the AFK command more simple.
spring adderBOT
#

@marsh mulch

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

thorny basin
#

does the embed send successfully?

marsh mulch
#

and there is no error messages

thorny basin
#

is self.afk_users a dict or a list?

#

if it is a dictionary you might need to convert the user ID to a string

marsh mulch
thorny basin
#

try converting the user ID to a string and then looking it up

thorny basin
marsh mulch
# thorny basin try converting the user ID to a string and then looking it up
@commands.command(name="afk")
    async def afk(self, ctx, *, afk_message: str = "No reason provided."):
        if str(ctx.author.id) not in self.afk_users: 
            self.afk_users[str(ctx.author.id)] = afk_message
            embed = discord.Embed(title=f"{ctx.author.display_name} is now AFK", description=f"> Reason: {afk_message}")
            embed.set_footer(text='Type ",remove" to remove the AFK status.')
            await ctx.send(embed=embed)
        else:
            embed = discord.Embed(title="Error", description="You are already AFK.")
            await ctx.send(embed=embed)

    @commands.Cog.listener()
    async def on_message(self, message):
        if not message.author.bot:
            if str(message.author.id) in self.afk_users:  
                afk_info = self.afk_users[str(message.author.id)]
                embed = discord.Embed(title=f"> Welcome back, {message.author.display_name}!", description=f"Your AFK status has been removed.\n\nOriginal message: {afk_info}")
                await message.channel.send(embed=embed)
                del self.afk_users[str(message.author.id)]

            for mention in message.mentions:
                if str(mention.id) in self.afk_users: 
                    afk_info = self.afk_users[str(mention.id)]
                    embed = discord.Embed(title=f"{mention.display_name} is currently AFK", description=f"> Reason: {afk_info}")
                    embed.set_footer(text='Type ",remove" to remove the AFK status.')
                    await message.channel.send(embed=embed)```
#

It didn't work.

#

is there any issues with the code?

thorny basin
#

ok so no embed sent?'

marsh mulch
#

yeah still no embed

thorny basin
#

are the indentations correct for everything?

marsh mulch
#

and when i try to do ,afk when i am already, it says that im already afk

marsh mulch
#

maybe theres another issues that causes this with the rest of the code i think i should rewrite

thorny basin
#

if you put everything in a giant try/except statement does anything get thrown?

#

and does the bot have the proper intents?

marsh mulch
#

yeah it does have the intents

thorny basin
#

ok

#

walk me through what you are doing to test it

marsh mulch
#

So basically i used the afk command which ran successfully and then tried to send a message to see if the bot's gonna remove my afk status as I wanted but it didn't.

#

And like I said theres no error messages or the embed

#

and it says that im already afk when i try to use ,afk command again which proves that the listener didn't work.

thorny basin
#

ok

#

and if you ping yourself does it send the currently afk message

marsh mulch
#

Oh

#

it used to do before i convert the user ID

#

to a string

#

but yeah it doesn't now

thorny basin
#

wait it worked when it was an int?

marsh mulch
#

it looks like yeah

#

but it wasnt removing my afk

thorny basin
#

you are sure that self.afk_users is a dict?

marsh mulch
#

100%

thorny basin
#

!e py my_dict = {650923352097292299: "No reason given"} print(my_dict)

spring adderBOT
#

@thorny basin :white_check_mark: Your 3.12 eval job has completed with return code 0.

{650923352097292299: 'No reason given'}
thorny basin
#

WHAT

marsh mulch
#

😭

thorny basin
#

everything is a lie

#

ok apparently not converting to string is fine then

marsh mulch
#

i guess

thorny basin
#

can you change py if str(message.author.id) in self.afk_users: afk_info = self.afk_users[str(message.author.id)] embed = discord.Embed(title=f"> Welcome back, {message.author.display_name}!", description=f"Your AFK status has been removed.\n\nOriginal message: {afk_info}") await message.channel.send(embed=embed) del self.afk_users[str(message.author.id)] to py if str(message.author.id) in self.afk_users: print("afk user detected") afk_info = self.afk_users[str(message.author.id)] print(afk_info) embed = discord.Embed(title=f"> Welcome back, {message.author.display_name}!", description=f"Your AFK status has been removed.\n\nOriginal message: {afk_info}") await message.channel.send(embed=embed) print("sent embed") del self.afk_users[str(message.author.id)] print("removed user from dict")

#

for debug reasons

marsh mulch
#

yeah its not printing anything..

#

It's not a big deal for me to rewrite the entire cog, theres nothing much in there

#

ill open another ticket if i experience any other issues after rewriting the cog

thorny basin
#

make sure the keys are either all int or all string

marsh mulch
#

πŸ‘

#

!close

spring adderBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.