Skip to content
8 changes: 8 additions & 0 deletions languages/en-GB.yml
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,13 @@ message:
stopReason: "Recording stopped."
stopped: "Your Audio has been converted and is now available for download!"
error: "Something went wrong while converting your audio!\nReason: %s"
reactions:
roleAssign: "You have assigned the Role %s to the Reaction!"
reactionNeeded: "You need to react to the message with the reaction you want to assign %s to!"
roleNotFound: "The Role you want to assign to the reaction was not found!"
removed: "Successfully removed the Reaction-Role for the Role %s!"
roleAdded: "You have received the Role %s!"
roleRemoved: "You have lost the Role %s!"
game:
description:
blackjack: "Play Blackjack with your friends!"
Expand Down Expand Up @@ -515,6 +522,7 @@ command:
nsfw: "Shows a random NSFW Image."
rule34: "Shows a random Rule34 Image."
rule34_slash: "Shows not safe for work Images from a specific Website."
reactions: "Assign roles to reactions on a message!"
category:
info: "Used to gather information or provide information."
moderation: "Moderation Tools that can help you manager Users or prevent rule breaking on the Server."
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/de/presti/ree6/commands/CommandEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,32 @@ public String getResource(String key, Object... parameters) {
*
* @return the Arguments.
*/

public String[] getArguments() {
return getArguments(false);
}

/**
* Get the Arguments associated with the Event.
*
* @param parseFromSlash if the Arguments should be parsed from the SlashCommandInteractionEvent.
*
* @return the Arguments.
*/
public String[] getArguments(boolean parseFromSlash) {
if (arguments == null) {
arguments = new String[0];
}

if (isSlashCommand() && parseFromSlash) {
getSlashCommandInteractionEvent().getOptions().forEach(option -> {
String[] newArguments = new String[arguments.length + 1];
System.arraycopy(arguments, 0, newArguments, 0, arguments.length);
newArguments[newArguments.length - 1] = option.getAsString();
arguments = newArguments;
});
}

return arguments;
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/de/presti/ree6/commands/CommandManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,17 @@ public void addSlashCommand(JDA jda) {
for (DiscordLocale discordLocale : DiscordLocale.values()) {
if (!LanguageService.languageResources.containsKey(discordLocale)) continue;

String description = LanguageService.getByLocale(discordLocale, command.getClass().getAnnotation(Command.class).description());
String description = LanguageService.getByLocale(discordLocale, commandAnnotation.description());
if (description.equals("Missing language resource!")) {
description = LanguageService.getDefault(command.getClass().getAnnotation(Command.class).description());
description = LanguageService.getDefault(commandAnnotation.description());
}

if (!description.equals("Missing language resource!")) {
commandData1.setDescriptionLocalization(discordLocale, description);
}
}

String description = LanguageService.getDefault(command.getClass().getAnnotation(Command.class).description());
String description = LanguageService.getDefault(commandAnnotation.description());

if (!description.equals("Missing language resource!")) {
commandData1.setDescription(description);
Expand Down
101 changes: 101 additions & 0 deletions src/main/java/de/presti/ree6/commands/impl/community/Reactions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package de.presti.ree6.commands.impl.community;

import de.presti.ree6.commands.Category;
import de.presti.ree6.commands.CommandEvent;
import de.presti.ree6.commands.interfaces.Command;
import de.presti.ree6.commands.interfaces.ICommand;
import de.presti.ree6.main.Main;
import de.presti.ree6.sql.entities.ReactionRole;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder;
import net.dv8tion.jda.internal.interactions.CommandDataImpl;

import java.util.Map;

/**
* A Command to add a Reaction Role.
*/
@Command(name = "reactions", description = "command.description.reactions", category = Category.COMMUNITY)
public class Reactions implements ICommand {

/**
* @inheritDoc
*/
@Override
public void onPerform(CommandEvent commandEvent) {
if (!commandEvent.getMember().hasPermission(Permission.ADMINISTRATOR)) {
commandEvent.reply(commandEvent.getResource("message.default.insufficientPermission", Permission.ADMINISTRATOR), 5);
return;
}

if (commandEvent.isSlashCommand()) {
OptionMapping action = commandEvent.getSlashCommandInteractionEvent().getOption("action");
OptionMapping message = commandEvent.getSlashCommandInteractionEvent().getOption("message");
OptionMapping role = commandEvent.getSlashCommandInteractionEvent().getOption("role");

switch (action.getAsString()) {
case "add" -> {
if (message == null || role == null) {
commandEvent.reply(commandEvent.getResource("message.default.invalidOption"), 5);
return;
}

commandEvent.getChannel().retrieveMessageById(message.getAsString()).onErrorMap(x -> {
commandEvent.reply(commandEvent.getResource("message.default.invalidOption"), 5);
return null;
}).queue(msg -> {
if (msg == null) return;
MessageCreateBuilder messageCreateBuilder = new MessageCreateBuilder();
messageCreateBuilder.setContent(commandEvent.getResource("message.reactions.reactionNeeded", role.getAsRole().getAsMention()));
msg.reply(messageCreateBuilder.build()).queue();
});

commandEvent.reply(commandEvent.getResource("message.default.checkBelow"));
}

case "remove" -> {
if (message == null || role == null) {
commandEvent.reply(commandEvent.getResource("message.default.invalidOption"), 5);
return;
}

ReactionRole reactionRole = Main.getInstance().getSqlConnector().getSqlWorker().getEntity(new ReactionRole(),
"SELECT * FROM ReactionRole WHERE gid=:gid AND roleId=:roleId AND messageId=:messageId",
Map.of("gid", commandEvent.getGuild().getIdLong(), "roleId", role.getAsRole().getIdLong(), "messageId", Long.parseLong(message.getAsString())));

if (reactionRole != null) {
Main.getInstance().getSqlConnector().getSqlWorker().deleteEntity(reactionRole);

commandEvent.reply(commandEvent.getResource("message.reactions.removed", role.getAsRole().getIdLong()), 5);
}
}

default -> commandEvent.reply(commandEvent.getResource("message.default.invalidOption"), 5);
}
} else {
commandEvent.reply(commandEvent.getResource("command.perform.onlySlashSupported"));
}
}

/**
* @inheritDoc
*/
@Override
public CommandData getCommandData() {
return new CommandDataImpl("reactions", "command.description.reactions")
.addOption(OptionType.STRING, "action", "The current action that should be performed.", true)
.addOption(OptionType.STRING, "message", "The ID of the Message.", true)
.addOption(OptionType.ROLE, "role", "The Role to be given.", true);
}

/**
* @inheritDoc
*/
@Override
public String[] getAlias() {
return new String[0];
}
}
111 changes: 104 additions & 7 deletions src/main/java/de/presti/ree6/events/OtherEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import de.presti.ree6.bot.version.BotState;
import de.presti.ree6.language.LanguageService;
import de.presti.ree6.main.Main;
import de.presti.ree6.sql.entities.ReactionRole;
import de.presti.ree6.sql.entities.TemporalVoicechannel;
import de.presti.ree6.sql.entities.level.ChatUserLevel;
import de.presti.ree6.sql.entities.level.VoiceUserLevel;
Expand All @@ -14,10 +15,14 @@
import de.presti.ree6.utils.data.ImageCreationUtility;
import de.presti.ree6.utils.others.*;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.entities.channel.ChannelType;
import net.dv8tion.jda.api.entities.channel.concrete.VoiceChannel;
import net.dv8tion.jda.api.entities.channel.middleman.GuildChannel;
import net.dv8tion.jda.api.entities.emoji.Emoji;
import net.dv8tion.jda.api.events.guild.GuildJoinEvent;
import net.dv8tion.jda.api.events.guild.GuildLeaveEvent;
import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent;
Expand All @@ -26,6 +31,8 @@
import net.dv8tion.jda.api.events.guild.voice.GuildVoiceUpdateEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent;
import net.dv8tion.jda.api.events.message.react.MessageReactionRemoveEvent;
import net.dv8tion.jda.api.events.session.ReadyEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.jetbrains.annotations.NotNull;
Expand All @@ -35,6 +42,7 @@
import java.time.Duration;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
* Event Handler for no categorized Events.
Expand Down Expand Up @@ -82,22 +90,22 @@ public void onGuildMemberJoin(@Nonnull GuildMemberJoinEvent event) {
if (channelStats.getMemberStatsChannelId() != null) {
GuildChannel guildChannel = event.getGuild().getGuildChannelById(channelStats.getMemberStatsChannelId());
if (guildChannel != null) {
guildChannel.getManager().setName(LanguageService.getByGuild(event.getGuild(),"label.overallMembers") + ": " + event.getGuild().getMemberCount()).queue();
guildChannel.getManager().setName(LanguageService.getByGuild(event.getGuild(), "label.overallMembers") + ": " + event.getGuild().getMemberCount()).queue();
}
}

event.getGuild().loadMembers().onSuccess(members -> {
if (channelStats.getRealMemberStatsChannelId() != null) {
GuildChannel guildChannel = event.getGuild().getGuildChannelById(channelStats.getRealMemberStatsChannelId());
if (guildChannel != null) {
guildChannel.getManager().setName(LanguageService.getByGuild(event.getGuild(),"label.realMembers") + ": " + members.stream().filter(member -> !member.getUser().isBot()).count()).queue();
guildChannel.getManager().setName(LanguageService.getByGuild(event.getGuild(), "label.realMembers") + ": " + members.stream().filter(member -> !member.getUser().isBot()).count()).queue();
}
}

if (channelStats.getBotMemberStatsChannelId() != null) {
GuildChannel guildChannel = event.getGuild().getGuildChannelById(channelStats.getBotMemberStatsChannelId());
if (guildChannel != null) {
guildChannel.getManager().setName(LanguageService.getByGuild(event.getGuild(),"label.botMembers") + ": "+ members.stream().filter(member -> member.getUser().isBot()).count()).queue();
guildChannel.getManager().setName(LanguageService.getByGuild(event.getGuild(), "label.botMembers") + ": " + members.stream().filter(member -> member.getUser().isBot()).count()).queue();
}
}
});
Expand Down Expand Up @@ -145,22 +153,22 @@ public void onGuildMemberRemove(@NotNull GuildMemberRemoveEvent event) {
if (channelStats.getMemberStatsChannelId() != null) {
GuildChannel guildChannel = event.getGuild().getGuildChannelById(channelStats.getMemberStatsChannelId());
if (guildChannel != null) {
guildChannel.getManager().setName(LanguageService.getByGuild(event.getGuild(),"label.overallMembers") + ": " + event.getGuild().getMemberCount()).queue();
guildChannel.getManager().setName(LanguageService.getByGuild(event.getGuild(), "label.overallMembers") + ": " + event.getGuild().getMemberCount()).queue();
}
}

event.getGuild().loadMembers().onSuccess(members -> {
if (channelStats.getRealMemberStatsChannelId() != null) {
GuildChannel guildChannel = event.getGuild().getGuildChannelById(channelStats.getRealMemberStatsChannelId());
if (guildChannel != null) {
guildChannel.getManager().setName(LanguageService.getByGuild(event.getGuild(),"label.realMembers") + ": " + members.stream().filter(member -> !member.getUser().isBot()).count()).queue();
guildChannel.getManager().setName(LanguageService.getByGuild(event.getGuild(), "label.realMembers") + ": " + members.stream().filter(member -> !member.getUser().isBot()).count()).queue();
}
}

if (channelStats.getBotMemberStatsChannelId() != null) {
GuildChannel guildChannel = event.getGuild().getGuildChannelById(channelStats.getBotMemberStatsChannelId());
if (guildChannel != null) {
guildChannel.getManager().setName(LanguageService.getByGuild(event.getGuild(),"label.botMembers") + ": " + members.stream().filter(member -> member.getUser().isBot()).count()).queue();
guildChannel.getManager().setName(LanguageService.getByGuild(event.getGuild(), "label.botMembers") + ": " + members.stream().filter(member -> member.getUser().isBot()).count()).queue();
}
}
});
Expand Down Expand Up @@ -321,7 +329,7 @@ public void onMessageReceived(@NotNull MessageReceivedEvent event) {

if (userLevel.addExperience(RandomUtils.random.nextInt(15, 26)) && Main.getInstance().getSqlConnector().getSqlWorker().getSetting(event.getGuild().getId(), "level_message").getBooleanValue()) {
Main.getInstance().getCommandManager().sendMessage(LanguageService.getByGuild(event.getGuild(),
"message.levelUp", userLevel.getLevel(), LanguageService.getByGuild(event.getGuild(),"label.chat")
"message.levelUp", userLevel.getLevel(), LanguageService.getByGuild(event.getGuild(), "label.chat")
, event.getMember().getAsMention()), event.getChannel());
}

Expand All @@ -337,6 +345,95 @@ public void onMessageReceived(@NotNull MessageReceivedEvent event) {
}
}

/**
* @inheritDoc
*/
@Override
public void onMessageReactionAdd(@NotNull MessageReactionAddEvent event) {
if (event.getMember() == null) return;

event.retrieveMessage().queue(message -> {

String reactionCode = event.getReaction().getEmoji().getAsReactionCode();

long emojiId;
if (event.getReaction().getEmoji().getType() == Emoji.Type.CUSTOM) {
emojiId = Long.parseLong(reactionCode.split(":")[1]);
} else {
emojiId = reactionCode.replace(":", "").hashCode();
}

if (message.getAuthor().getId().equalsIgnoreCase(event.getJDA().getSelfUser().getId())) {
String messageContent = message.getContentRaw();

if (messageContent.startsWith(LanguageService.getByGuild(event.getGuild(), "message.reactions.reactionNeeded", "SPLIT_HERE").split("SPLIT_HERE")[0])) {
if (event.getMember().hasPermission(Permission.ADMINISTRATOR) && message.getMessageReference() != null) {
if (message.getMentions().getRoles().isEmpty()) {
message.editMessage(LanguageService.getByGuild(event.getGuild(), "message.reactions.roleNotFound")).queue();
return;
}

Role role = message.getMentions().getRoles().get(0);

if (role == null) {
message.editMessage(LanguageService.getByGuild(event.getGuild(), "message.reactions.roleNotFound")).queue();
return;
}

ReactionRole reactionRole = new ReactionRole(event.getGuild().getIdLong(), emojiId, role.getIdLong(), message.getMessageReference().getMessageIdLong());
Main.getInstance().getSqlConnector().getSqlWorker().updateEntity(reactionRole);

if (message.getMessageReference().getMessage() != null) {
message.getMessageReference().getMessage().addReaction(event.getEmoji()).queue();
}

message.editMessage(LanguageService.getByGuild(event.getGuild(), "message.reactions.roleAssign", role.getAsMention()))
.delay(5, TimeUnit.SECONDS).flatMap(Message::delete).queue();
}
}
} else {
ReactionRole reactionRole = Main.getInstance().getSqlConnector().getSqlWorker().getEntity(new ReactionRole(), "SELECT * FROM ReactionRole WHERE gid=:gid AND emoteId=:emoteId AND messageId=:messageId", Map.of("gid", event.getGuild().getIdLong(), "emoteId", emojiId, "messageId", message.getIdLong()));

if (reactionRole != null) {
Role role = event.getGuild().getRoleById(reactionRole.getRoleId());

if (role != null) {
event.getGuild().addRoleToMember(event.getMember(), role).queue();
}
}
}
});
}

/**
* @inheritDoc
*/
@Override
public void onMessageReactionRemove(@NotNull MessageReactionRemoveEvent event) {
if (event.getMember() == null) return;

String reactionCode = event.getReaction().getEmoji().getAsReactionCode();

long emojiId;
if (event.getReaction().getEmoji().getType() == Emoji.Type.CUSTOM) {
emojiId = Long.parseLong(reactionCode.split(":")[1]);
} else {
emojiId = reactionCode.replace(":", "").hashCode();
}

ReactionRole reactionRole = Main.getInstance().getSqlConnector().getSqlWorker().getEntity(new ReactionRole(),
"SELECT * FROM ReactionRole WHERE gid=:gid AND emoteId=:emoteId AND messageId=:messageId",
Map.of("gid", event.getGuild().getIdLong(), "emoteId", emojiId, "messageId", event.getMessageIdLong()));

if (reactionRole != null) {
Role role = event.getGuild().getRoleById(reactionRole.getRoleId());

if (role != null) {
event.getGuild().removeRoleFromMember(event.getMember(), role).queue();
}
}
}

/**
* @inheritDoc
*/
Expand Down
Loading