@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.
#π Bot isn't cleaning the AFK Status after sending a message.
56 messages Β· Page 1 of 1 (latest)
@marsh mulch
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.
Closes after a period of inactivity, or when you send !close.
does the embed send successfully?
no it doesn't send the embed either
and there is no error messages
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
its a dict
yeah so the key cannot be a number
try converting the user ID to a string and then looking it up
sometimes discord.py can be annoying and catch some of the exceptions thrown by a command
@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?
ok so no embed sent?'
yeah still no embed
are the indentations correct for everything?
and when i try to do ,afk when i am already, it says that im already afk
yeah i checked everything
maybe theres another issues that causes this with the rest of the code i think i should rewrite
if you put everything in a giant try/except statement does anything get thrown?
and does the bot have the proper intents?
yeah it does have the intents
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.
Oh
it used to do before i convert the user ID
to a string
but yeah it doesn't now
wait it worked when it was an int?
you are sure that self.afk_users is a dict?
100%
!e py my_dict = {650923352097292299: "No reason given"} print(my_dict)
@thorny basin :white_check_mark: Your 3.12 eval job has completed with return code 0.
{650923352097292299: 'No reason given'}
WHAT
π
i guess
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
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
make sure the keys are either all int or all string
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.