Skip to content

Commit 8006434

Browse files
Rewrite top.gg integration to update every hour
1 parent 97ea226 commit 8006434

2 files changed

Lines changed: 55 additions & 26 deletions

File tree

src/main/java/technobot/TechnoBot.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
import net.dv8tion.jda.api.requests.GatewayIntent;
88
import net.dv8tion.jda.api.sharding.DefaultShardManagerBuilder;
99
import net.dv8tion.jda.api.sharding.ShardManager;
10-
import net.dv8tion.jda.api.utils.ChunkingFilter;
11-
import net.dv8tion.jda.api.utils.MemberCachePolicy;
1210
import okhttp3.OkHttpClient;
1311
import org.jetbrains.annotations.NotNull;
1412
import technobot.commands.CommandRegistry;

src/main/java/technobot/listeners/GuildListener.java

Lines changed: 55 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package technobot.listeners;
22

3+
import net.dv8tion.jda.api.JDA;
34
import net.dv8tion.jda.api.entities.Guild;
45
import net.dv8tion.jda.api.entities.Member;
56
import net.dv8tion.jda.api.entities.Role;
@@ -15,6 +16,8 @@
1516
import technobot.data.GuildData;
1617

1718
import java.io.IOException;
19+
import java.util.Timer;
20+
import java.util.TimerTask;
1821

1922
/**
2023
* Listens for guild events.
@@ -24,10 +27,14 @@
2427
*/
2528
public class GuildListener extends ListenerAdapter {
2629

30+
private static final long STATS_UPDATE_PERIOD = 3600000; //1 hour in millis
31+
2732
private final TechnoBot bot;
33+
private final Timer timer;
2834

2935
public GuildListener(TechnoBot bot) {
3036
this.bot = bot;
37+
this.timer = new Timer();
3138
}
3239

3340
/**
@@ -53,30 +60,9 @@ public void onGuildJoin(@NotNull GuildJoinEvent event) {
5360
public void onReady(@NotNull ReadyEvent event) {
5461
// Get Top.GG bearer token from .env
5562
String TOPGG_TOKEN = bot.config.get("TOPGG_TOKEN");
56-
if (TOPGG_TOKEN == null) return;
57-
58-
// Build post request to update server count
59-
RequestBody formBody = new FormBody.Builder()
60-
.add("server_count", String.valueOf(event.getGuildTotalCount()))
61-
.add("shard_count", String.valueOf(event.getJDA().getShardManager().getShardsTotal()))
62-
.build();
63-
String url = "https://top.gg/api/bots/" + event.getJDA().getSelfUser().getId() + "/stats";
64-
Request request = new Request.Builder()
65-
.url(url)
66-
.post(formBody)
67-
.addHeader("Authorization", "Bearer " + TOPGG_TOKEN)
68-
.build();
69-
Call call = bot.httpClient.newCall(request);
70-
71-
// Execute post request
72-
try {
73-
Response response = call.execute();
74-
if (response.code() != 200) {
75-
System.out.println("Warning: Unable to update Top.GG statistics! [code="+response.code()+"]");
76-
}
77-
response.close();
78-
} catch (IOException e) {
79-
System.out.println("ERROR IOException: Unable to update Top.GG statistics!");
63+
if (TOPGG_TOKEN != null) {
64+
// Update bot statistics at a fixed rate
65+
timer.scheduleAtFixedRate(new StatUpdateTask(event.getJDA(), TOPGG_TOKEN), 0, STATS_UPDATE_PERIOD);
8066
}
8167
}
8268

@@ -104,4 +90,49 @@ public void onGuildMemberJoin(@NotNull GuildMemberJoinEvent event) {
10490
}
10591
}
10692
}
93+
94+
/**
95+
* Timer task that updates bot statistics to Top.GG
96+
* Use in OnReady event every hour to send post request.
97+
*/
98+
private class StatUpdateTask extends TimerTask {
99+
100+
private final JDA jda;
101+
private final String TOPGG_TOKEN;
102+
103+
public StatUpdateTask(JDA jda, String TOPGG_TOKEN) {
104+
this.jda = jda;
105+
this.TOPGG_TOKEN = TOPGG_TOKEN;
106+
}
107+
108+
/**
109+
* Sends a post request to top.gg to update stats.
110+
*/
111+
@Override
112+
public void run() {
113+
// Build post request to update server count
114+
RequestBody formBody = new FormBody.Builder()
115+
.add("server_count", String.valueOf(jda.getGuilds().size()))
116+
.add("shard_count", String.valueOf(jda.getShardManager().getShardsTotal()))
117+
.build();
118+
String url = "https://top.gg/api/bots/" + jda.getSelfUser().getId() + "/stats";
119+
Request request = new Request.Builder()
120+
.url(url)
121+
.post(formBody)
122+
.addHeader("Authorization", "Bearer " + TOPGG_TOKEN)
123+
.build();
124+
Call call = bot.httpClient.newCall(request);
125+
126+
// Execute post request
127+
try {
128+
Response response = call.execute();
129+
if (response.code() != 200) {
130+
System.out.println("Warning: Unable to update Top.GG statistics! [code="+response.code()+"]");
131+
}
132+
response.close();
133+
} catch (IOException e) {
134+
System.out.println("ERROR IOException: Unable to update Top.GG statistics!");
135+
}
136+
}
137+
}
107138
}

0 commit comments

Comments
 (0)