Added a role toggling system in main discord.

This commit is contained in:
Joel 2021-02-21 21:49:32 +01:00
parent a922104f8f
commit 59b13bad06
3 changed files with 61 additions and 4 deletions

@ -9,6 +9,7 @@ import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.requests.GatewayIntent; import net.dv8tion.jda.api.requests.GatewayIntent;
import zone.themcgamer.discordbot.command.impl.*; import zone.themcgamer.discordbot.command.impl.*;
import zone.themcgamer.discordbot.events.GuildMemberJoinQuitListener; import zone.themcgamer.discordbot.events.GuildMemberJoinQuitListener;
import zone.themcgamer.discordbot.events.ReactionListener;
import javax.security.auth.login.LoginException; import javax.security.auth.login.LoginException;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
@ -46,7 +47,8 @@ public class MGZBot {
.enableIntents(GatewayIntent.GUILD_MEMBERS, GatewayIntent.GUILD_EMOJIS) .enableIntents(GatewayIntent.GUILD_MEMBERS, GatewayIntent.GUILD_EMOJIS)
.addEventListeners( .addEventListeners(
commandClientBuilder.build(), commandClientBuilder.build(),
new GuildMemberJoinQuitListener(this)) new GuildMemberJoinQuitListener(this),
new ReactionListener())
.build(); .build();
jda.awaitReady(); jda.awaitReady();
} catch (LoginException | InterruptedException ex) { } catch (LoginException | InterruptedException ex) {

@ -10,7 +10,6 @@ import zone.themcgamer.discordbot.utilities.MessageUtils;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
public class SayCommand extends BaseCommand { 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. //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 embedBuilder = EmbedUtils.defaultEmbed();
embedBuilder.setTitle(args.get(1).replace("_", " ")); 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(); event.getChannel().sendMessage(embedBuilder.build()).queue();
} }
} }

@ -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 -> {
});
}
}