How to Count the Number of Messages

How can I count the number of messages in one channel of each user?

The search is not ( yet ) avaible for Bots.

You could Channel#fetchMessages but this would be very Api intensive.

Best way would be to get a database and increment it when the user sends a message, but that will only count messages when the bot is running.

How to count incoming messages from a "stream" in Java?

Your code actully runs fo 1 second (1000ms, I don't know if this is a typo or a voluntary simplification). One other problem is that it calls end = getMessagesRecievedCount(); repeatedly inside a while loop, while you actually need only the starting and final value. A way to solve this would be using Thread.sleep() (if you never need to cancel the counting midway):

    public void getMessagesPerMinute(){
float start = getMessagesRecievedCount();
float end = 0;
try{
Thread.sleep(10000);
}
catch(InterruptedException e){
System.out.println("do something");
}
end = getMessagesRecievedCount();
System.out.println("start: "+start+" end: "+end+
"Total messages: "+ (end-start)+"\n");
}

For blocking the important thing is that this code runs in a different thread that the one updating the value of messagesRecievedCount or doing other things that you may want to do in the meanwhile, so calling it inside a new thread is probably the best solution. I'm not familiar with the framework you are using so it may be already using different threads that better suit this purpose.

If you intend to do something more with the variable messagesRecievedCount some synchronization would be required, but for an estimate of the number of messages for minute this should be good enough.

Here is some test code I used that you can hopefully adapt to better suit your case and play with to pinpoint the problem. The difference is quite constant in this case, but the values are clearly updated. Making the ExampleClient instance public is a shortcut which should probaby be avoided in the actual code.

public class Test{

public static ExampleClient example=new ExampleClient();

public static void main(String[] args){
Thread a=new MessagesPerTenSecondFetcher();
Thread b=new MessagesPerTenSecondFetcher();
Thread c=new MessagesPerTenSecondFetcher();
Thread d= new MessageProducer();
a.start();
d.start();
b.start();
try{
Thread.sleep(2000);
}
catch(InterruptedException e){
System.out.println("do something");
}
c.start();
}
}

class ExampleClient {
private float messagesRecievedCount;

public void onMessage(String s) {
setMessagesRecievedCount(getMessagesRecievedCount() + 1);
}

public void getMessagesPerMinute(){
float start = getMessagesRecievedCount();
float end = 0;
try{
Thread.sleep(10000);
}
catch(InterruptedException e){
System.out.println("do something");
}
end = getMessagesRecievedCount();
System.out.println("start: "+start+" end: "+end+
"Total messages: "+ (end-start)+"\n");
}

public float getMessagesRecievedCount() {
return messagesRecievedCount;
}

public void setMessagesRecievedCount(float messagesRecievedCount) {
this.messagesRecievedCount = messagesRecievedCount;
}
}

class MessagesPerTenSecondFetcher extends Thread{
@Override
public void run(){
Test.example.getMessagesPerMinute();
}
}

class MessageProducer extends Thread{
@Override
public void run(){
for(int i =0; i<100;i++){
Test.example.onMessage("a");
try{
Thread.sleep(130);
}
catch(InterruptedException e){
System.out.println("do something");
}
}
}
}

How to count the number of messages?

That's the job for collections.Counter:

from collections import Counter
counter = Counter(item['userId'] for item in items)
print(counter)

Count the number of messages for specific user jda

You actually never put anything into playerMessages map.
Replace

private void setPlayerMessages(User member, int num){
playerXp.put(member, num);
}

with

private void setPlayerMessages(User member, int num){
playerMessages.put(member, num);
}

Count messages from a channel

Two ways to do this.

Cloning

You can clone the TextChannel using TextChannel.clone.
This stops you having the 14 day limitation using BulkDelete.

const newChannel = await message.channel.clone();
message.channel.delete();

newChannel.send('The channel has been purged.');
BulkDelete

You can delete all the messages in the last 14 days very easily.

await message.channel.messages.fetch();
const messageCount = message.channel.messages.cache.map(x => x).length;
message.channel.bulkDelete(messageCount, true);

Note: Not the most elegant solutions since it's late at night but this should help you out.



Related Topics



Leave a reply



Submit