Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate the Discord bot to JDA 5 #127

Merged
merged 1 commit into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<repository>
<id>chew</id>
<name>m2-chew</name>
<url>https://m2.chew.pro/releases</url>
<url>https://m2.chew.pro/snapshots</url>
</repository>
</repositories>

Expand Down Expand Up @@ -68,13 +68,13 @@
<dependency>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId>
<version>4.4.1_353</version>
<version>5.0.0-beta.24</version>
</dependency>

<dependency>
<groupId>pw.chew</groupId>
<artifactId>jda-chewtils</artifactId>
<version>1.24.1</version>
<version>2.0-SNAPSHOT</version>
<scope>compile</scope>
<type>pom</type>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
import java.util.List;
import java.util.Objects;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.ChannelType;
import net.dv8tion.jda.api.entities.GuildChannel;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.entities.channel.ChannelType;
import net.dv8tion.jda.api.entities.channel.middleman.GuildChannel;
import com.jagrosh.jdautilities.command.SlashCommandEvent;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -72,7 +72,7 @@ public void execute(SlashCommandEvent event) {
}

GuildChannel channelArgument = ThrowableOptional
.of(() -> Objects.requireNonNull(event.getOption(CHANNEL_ARGUMENT_NAME)).getAsGuildChannel())
.of(() -> Objects.requireNonNull(event.getOption(CHANNEL_ARGUMENT_NAME)).getAsChannel())
.orElse(null);

