I
I
IEVEN2021-03-14 16:45:14
Python
IEVEN, 2021-03-14 16:45:14

(discord.py) How to check how long a person has been in voice channels on a discord server?

I am writing a bot in Python, discord.py library

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-03-15
@Vindicar

1. Reading https://discordpy.readthedocs.io/en/latest/api.htm... . It lists all the events that can be caught through discord.py. We are interested in discord.on_voice_state_update(member, before, after) , it allows you to react to the entry and exit of users on voice channels. Also important: when starting the bot, you will need to specify the intent Intents.voice_states , otherwise we will not receive the event.
2. Understanding how to compare the `before` and `after` parameters in order to understand whether the user has entered or exited. Both are a VoiceState structure .
3. You describe the data structure where you store the time of the user's last entry into the voice channel. For example, a dictionary where user idon the server is the key and the time is the value. If you need independent registration across channels, then the channel id should be part of the key.
4. At the user's entrance, you make an entry in this dictionary. At the user's exit, you look for an entry, find the difference between the remembered entry time and the current time, delete the entry. Provide a case if there is no record.
5. PROFIT. With this difference, you already do what you want, you enter it into the database, or something else.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question