Skip to content

Commit

Permalink
Mirgated from Javacord to JDA (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
RealYusufIsmail authored Aug 29, 2024
1 parent d963aa0 commit ea7ba11
Show file tree
Hide file tree
Showing 62 changed files with 1,495 additions and 1,671 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.0.4] - 28/08/2024
## [0.0.4] - 29/08/2024

### Fixed some minor text issues with the bot info command.
### Improved performance of the bot.
### Fixed some issues with the serp API. (Currently limited to my server)
### Migrated from Javacord to JDA.
### Added new roll dice and trivia commands.

## [0.0.3] - 19/08/2024

Expand Down
3 changes: 1 addition & 2 deletions DiscordBot/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ plugins {

dependencies {
// JavaCord and related dependencies
implementation("org.javacord:javacord:3.8.0")
implementation("org.javacord:javacord-core:3.8.0")
implementation("net.dv8tion:JDA:5.1.0")
implementation("io.github.realyusufismail:jconfig:1.1.2")
implementation("io.github.classgraph:classgraph:4.8.171")
implementation("net.fellbaum:jemoji:1.4.1")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import com.zaxxer.hikari.HikariDataSource;
import io.github.yusufsdiscordbot.mystiguardian.api.SerpAPI;
import io.github.yusufsdiscordbot.mystiguardian.button.ButtonClickHandler;
import io.github.yusufsdiscordbot.mystiguardian.commands.moderation.util.UnbanCheckThread;
import io.github.yusufsdiscordbot.mystiguardian.database.MystiGuardianDatabase;
import io.github.yusufsdiscordbot.mystiguardian.event.EventDispatcher;
Expand All @@ -34,20 +33,20 @@
import io.github.yusufsdiscordbot.mystiguardian.utils.MystiGuardianUtils;
import io.github.yusufsdiscordbot.mystiguardian.youtube.YouTubeNotificationSystem;
import java.time.Instant;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.Future;
import lombok.Getter;
import lombok.val;
import org.javacord.api.DiscordApi;
import org.javacord.api.entity.activity.ActivityType;
import net.dv8tion.jda.api.JDA;
import org.jooq.DSLContext;

public class MystiGuardianConfig {
@Getter private static final String version = System.getProperty("version");
@Getter private static MystiGuardianDatabase database;
@Getter private static DSLContext context;
@Getter private static final EventDispatcher eventDispatcher = new EventDispatcher();
@Getter private DiscordApi api;
@Getter private JDA jda;
public static Instant startTime = Instant.ofEpochSecond(0L);
public static Future<?> mainThread;
public static boolean reloading = false;
Expand Down Expand Up @@ -90,41 +89,36 @@ private void shutdown() {
public void run() {
startTime = Instant.now();

logger.info("Logged in as {}", api.getYourself().getDiscriminatedName());
logger.info("Logged in as {}", jda.getSelfUser().getAsMention());

if (reloading) {
notifyOwner();
reloading = false;
}

api.updateActivity(ActivityType.LISTENING, "to your commands");

eventDispatcher.registerEventHandler(
ModerationActionTriggerEvent.class, new ModerationActionTriggerEventListener());

api.addSlashCommandCreateListener(slashCommandsHandler::onSlashCommandCreateEvent);
api.addLostConnectionListener(DiscordEvents::onLostConnectionEvent);
api.addButtonClickListener(ButtonClickHandler::new);
jda.addEventListener(new DiscordEvents(slashCommandsHandler));

new YouTubeNotificationSystem(api);
new YouTubeNotificationSystem(jda);

MystiGuardianUtils.clearGithubAIModel();

val serpAPI = new SerpAPI();
serpAPI.scheduleSearchAndSendResponse(api);
serpAPI.scheduleSearchAndSendResponse(jda);
}

private void notifyOwner() {
api.getUserById(MystiGuardianUtils.getMainConfig().ownerId())
.thenAccept(
user ->
user.openPrivateChannel()
.thenAccept(channel -> channel.sendMessage("Reloaded successfully")));
Objects.requireNonNull(jda.getUserById(getMainConfig().ownerId()))
.openPrivateChannel()
.flatMap(channel -> channel.sendMessage("Reloaded successfully"))
.queue();
}

public void handleRegistrations() {
try {
this.slashCommandsHandler = new AutoSlashAdder(api);
this.slashCommandsHandler = new AutoSlashAdder(jda);
} catch (RuntimeException e) {
logger.error("Failed to load slash commands", e);
return;
Expand All @@ -138,7 +132,7 @@ public void handleRegistrations() {
return;
}

this.unbanCheckThread = new UnbanCheckThread(api);
this.unbanCheckThread = new UnbanCheckThread(jda);

if (unbanCheckThread.isRunning()) {
logger.info("Stopping unban check thread...");
Expand All @@ -149,7 +143,7 @@ public void handleRegistrations() {
unbanCheckThread.start();
}

public void setAPI(DiscordApi api) {
this.api = api;
public void setAPI(JDA jda) {
this.jda = jda;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import lombok.val;
import org.javacord.api.DiscordApi;
import org.javacord.api.entity.message.embed.EmbedBuilder;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.JDA;
import org.jetbrains.annotations.NotNull;

public class SerpAPI {
Expand Down Expand Up @@ -170,16 +171,16 @@ private boolean filteredOutLink(String link) {
return false;
}

public void scheduleSearchAndSendResponse(DiscordApi api) {
public void scheduleSearchAndSendResponse(JDA jda) {
MystiGuardianUtils.getScheduler()
.scheduleAtFixedRate(
() -> searchAndSendResponse(api),
() -> searchAndSendResponse(jda),
0, // initial delay
12, // period
TimeUnit.HOURS);
}

public void searchAndSendResponse(DiscordApi api) {
public void searchAndSendResponse(JDA jda) {
MystiGuardianUtils.runInVirtualThread(
() -> {
try {
Expand All @@ -198,10 +199,10 @@ public void searchAndSendResponse(DiscordApi api) {
}

EmbedBuilder embed = parseJsonToEmbed(result);
sendEmbedToChannel(api, embed);
sendEmbedToChannel(jda, embed);
} catch (IOException e) {
MystiGuardianUtils.logger.error("Failed to perform search", e);
handleSearchFailure(api, e);
handleSearchFailure(jda, e);
}
});
}
Expand All @@ -215,26 +216,31 @@ private EmbedBuilder parseJsonToEmbed(@NotNull JsonNode jsonNode) {
}
}

private void sendEmbedToChannel(@NotNull DiscordApi api, EmbedBuilder embed) {
api.getServerById(MystiGuardianUtils.getSerpAPIConfig().guildId())
.flatMap(
server -> server.getTextChannelById(MystiGuardianUtils.getSerpAPIConfig().channelId()))
.ifPresent(channel -> channel.sendMessage(embed));
private void sendEmbedToChannel(@NotNull JDA jda, EmbedBuilder embed) {
Objects.requireNonNull(
Objects.requireNonNull(
jda.getGuildById(MystiGuardianUtils.getSerpAPIConfig().guildId()))
.getTextChannelById(MystiGuardianUtils.getSerpAPIConfig().channelId()))
.sendMessageEmbeds(embed.build())
.queue();
}

private void handleSearchFailure(@NotNull DiscordApi api, Throwable ex) {
api.getServerById(MystiGuardianUtils.getSerpAPIConfig().guildId())
.flatMap(
server -> server.getTextChannelById(MystiGuardianUtils.getSerpAPIConfig().channelId()))
.ifPresent(
channel -> channel.sendMessage("Failed to perform search: " + ex.getMessage() + ex));
private void handleSearchFailure(@NotNull JDA jda, Throwable ex) {
Objects.requireNonNull(
Objects.requireNonNull(
jda.getGuildById(MystiGuardianUtils.getSerpAPIConfig().guildId()))
.getTextChannelById(MystiGuardianUtils.getSerpAPIConfig().channelId()))
.sendMessage("Failed to perform search: " + ex.getMessage() + ex)
.queue();
}

private void sendNothingFoundMessage(@NotNull DiscordApi api) {
api.getServerById(MystiGuardianUtils.getSerpAPIConfig().guildId())
.flatMap(
server -> server.getTextChannelById(MystiGuardianUtils.getSerpAPIConfig().channelId()))
.ifPresent(channel -> channel.sendMessage("No new relevant results found."));
private void sendNothingFoundMessage(@NotNull JDA jda) {
Objects.requireNonNull(
Objects.requireNonNull(
jda.getGuildById(MystiGuardianUtils.getSerpAPIConfig().guildId()))
.getTextChannelById(MystiGuardianUtils.getSerpAPIConfig().channelId()))
.sendMessage("No new relevant results found.")
.queue();
}

private void saveRemainingCredits() {
Expand Down
Loading

0 comments on commit ea7ba11

Please sign in to comment.