Skip to content

Commit

Permalink
update top.gg stat handling
Browse files Browse the repository at this point in the history
- use own fork to fix pr Top-gg-Community/java-sdk#18
- update stats for all shards on startup
- use callback to log errors and success
  • Loading branch information
robinfriedli committed Jul 25, 2023
1 parent dfa5932 commit e7038ab
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 24 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ dependencies {
implementation "org.apache.commons:commons-collections4:4.4"
implementation "org.apache.commons:commons-text:1.9"
implementation "org.codehaus.groovy:groovy-all:3.0.10"
implementation "com.github.DiscordBotList:DBL-Java-Library:2.0.1"
implementation "com.github.robinfriedli:topgg-java-sdk:1a1af5964a"
implementation "org.ehcache:ehcache:3.9.7"
implementation "org.glassfish:javax.el:3.0.1-b11"
implementation "org.hibernate:hibernate-core:5.6.1.Final"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import org.slf4j.LoggerFactory;

import com.google.common.base.Strings;
import net.dv8tion.jda.api.JDA;
import org.discordbots.api.client.DiscordBotListAPI;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -13,13 +15,14 @@
public class TopGGComponent {

private final Logger logger = LoggerFactory.getLogger(getClass());
@Value("${aiode.tokens.discord_bot_id}")
private String discordBotId;
@Value("${aiode.tokens.topgg_token}")
private String topGGToken;

@Bean
public DiscordBotListAPI getDiscordBotListAPI() {
private final String discordBotId;
private final String topGGToken;

@Nullable
private final DiscordBotListAPI discordBotListAPI;

public TopGGComponent(@Value("${aiode.tokens.discord_bot_id}") String discordBotId, @Value("${aiode.tokens.topgg_token}") String topGGToken) {
DiscordBotListAPI discordBotListAPI;
if (!Strings.isNullOrEmpty(discordBotId) && !Strings.isNullOrEmpty(topGGToken)) {
discordBotListAPI = new DiscordBotListAPI.Builder()
Expand All @@ -30,7 +33,41 @@ public DiscordBotListAPI getDiscordBotListAPI() {
logger.info("top.gg api not set up, missing properties");
discordBotListAPI = null;
}
this.discordBotListAPI = discordBotListAPI;
this.discordBotId = discordBotId;
this.topGGToken = topGGToken;
}

@Nullable
@Bean
public DiscordBotListAPI getDiscordBotListAPI() {
return discordBotListAPI;
}

public void updateStatsForShard(JDA shard) {
if (discordBotListAPI != null) {
try {
JDA.ShardInfo shardInfo = shard.getShardInfo();
discordBotListAPI.setStats(shardInfo.getShardId(), shardInfo.getShardTotal(), (int) shard.getGuildCache().size())
.whenComplete(((unused, throwable) -> {
if (throwable != null) {
logger.error("Exception setting discordBotListAPI stats", throwable);
} else {
logger.info("Updated top.gg stats for shard " + shard);
}
}));
} catch (Exception e) {
logger.error("Exception setting discordBotListAPI stats", e);
}
}
}

public String getDiscordBotId() {
return discordBotId;
}

public String getTopGGToken() {
return topGGToken;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package net.robinfriedli.aiode.boot.tasks;

import java.util.Objects;

import net.dv8tion.jda.api.JDA;
import net.robinfriedli.aiode.boot.StartupTask;
import net.robinfriedli.aiode.boot.configurations.TopGGComponent;
import net.robinfriedli.aiode.entities.xml.StartupTaskContribution;
import org.jetbrains.annotations.Nullable;

public class UpdateTopGGStatsTask implements StartupTask {

private final StartupTaskContribution contribution;
private final TopGGComponent topGGComponent;

public UpdateTopGGStatsTask(StartupTaskContribution contribution, TopGGComponent topGGComponent) {
this.contribution = contribution;
this.topGGComponent = topGGComponent;
}

@Override
public void perform(@Nullable JDA shard) throws Exception {
topGGComponent.updateStatsForShard(Objects.requireNonNull(shard));
}

@Override
public StartupTaskContribution getContribution() {
return contribution;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.Objects;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaDelete;
import javax.persistence.criteria.Root;
Expand All @@ -12,7 +11,6 @@
import org.slf4j.LoggerFactory;

import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
Expand All @@ -25,6 +23,7 @@
import net.robinfriedli.aiode.Aiode;
import net.robinfriedli.aiode.boot.SpringPropertiesConfig;
import net.robinfriedli.aiode.boot.configurations.HibernateComponent;
import net.robinfriedli.aiode.boot.configurations.TopGGComponent;
import net.robinfriedli.aiode.command.commands.customisation.RenameCommand;
import net.robinfriedli.aiode.concurrent.CommandExecutionQueueManager;
import net.robinfriedli.aiode.concurrent.EventHandlerPool;
Expand All @@ -33,7 +32,6 @@
import net.robinfriedli.aiode.discord.MessageService;
import net.robinfriedli.aiode.discord.property.properties.ColorSchemeProperty;
import net.robinfriedli.aiode.entities.GrantedRole;
import org.discordbots.api.client.DiscordBotListAPI;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Component;

Expand All @@ -44,20 +42,19 @@
public class GuildManagementListener extends ListenerAdapter {

private final CommandExecutionQueueManager executionQueueManager;
@Nullable
private final DiscordBotListAPI discordBotListAPI;
private final GuildManager guildManager;
private final HibernateComponent hibernateComponent;
private final Logger logger;
private final MessageService messageService;
private final TopGGComponent topGGComponent;

public GuildManagementListener(CommandExecutionQueueManager executionQueueManager,
@Nullable DiscordBotListAPI discordBotListAPI,
GuildManager guildManager,
HibernateComponent hibernateComponent,
MessageService messageService) {
MessageService messageService,
TopGGComponent topGGComponent) {
this.executionQueueManager = executionQueueManager;
this.discordBotListAPI = discordBotListAPI;
this.topGGComponent = topGGComponent;
this.guildManager = guildManager;
this.hibernateComponent = hibernateComponent;
logger = LoggerFactory.getLogger(getClass());
Expand Down Expand Up @@ -149,15 +146,7 @@ public void onGuildMemberUpdateNickname(@Nonnull GuildMemberUpdateNicknameEvent
}

private void updateDiscordBotsApiStats(Event event) {
if (discordBotListAPI != null) {
try {
JDA jda = event.getJDA();
JDA.ShardInfo shardInfo = jda.getShardInfo();
discordBotListAPI.setStats(shardInfo.getShardId(), shardInfo.getShardTotal(), (int) jda.getGuildCache().size());
} catch (Exception e) {
logger.error("Exception setting discordBotListAPI stats", e);
}
}
topGGComponent.updateStatsForShard(event.getJDA());
}

}
1 change: 1 addition & 0 deletions src/main/resources/xml-contributions/startupTasks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
<startupTask runForEachShard="false" implementation="net.robinfriedli.aiode.boot.tasks.InitialiseCommandContributionsTask"/>
<startupTask runForEachShard="true" implementation="net.robinfriedli.aiode.boot.tasks.UpsertSlashCommandsTask"/>
<startupTask runForEachShard="false" mainInstanceOnly="true" implementation="net.robinfriedli.aiode.boot.tasks.RefreshPersistentGlobalChartsStartupTask"/>
<startupTask runForEachShard="true" implementation="net.robinfriedli.aiode.boot.tasks.UpdateTopGGStatsTask"/>
</startupTasks>

0 comments on commit e7038ab

Please sign in to comment.