-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlog.java
More file actions
148 lines (119 loc) · 4.87 KB
/
log.java
File metadata and controls
148 lines (119 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package database;
import com.google.api.core.ApiFuture;
import com.google.cloud.firestore.*;
import io.github.ccincharge.newsapi.datamodels.Article;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent;
import java.util.*;
public class log {
public static void logMsg(Firestore db, MessageReceivedEvent event){
System.out.println("Logging...");
DocumentReference docRef = db.collection("Korusuke-Bot").document(event.getGuild().getName());
docRef = docRef.collection("AllMsg").document(event.getMessageId());
// Add document data with id "alovelace" using a hashmap
Map<String, Object> data = new HashMap<>();
data.put("Author", event.getAuthor().getName());
data.put("Message", event.getMessage().getContentRaw());
data.put("Time", "" + event.getMessage().getCreationTime());
//asynchronously write data
ApiFuture<WriteResult> result = docRef.set(data);
// ...
// result.get() blocks on response
}
public static void updatepts(Firestore db, MessageReceivedEvent event){
System.out.println("Calculating pts based on K1506-formula created by my master...");
DocumentReference docRef = db.collection("Korusuke-Bot").document(event.getGuild().getName());
docRef = docRef.collection("UserPts").document(event.getAuthor().getName());
ApiFuture<DocumentSnapshot> future = docRef.get();
try {
DocumentSnapshot document = future.get();
int points = event.getMessage().getContentRaw().length();
if(document.exists()){
points = points + Math.toIntExact(document.getLong("Points"));
docRef.update("Points", points);
}else{
Map<String, Object> data = new HashMap<>();
data.put("Author", event.getAuthor().getName());
data.put("Points", points);
//asynchronously write data
ApiFuture<WriteResult> result = docRef.set(data);
}
} catch (Exception e){
System.out.println("Error: " + e.getMessage());
}
}
public static void update_ranks(Firestore db, MessageReceivedEvent event){
//asynchronously retrieve all documents
DocumentReference docRef = db.collection("Korusuke-Bot").document(event.getGuild().getName());
ApiFuture<QuerySnapshot> future = docRef.collection("UserPts").get();
// future.get() blocks on response
List<users> guys = new ArrayList<>();
try{
List<QueryDocumentSnapshot> documents = future.get().getDocuments();
for (QueryDocumentSnapshot document : documents) {
Map<String, Object> data = document.getData();
users guy = new users();
String name;
int points;
for (Map.Entry<String, Object> entry : data.entrySet()){
if(entry.getKey().equals("Author")){
name = String.valueOf(entry.getValue());
guy.setName(name);
}
else if(entry.getKey().equals("Points")){
points = Integer.parseInt(String.valueOf(entry.getValue()));
guy.setRank(points);
}
System.out.println("Key: "+ entry.getKey() + "\nValue: " + entry.getValue());
}
guys.add(guy);
}
}
catch(Exception e){
System.out.println("Error: " + e.getMessage());
}
Collections.sort(guys);
for(int i=0; i<guys.size(); i++){
if(event.getAuthor().getName().equals(guys.get(i).getName())){
docRef = db.collection("Korusuke-Bot").document(event.getGuild().getName());
docRef = docRef.collection("UserPts").document(event.getAuthor().getName());
Map<String, Object> data = new HashMap<>();
data.put("Rank", guys.size() - i);
docRef.update(data);
}
System.out.println("Name: "+guys.get(i).getName()+" Points: "+guys.get(i).getRank());
}
}
}
class users implements Comparable{
String name;
int rank;
public String getName() {
return name;
}
public int getRank() {
return rank;
}
public void setName(String name) {
this.name = name;
}
public void setRank(int rank) {
this.rank = rank;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", rank=" + rank +
'}';
}
@Override
public int compareTo(Object o) {
if (this.getRank() < ((users) o).getRank()) {
return -1;
}
if (this.getRank() == ((users) o).getRank()) {
return 0;
}
return 1;
}
}