Reputation: 11
I am making a discord bot and I want to know how I can add a basic implementation of seeing which users are active daily on the server and how long they're active. The definition of active is not accurate but it could be if they sent a message in the group throughout the day. What would be the code to implement something like this in my discord bot? Thanks a lot!
Upvotes: 1
Views: 4414
Reputation: 89
Checking to see who has sent a message is simple enough to do,
You could use the on_message
function to see who has sent a message in your server, using this method you could get the user and append them to a list and then delete the list every so often (eg. daily, or you could wait for an admin to call a command within the server).
The positive side of this method is that you can control what counts as active, for example you could also use on_reaction_add
and its opposite.
This is a simple example of a bot that checks the users that have sent messages in the server and adds them to a list. Note: This script doesn't delete the list, meaning that members will be permanently in the active list, you should be able to implement this in your code fairly easily.
import discord
client = discord.Client()
activeMembers = [] #We create an empty list to hold the users that have been active
@client.event
async def on_message(message):
if message.author not in activeMembers:
activeMembers.append(message.author)
client.run("YOUR TOKEN HERE")
I've missed out some things that you probably have if you've started your bot already. If you haven't, fear not as there are multiple articles and tutorials on creating a bot.
The problem is that you want to see how long a member is active on your server, so what you could do is log the times they have sent messages, and then calculate the time they've been active on the server by deducting one value for another. This isn't accurate as you've stated, but is the best solution in my mind. The biggest problem here is that someone could send a message early in the morning and then go off about their day and then send a message at night, this would mean that the bot would think they are active for many hours (seeing as their first message was sent much much earlier) when in fact they are not. A simple workaround is to check every users last message time, and use a threshold time (if you will) to see who is no longer active.
For example, if someone sent their last message half an hour ago, they would no longer be seen as active and their total time would be calculated based on their first message and their last message. Their data would then be purged, so that if they send a message much later on, the bot doesn't use their first message of the day to calculate how long they've been on for.
Looking back on this this explanation is a bit shoddy, so Ill try quickly summarise it in a timeline sort of fashion.
A user sends a message in a channel and the bot records their id and the time they sent it -> They send another message five minutes later, the bot records the same data -> The user hasn't sent a message in half an hour (example of a threshold time, you could make yours shorter or longer) and the bot calculates their time and removes their message times. This time is then added to their total time.
This can be done using .create_task()
where you pass in a function to run in the background. The function you would pass would regularly check when users sent their last message.
Should you want to define active as things other than reactions or sending messages, visit discord.py to find out the different things you can do.
Best of luck,
Ben
Upvotes: 1