From 59b13bad06e485a263c3943a06a00272ac08a904 Mon Sep 17 00:00:00 2001 From: Joel Date: Sun, 21 Feb 2021 21:49:32 +0100 Subject: [PATCH] Added a role toggling system in main discord. --- .../zone/themcgamer/discordbot/MGZBot.java | 4 +- .../discordbot/command/impl/SayCommand.java | 4 +- .../discordbot/events/ReactionListener.java | 57 +++++++++++++++++++ 3 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 discordbot/src/main/java/zone/themcgamer/discordbot/events/ReactionListener.java diff --git a/discordbot/src/main/java/zone/themcgamer/discordbot/MGZBot.java b/discordbot/src/main/java/zone/themcgamer/discordbot/MGZBot.java index dd9be36..f11acb2 100644 --- a/discordbot/src/main/java/zone/themcgamer/discordbot/MGZBot.java +++ b/discordbot/src/main/java/zone/themcgamer/discordbot/MGZBot.java @@ -9,6 +9,7 @@ import net.dv8tion.jda.api.entities.Activity; import net.dv8tion.jda.api.requests.GatewayIntent; import zone.themcgamer.discordbot.command.impl.*; import zone.themcgamer.discordbot.events.GuildMemberJoinQuitListener; +import zone.themcgamer.discordbot.events.ReactionListener; import javax.security.auth.login.LoginException; import java.util.concurrent.Executors; @@ -46,7 +47,8 @@ public class MGZBot { .enableIntents(GatewayIntent.GUILD_MEMBERS, GatewayIntent.GUILD_EMOJIS) .addEventListeners( commandClientBuilder.build(), - new GuildMemberJoinQuitListener(this)) + new GuildMemberJoinQuitListener(this), + new ReactionListener()) .build(); jda.awaitReady(); } catch (LoginException | InterruptedException ex) { diff --git a/discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/SayCommand.java b/discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/SayCommand.java index 7732125..9283da9 100644 --- a/discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/SayCommand.java +++ b/discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/SayCommand.java @@ -10,7 +10,6 @@ import zone.themcgamer.discordbot.utilities.MessageUtils; import java.util.Arrays; import java.util.List; -import java.util.stream.Collectors; public class SayCommand extends BaseCommand { @@ -32,10 +31,9 @@ public class SayCommand extends BaseCommand { } //TODO a way to add images, and such to the embeds. - String description = args.stream().skip(2).collect(Collectors.joining(" ")); EmbedBuilder embedBuilder = EmbedUtils.defaultEmbed(); embedBuilder.setTitle(args.get(1).replace("_", " ")); - embedBuilder.setDescription(description); + embedBuilder.setDescription(event.getMessage().getContentRaw().replace(args.get(1), "").replace("." + args.get(0), "")); event.getChannel().sendMessage(embedBuilder.build()).queue(); } } diff --git a/discordbot/src/main/java/zone/themcgamer/discordbot/events/ReactionListener.java b/discordbot/src/main/java/zone/themcgamer/discordbot/events/ReactionListener.java new file mode 100644 index 0000000..0a081d7 --- /dev/null +++ b/discordbot/src/main/java/zone/themcgamer/discordbot/events/ReactionListener.java @@ -0,0 +1,57 @@ +package zone.themcgamer.discordbot.events; + +import net.dv8tion.jda.api.entities.Guild; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.MessageReaction; +import net.dv8tion.jda.api.entities.Role; +import net.dv8tion.jda.api.events.message.guild.react.GuildMessageReactionAddEvent; +import net.dv8tion.jda.api.hooks.ListenerAdapter; +import zone.themcgamer.discordbot.BotConstants; +import zone.themcgamer.discordbot.utilities.EmbedUtils; + +import javax.annotation.Nonnull; + +public class ReactionListener extends ListenerAdapter { + + @Override + public void onGuildMessageReactionAdd(@Nonnull GuildMessageReactionAddEvent event) { + Guild guild = event.getGuild(); + Member member = event.getMember(); + + if (!guild.getId().equals(BotConstants.MAIN_GUILD_ID)) + return; + + if (event.getChannel().getId().equals("813139125195898880") && event.getMessageId().equals("813143359249842186")) { + MessageReaction.ReactionEmote reactionEmote = event.getReactionEmote(); + if (reactionEmote.getName().equals("🗞️")) { + Role role = guild.getRoleById(812440883898875914L); // News role + if (role == null) + return; + toggleRole(guild, member, role); + } else if (reactionEmote.getName().equals("📊")) { + Role role = guild.getRoleById(813139198428839976L); // Polls role + if (role == null) + return; + toggleRole(guild, member, role); + } else if (reactionEmote.getName().equals("🥁")) { + Role role = guild.getRoleById(813140631705878549L); // Events role + if (role == null) + return; + toggleRole(guild, member, role); + } + event.getReaction().removeReaction(member.getUser()).queue(); + } + } + + protected void toggleRole(Guild guild, Member member, Role role) { + if (member.getRoles().contains(role)) + guild.removeRoleFromMember(member.getIdLong(), role).queue(); + else guild.addRoleToMember(member.getIdLong(), role).queue(); + + member.getUser().openPrivateChannel().queue(privateChannel -> { + privateChannel.sendMessage(EmbedUtils.successEmbed().setDescription("Succesfully toggled " + role.getName() + " " + (!member.getRoles().contains(role) ? "On" : "Off")).build()).queue(); + }, error -> { + + }); + } +} \ No newline at end of file