M
M
MIKHAN_GO2021-02-28 13:44:42
Python
MIKHAN_GO, 2021-02-28 13:44:42

Why does the function to edit the channel under online in discord.py not work?

The function to edit the channel does not work, in which it should be online:

@client.event
async def update_online():
    online_now = sum([0 if member.status == discord.status.offline else 1 for member in after.guild.members])
    channel = bot.get_channel(806502570696376390)
    await channel.edit(name=f"ONLINE: {online_now}")


It does not give an error, it just does not work, more precisely, the name of the channel does not change.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Nevzorov, 2021-02-28
@fixator10

https://discordpy.readthedocs.io/en/stable/api.htm...

  1. Event listeners should start withon_
  2. The event on_update_online()doesn't exist, you need it on_member_update(before, after): https://discordpy.readthedocs.io/en/stable/api.htm...
  3. Using sum() in an async context is most likely a bad idea
  4. Ratelimits allow you to edit the name and description of the channel only 2 times in ten minutes

In light of the above:
from datetime import datetime

...

last_time = datetime.now()

@client.event
async def on_member_update(before, after):
    if (datetime.now() - last_time).total_seconds() > 60*5:  # один раз в пять минут
        return
    last_time = datetime.now()
    online = len([m for m in after.guild.members if m.status != discord.Status.offline])
    await bot.get_channel(CHANNEL_ID).edit(name=f"Online: {online}")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question