The community-maintained Java library for Top.gg.
Replace VERSION here with the latest version or commit hash. The latest version can be found under GitHub releases.
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.github.top-gg</groupId>
<artifactId>java-sdk</artifactId>
<version>VERSION</version>
</dependency>
</dependencies>repositories {
maven { url 'https://jitpack.io' }
}
dependencies {
compile 'com.github.top-gg:java-sdk:VERSION'
}final DiscordBotListAPI client = new DiscordBotListAPI
.Builder()
.token(System.getEnv("TOPGG_TOKEN"))
.botId("BOT_ID")
.build();final Bot bot = client.getBot("264811613708746752").toCompletableFuture().get();final BotResult result = client.getBots().toCompletableFuture().get();
final List<Bot> bots = result.getResults();// Limit Offset Sort by
final BotResult result = client.getBots(50, 0, "date").toCompletableFuture().get();
final List<Bot> bots = result.getResults();final List<SimpleUser> voters = client.getVoters().toCompletableFuture().get();final List<SimpleUser> voters = client.getVoters(2).toCompletableFuture().get();final boolean hasVoted = client.hasVoted("661200758510977084").toCompletableFuture().get();final long serverCount = client.getServerCount().toCompletableFuture().get();client.postServerCount(bot.getServerCount()).toCompletableFuture().get();client.startAutoposter(() -> {
return bot.getServerCount();
});client.startAutoposter(() -> {
return bot.getServerCount();
}, new BiConsumer<>() {
@Override
public void accept(Integer serverCount, Throwable error) {
if (serverCount != null) {
System.out.println("Successfully posted " + serverCount + " servers to Top.gg!");
} else {
System.err.println("Post error: " + error.getMessage());
client.stopAutoposter();
}
}
});// Posts once every 30 minutes
client.startAutoposter(1800, () -> {
return bot.getServerCount();
}, new BiConsumer<>() {
@Override
public void accept(Integer serverCount, Throwable error) {
if (serverCount != null) {
System.out.println("Successfully posted " + serverCount + " servers to Top.gg!");
} else {
System.err.println("Post error: " + error.getMessage());
client.stopAutoposter();
}
}
});final boolean multiplierActive = client.getVotingMultiplier().toCompletableFuture().get().isWeekend();final String widgetUrl = Widget.large(Widget.Type.DISCORD_BOT, "574652751745777665");final String widgetUrl = Widget.votes(Widget.Type.DISCORD_BOT, "574652751745777665");final String widgetUrl = Widget.owner(Widget.Type.DISCORD_BOT, "574652751745777665");final String widgetUrl = Widget.social(Widget.Type.DISCORD_BOT, "574652751745777665");In your TopggWebhookFilterConfig.java:
import org.discordbots.api.client.entity.Vote;
import org.discordbots.api.client.webhooks.SpringBoot;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class TopggWebhookFilterConfig {
@Bean
public FilterRegistrationBean<SpringBoot<Vote>> registerVoteWebhook() {
final FilterRegistrationBean<SpringBoot<Vote>> registrationBean = new FilterRegistrationBean<>();
registrationBean.setFilter(new SpringBoot<>(Vote.class, System.getenv("MY_TOPGG_WEBHOOK_SECRET")) {
@Override
public void callback(final Vote vote) {
System.out.println("A user with the ID of " + vote.getVoterId() + " has voted us on Top.gg!");
}
});
registrationBean.addUrlPatterns("/votes");
registrationBean.setOrder(1);
return registrationBean;
}
}In your MyVoteListener.java:
import org.discordbots.api.client.entity.Vote;
import org.discordbots.api.client.webhooks.Dropwizard;
import jakarta.ws.rs.Path;
@Path("/votes")
public class MyVoteListener extends Dropwizard<Vote> {
public MyVoteListener() {
super(Vote.class, System.getenv("MY_TOPGG_WEBHOOK_SECRET"));
}
@Override
public void callback(final Vote vote) {
System.out.println("A user with the ID of " + vote.getVoterId() + " has voted us on Top.gg!");
}
}In your MyServer.java:
import io.dropwizard.core.Application;
import io.dropwizard.core.Configuration;
import io.dropwizard.core.setup.Environment;
import io.dropwizard.jersey.setup.JerseyEnvironment;
public class MyServer extends Application<Configuration> {
public static void main(String[] args) throws Exception {
new MyServer().run(args);
}
@Override
public void run(Configuration config, Environment env) {
final JerseyEnvironment jersey = env.jersey();
jersey.register(new MyVoteListener());
}
}In your MyServer.java:
import org.discordbots.api.client.entity.Vote;
import org.discordbots.api.client.webhooks.EclipseJetty;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
public class MyServer {
public static void main(String[] args) throws Exception {
final Server server = new Server(8080);
final ServletContextHandler handler = new ServletContextHandler();
handler.setContextPath("/");
handler.addServlet(new ServletHolder(new EclipseJetty<>(Vote.class, System.getenv("MY_TOPGG_WEBHOOK_SECRET")) {
@Override
public void callback(final Vote vote) {
System.out.println("A user with the ID of " + vote.getVoterId() + " has voted us on Top.gg!");
}
}), "/votes");
server.setHandler(handler);
server.start();
server.join();
}
}