Discord bot commands not working

Question: Discord commands not working, on ready() event not working

Repl link: JoyfulHardtofindWamp - Replit

image

code snippet

Hi @VictorGeorge1 !
In what way are the command not working? Could you elaborate further?
Also, is it possible that your bot does not respond as it isn’t deployed?

1 Like

Hello,

I recently deployed my bot with Reserved VM and it also wasn’t responding to commands, whether or not the bot is deployed. This just happened recently, the last time it could execute commands I believe was Feb 1st the last time I received the message on Discord that my bot is on. The on_ready() statement should print out a message to the console, and it doesn’t. The bot is on but there’s no action when I use the commands.

Regards,

Victor

Did you edit the bot’s code since then?
Try re-deploying the bot.

No, I don’t think I changed anything. I just redeployed my project and I’m still having the same issue.

Victor

Is there anything in the code which would explain why the bot is not executing commands or executing the on_ready() statement? I thought it was related to deployment but now even if its not deployed, it’s not working.

Victor

Usually this kinda of thing happens when the token is wrong or it gets changed or the bot permissions on the server change too.
Did you verifiy if the token you are using is the same as the one on the discord developer portal?

Other than that you will have to debug your code to find out what is happening.

1 Like

None of this worked. Used a new token matching the one on developer portal and still same issue.
Debugger isn’t showing anything.

Wrap the client2.run(os.getenv('TOKEN')) call in a try-except block (in your join.py file) to catch runtime errors. Run your bot again and post the results.

import discord
import os

class MyClient(discord.Client):
    async def on_ready(self):
        print('Logged in as')
        print(self.user.name)
        print(self.user.id)
        print('------')

    async def on_member_join(self, member):
        arrival_channel = client2.get_channel(REDACTED) #join-logs channel
        await arrival_channel.send(member.mention + "Website: http://wtetour.com Please type **dc.verify** into this channel to complete the verification process.**You will receive a private message from the Double Counter bot, after typing this message into the channel, asking to verify your account. You must complete the verification before an admin can assign you a role.** Please wait to be assigned a role, an admin (#admins) will contact you or assign you a role shortly or you can private message one of them asking to be assigned a role.")

intents = discord.Intents.default()
intents.members = True

client2 = MyClient(intents=intents)

try:
    client2.run(os.getenv('TOKEN'))
except Exception as e:
    print(f"An error occurred: {e}")
2 Likes

nothing happened when i did this

Here’s an example bot using discord.py:

from discord.ext.commands import AutoShardedBot
from discord import Intents

class Example(AutoShardedBot):
    def __init__(self):
        super().__init__(
            command_prefix="!",
            description="test",
            help_command=None,
            intents=Intents.all(),
            shard_count=1
        )
        self.run()

    def run(self) -> None:
        super().run(
            token="",
            reconnect=True,
        )

    async def on_ready(self):
        print(f"Logged in as {self.user}\n-----------\n")

You’d call it as:

Example() (since run() is ran when the class instance is initialized, you don’t have to do like bot.run(), just call the class Example() and the bot will auto-start.

Still isn’t solving the problem, I added this example in comments ‘’’ code ‘’’ just above the original block, which I’ve reverted to as this didn’t solve my issue. Not sure if I constructed it correctly

join.py somehow works now but not main.py

Which error is giving?

It works fine for me. If you’re trying to create commands, you just need to develop a new function and utilize the decorator @command (@commands.command). Proceed as usual, ensuring that you include a self argument. For example: def userinfo(self, ctx, user: discord.User)

No error, but the command is executing when someone joins the server

1 Like

Can you show me the current event you’re using for tracking who joined the server?

I think I fixed it, my guess is that the import join.py imported a block of code which is already in main.py but had a token and discord client which conflicted with the one in main.py. So I commented out the import join.py line in main.py.

Now I have to tackle deployments. My bot no longer stays on for a period of time. I thought the reserve VM deployment would keep it on, but it doesn’t stay on.

1 Like

the event is on_member_join()

1 Like

There are easy ways to solve this issue. Use discord.py cogs; don’t shove everything into one file. I create cogs for each module, enabling easy fixing and effectively resolving most issues people face.