if (channelArgument != null) {
Expand All @@ -84,7 +84,7 @@ public void execute(SlashCommandEvent event) {
return;
}

if (!event.getGuild().getSelfMember().hasPermission(channelArgument, Permission.VIEW_CHANNEL, Permission.MESSAGE_WRITE)) {
if (!event.getGuild().getSelfMember().hasPermission(channelArgument, Permission.VIEW_CHANNEL, Permission.MESSAGE_SEND)) {
event
.getHook()
.editOriginalFormat("%s It seems that the bot dont have talk access to this channel", ERROR_EMOJI)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
import java.util.List;
import java.util.Objects;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.ChannelType;
import net.dv8tion.jda.api.entities.GuildChannel;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import com.jagrosh.jdautilities.command.SlashCommandEvent;
import net.dv8tion.jda.api.entities.channel.ChannelType;
import net.dv8tion.jda.api.entities.channel.middleman.GuildChannel;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -71,8 +71,7 @@ public void execute(SlashCommandEvent event) {
}

GuildChannel channelArgument = ThrowableOptional
.of(() -> Objects.requireNonNull(event.getOption(CHANNEL_ARGUMENT_NAME))
.getAsGuildChannel())
.of(() -> Objects.requireNonNull(event.getOption(CHANNEL_ARGUMENT_NAME)).getAsChannel())
.orElse(null);

if (channelArgument != null) {
Expand All @@ -84,7 +83,7 @@ public void execute(SlashCommandEvent event) {
return;
}

if (!event.getGuild().getSelfMember().hasPermission(channelArgument, Permission.VIEW_CHANNEL, Permission.MESSAGE_WRITE)) {
if (!event.getGuild().getSelfMember().hasPermission(channelArgument, Permission.VIEW_CHANNEL, Permission.MESSAGE_SEND)) {
event
.getHook()
.editOriginalFormat("%s It seems that the bot dont have talk access to this channel", ERROR_EMOJI)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import com.jagrosh.jdautilities.command.SlashCommandEvent;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import org.springframework.transaction.annotation.Transactional;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import com.jagrosh.jdautilities.command.SlashCommandEvent;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -125,7 +125,7 @@ public void execute(SlashCommandEvent event) {
.addField(":x: Preventing rollbacks for role",
roleArgument.getName() + " (" + roleArgument.getId() + ")", true);

logChannel.get().sendMessage(embedBuilder.build()).queue();
logChannel.get().sendMessageEmbeds(embedBuilder.build()).queue();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import com.jagrosh.jdautilities.command.SlashCommandEvent;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -132,7 +132,7 @@ public void execute(SlashCommandEvent event) {
.addField(":unlock: Role reapplied at future member join",
roleArgument.getName() + " (" + roleArgument.getId() + ")", true);

logChannel.get().sendMessage(embedBuilder.build()).queue();
logChannel.get().sendMessageEmbeds(embedBuilder.build()).queue();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import com.jagrosh.jdautilities.command.SlashCommandEvent;
import org.springframework.transaction.annotation.Transactional;

public class GetRolesCommand extends SlashCommand {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
package com.brandonfl.discordrolepersistence.discordbot.command.slash;

import com.jagrosh.jdautilities.command.SlashCommand;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import com.jagrosh.jdautilities.command.SlashCommandEvent;

public class PingCommand extends SlashCommand {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@
import com.brandonfl.discordrolepersistence.config.BotProperties;
import com.brandonfl.discordrolepersistence.db.repository.RepositoryContainer;
import com.brandonfl.discordrolepersistence.utils.DiscordBotUtils;
import javax.annotation.Nonnull;
import jakarta.annotation.Nonnull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.events.ReadyEvent;
import net.dv8tion.jda.api.events.ReconnectedEvent;
import net.dv8tion.jda.api.events.session.ReadyEvent;
import net.dv8tion.jda.api.events.session.SessionRecreateEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.jetbrains.annotations.NotNull;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StopWatch;

Expand All @@ -46,6 +47,8 @@ public class BotEvent extends ListenerAdapter {
private final BotProperties botProperties;
private final RepositoryContainer repositoryContainer;



@Override
@Transactional
public void onReady(@Nonnull ReadyEvent event) {
Expand All @@ -68,7 +71,7 @@ public void onReady(@Nonnull ReadyEvent event) {

@Override
@Transactional
public void onReconnect(@Nonnull ReconnectedEvent event) {
public void onSessionRecreate(@NotNull SessionRecreateEvent event) {
log.warn("Bot reconnected !");

if (botProperties.getSetting().getPersistence().needToReloadPersistenceAtBotReload()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
package com.brandonfl.discordrolepersistence.discordbot.event;

import com.brandonfl.discordrolepersistence.service.UserService;
import javax.annotation.Nonnull;
import jakarta.annotation.Nonnull;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import com.brandonfl.discordrolepersistence.config.BotProperties;
import com.brandonfl.discordrolepersistence.service.LoggerService;
import com.brandonfl.discordrolepersistence.service.UserService;
import javax.annotation.Nonnull;
import jakarta.annotation.Nonnull;
import lombok.RequiredArgsConstructor;
import net.dv8tion.jda.api.events.guild.member.GuildMemberRoleAddEvent;
import net.dv8tion.jda.api.events.guild.member.GuildMemberRoleRemoveEvent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

import com.brandonfl.discordrolepersistence.db.entity.ServerEntity;
import com.brandonfl.discordrolepersistence.db.repository.RepositoryContainer;
import javax.annotation.Nonnull;
import jakarta.annotation.Nonnull;
import lombok.RequiredArgsConstructor;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Role;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import com.brandonfl.discordrolepersistence.db.repository.RepositoryContainer;
import com.brandonfl.discordrolepersistence.service.LoggerService;
import jakarta.transaction.Transactional;
import javax.annotation.Nonnull;
import jakarta.annotation.Nonnull;
import lombok.RequiredArgsConstructor;
import net.dv8tion.jda.api.events.role.GenericRoleEvent;
import net.dv8tion.jda.api.events.role.RoleCreateEvent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import jakarta.annotation.Nonnull;
import lombok.RequiredArgsConstructor;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.events.guild.GenericGuildEvent;
import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent;
import net.dv8tion.jda.api.events.role.GenericRoleEvent;
Expand Down Expand Up @@ -66,7 +66,7 @@ public void logRolesGivedBack(GuildMemberJoinEvent joinEvent, Long serverGuid, S
.setAuthor("Role backup for " + joinEvent.getMember().getEffectiveName(), null, joinEvent.getMember().getUser().getEffectiveAvatarUrl())
.appendDescription("User id : " + joinEvent.getMember().getUser().getId() + "\n\n" + rolesGivedBackStringBuilder.toString());

logChannel.get().sendMessage(logEmbedBuilder.build()).queue();
logChannel.get().sendMessageEmbeds(logEmbedBuilder.build()).queue();
}
}

Expand All @@ -79,7 +79,7 @@ public void logRolesGivedBack(GuildMemberJoinEvent joinEvent, Long serverGuid, S
.addField("Here are your old roles that have been given back to you", rolesGivedBackStringBuilder.toString(), true);

welcomeBackChannel.get().sendMessage(joinEvent.getMember().getAsMention()).queue();
welcomeBackChannel.get().sendMessage(welcomeBackEmbedBuilder.build()).queue();
welcomeBackChannel.get().sendMessageEmbeds(welcomeBackEmbedBuilder.build()).queue();
}
}
}
Expand Down Expand Up @@ -109,7 +109,7 @@ public void logUserRoleUpdate(
.addField(fieldName, roles.stream().map(
Role::getName).collect(Collectors.joining("\n")), true);

textChannel.get().sendMessage(embedBuilder.build()).queue();
textChannel.get().sendMessageEmbeds(embedBuilder.build()).queue();
}
}

Expand All @@ -125,7 +125,7 @@ public void logServerRole(
roleEvent.getRole().getName() + " (" + roleEvent.getRole().getId() + ")",
true);

textChannel.get().sendMessage(embedBuilder.build()).queue();
textChannel.get().sendMessageEmbeds(embedBuilder.build()).queue();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.annotation.Nonnull;
import jakarta.annotation.Nonnull;
import lombok.RequiredArgsConstructor;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Guild;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.exceptions.PermissionException;
import net.dv8tion.jda.api.managers.Presence;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down
Loading