Search code examples
pythondiscorddiscord.pysharding

Discord.py shard connection variable with print statement


KEEP IN MIND IM USING PYTHON NOT JAVASCRIPT

I am using discord.py and I recently added sharding to my bot. I really wanted to make it so that whenever a shard starts up it says in the console that shard number (number) started. That way I can know which shards are starting and it will be REALLY useful for the project that I am doing. This is my code for my sharding so far:

# creating the discord bot
client = commands.AutoShardedBot(shard_count=2, command_prefix = '$')

So I wanted something like a variable that I can say in a print statement kinda like this example below, I just need to know how to make a variable or something like that where I can put it in a print statement.

print("Shard number "+(shard_id_ready)+" has started.")

so that way if shard 5 started it would say

Shard number 5 has started.

I've looked everywhere and I can't find an example that is in python and not javascript

KEEP IN MIND IM USING PYTHON NOT JAVASCRIPT


Solution

  • You can use on_shard_connect and/or on_shard_ready. Ready is for when the shard is properly connected and is able to do things in discord, while connect is for when the shard gets to the connection to discord.

    @client.event
    async def on_shard_ready(shard_id):
        print(f'Shard #{shard_id} is ready')