Home>

I'm writing a function that changes the name of a channel when someone leaves or joins the server.
It displays the number of server members. But sometimes she lags and does not react to the fact that someone has come or left the server.
Here is the code:

@bot.event
async def on_member_join(member:discord.Member):
    CountChannel= bot.get_channel(727401938001461298)
    await CountChannel.edit(
            name= "Members: {}".format(len(member.guild.members))
        )
@bot.event
async def on_member_remove(member:discord.Member):
    CountChannel= bot.get_channel(727401938001461298)
    await CountChannel.edit(
            name= "Members: {}".format(len(member.guild.members))
        )

What's the problem? Maybe it's because my computer is slow?

  • Answer # 1

    Just make a loop where the bot will delete the channel and change its name to the current number of members when someone joins the server:

    @client.event
        async def on_member_join(member):
            overwrites={
                member.guild.default_role: discord.PermissionOverwrite(connect=False),
                member.guild.me: discord.PermissionOverwrite(connect=False)}
            category= await member.guild.create_category('Info')
            await member.guild.create_voice_channel('Members:'
                .format(member.guild.member_count), overwrites=overwrites, category=category)
            await channel.delete()
            # And so in a circle
    

    please try to leave a little more detailed answers. You can add to your answer by clicking edit.

    aleksandr barakin2022-02-03 18:52:52
  • Answer # 2

    Just make a loop where the bot will delete the channel and change its name to the current number of members when someone joins the server:

    @client.event
        async def on_member_join(member):
            overwrites={
                member.guild.default_role: discord.PermissionOverwrite(connect=False),
                member.guild.me: discord.PermissionOverwrite(connect=False)}
            category= await member.guild.create_category('Info')
            await member.guild.create_voice_channel('Members:'
                .format(member.guild.member_count), overwrites=overwrites, category=category)
            await channel.delete()
            # And so in a circle
    

    please try to leave a little more detailed answers. You can add to your answer by clicking edit.

    aleksandr barakin2022-02-03 18:52:52
  • Answer # 3

    Discord has recently had a limit of 2 channel title/description edits per 10 minutes:

    After two edits, "ratelimit" is triggered and the library delays the channel change until the delay ends.

    Possible solutions:

    • Opt out of channels with statistics
    • Add a check for the time elapsed since the channel was last modified
    • Change the channel every 5 minutes, for example withdiscord.ext.tasks:
    from discord.ext import tasks
    ...
    update_required=false
    @bot.event
    async def on_member_join(member:discord.Member):
        update_required=true
    @bot.event
    async def on_member_remove(member:discord.Member):
        update_required=true
    @tasks.loop(minutes=5)
    async def stats_update():
        if update_required:
            count_channel= bot.get_channel(727401938001461298) # CamelCase usually stands for Classes: https://www.python.org/dev/peps/pep-0008/#prescriptive-naming-conventions
            await count_channel.edit(name= f"Members: {len(channel.guild.members)}")
            update_required=false
    stats_update.start()
    
  • Answer # 4

    Discord has recently had a limit of 2 channel title/description edits per 10 minutes:

    After two edits, "ratelimit" is triggered and the library delays the channel change until the delay ends.

    Possible solutions:

    • Opt out of channels with statistics
    • Add a check for the time elapsed since the channel was last modified
    • Change the channel every 5 minutes, for example withdiscord.ext.tasks:
    from discord.ext import tasks
    ...
    update_required=false
    @bot.event
    async def on_member_join(member:discord.Member):
        update_required=true
    @bot.event
    async def on_member_remove(member:discord.Member):
        update_required=true
    @tasks.loop(minutes=5)
    async def stats_update():
        if update_required:
            count_channel= bot.get_channel(727401938001461298) # CamelCase usually stands for Classes: https://www.python.org/dev/peps/pep-0008/#prescriptive-naming-conventions
            await count_channel.edit(name= f"Members: {len(channel.guild.members)}")
            update_required=false
    stats_update.start()