From bb812615b9cf519a70333e79102ad05ae7dfc360 Mon Sep 17 00:00:00 2001 From: Joel Date: Sun, 21 Feb 2021 22:40:16 +0100 Subject: [PATCH] Updates in bot --- .../zone/themcgamer/discordbot/MGZBot.java | 9 +- .../impl/AddReactionToMessageCommand.java | 50 ++++++++++ .../events/GuildMemberJoinQuitListener.java | 95 ------------------ .../discordbot/events/GuildsListener.java | 56 +++++++++++ .../discordbot/events/MainGuildListener.java | 98 +++++++++++++++++++ .../discordbot/events/ReactionListener.java | 47 --------- .../discordbot/utilities/GuildUtils.java | 2 + 7 files changed, 211 insertions(+), 146 deletions(-) create mode 100644 discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/AddReactionToMessageCommand.java delete mode 100644 discordbot/src/main/java/zone/themcgamer/discordbot/events/GuildMemberJoinQuitListener.java create mode 100644 discordbot/src/main/java/zone/themcgamer/discordbot/events/GuildsListener.java create mode 100644 discordbot/src/main/java/zone/themcgamer/discordbot/events/MainGuildListener.java delete 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 acbef76..06ed144 100644 --- a/discordbot/src/main/java/zone/themcgamer/discordbot/MGZBot.java +++ b/discordbot/src/main/java/zone/themcgamer/discordbot/MGZBot.java @@ -8,8 +8,8 @@ import net.dv8tion.jda.api.OnlineStatus; 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 zone.themcgamer.discordbot.events.GuildsListener; +import zone.themcgamer.discordbot.events.MainGuildListener; import javax.security.auth.login.LoginException; import java.util.concurrent.Executors; @@ -39,6 +39,7 @@ public class MGZBot { commandClientBuilder.addCommand(new InviteCommand()); commandClientBuilder.addCommand(new MessageCommand()); commandClientBuilder.addCommand(new EditMessageCommand()); + commandClientBuilder.addCommand(new AddReactionToMessageCommand()); try { jda = JDABuilder.createDefault(BotConstants.TOKEN) @@ -48,8 +49,8 @@ public class MGZBot { .enableIntents(GatewayIntent.GUILD_MEMBERS, GatewayIntent.GUILD_EMOJIS) .addEventListeners( commandClientBuilder.build(), - new GuildMemberJoinQuitListener(this), - new ReactionListener()) + new GuildsListener(this), + new MainGuildListener(this)) .build(); jda.awaitReady(); } catch (LoginException | InterruptedException ex) { diff --git a/discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/AddReactionToMessageCommand.java b/discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/AddReactionToMessageCommand.java new file mode 100644 index 0000000..ffd5b52 --- /dev/null +++ b/discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/AddReactionToMessageCommand.java @@ -0,0 +1,50 @@ +package zone.themcgamer.discordbot.command.impl; + +import com.jagrosh.jdautilities.command.CommandEvent; +import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.Permission; +import net.dv8tion.jda.api.entities.Message; +import net.dv8tion.jda.api.entities.TextChannel; +import zone.themcgamer.discordbot.MGZBot; +import zone.themcgamer.discordbot.command.BaseCommand; +import zone.themcgamer.discordbot.guild.Guild; +import zone.themcgamer.discordbot.utilities.EmbedUtils; +import zone.themcgamer.discordbot.utilities.MessageUtils; + +import java.util.Arrays; +import java.util.List; + +public class AddReactionToMessageCommand extends BaseCommand { + + public AddReactionToMessageCommand() { + name = "addreaction"; + aliases = new String[]{"react"}; + help = "Edit a message from the bot."; + arguments = " "; + userPermissions = new Permission[] { Permission.ADMINISTRATOR }; + guildOnly = true; + guilds = Arrays.asList(Guild.MAIN, Guild.TEAM, Guild.TEST); + } + + @Override + protected void execute(CommandEvent event, List args) { + if (args.size() < 3) { + MessageUtils.sendUsageMessage(event.getTextChannel(),this); + return; + } + + TextChannel textChannelById = MGZBot.getInstance().getJda().getTextChannelById(args.get(1)); + if (textChannelById == null) { + event.reply("Channel does not exist!"); + return; + } + + textChannelById.addReactionById(args.get(2), args.get(3)).queue(message -> { + + }, error -> { + EmbedBuilder embedBuilder = EmbedUtils.errorEmbed(); + embedBuilder.setDescription(error.getLocalizedMessage()); + event.reply(embedBuilder.build()); + }); + } +} diff --git a/discordbot/src/main/java/zone/themcgamer/discordbot/events/GuildMemberJoinQuitListener.java b/discordbot/src/main/java/zone/themcgamer/discordbot/events/GuildMemberJoinQuitListener.java deleted file mode 100644 index cdf8bd8..0000000 --- a/discordbot/src/main/java/zone/themcgamer/discordbot/events/GuildMemberJoinQuitListener.java +++ /dev/null @@ -1,95 +0,0 @@ -package zone.themcgamer.discordbot.events; - -import lombok.RequiredArgsConstructor; -import net.dv8tion.jda.api.EmbedBuilder; -import net.dv8tion.jda.api.entities.Guild; -import net.dv8tion.jda.api.entities.Role; -import net.dv8tion.jda.api.entities.TextChannel; -import net.dv8tion.jda.api.entities.User; -import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent; -import net.dv8tion.jda.api.events.guild.member.GuildMemberRemoveEvent; -import net.dv8tion.jda.api.hooks.ListenerAdapter; -import zone.themcgamer.discordbot.BotConstants; -import zone.themcgamer.discordbot.MGZBot; -import zone.themcgamer.discordbot.utilities.EmbedUtils; - -import javax.annotation.Nonnull; - -@RequiredArgsConstructor -public class GuildMemberJoinQuitListener extends ListenerAdapter { - private final MGZBot mgzBot; - @Override - public void onGuildMemberJoin(@Nonnull GuildMemberJoinEvent event) { - User user = event.getUser(); - Guild guild = event.getGuild(); - if (user.isBot()) - return; - - if (guild.getId().equals(BotConstants.MAIN_GUILD_ID)) { - Role memberRole = guild.getRoleById(793672609395900446L); - if (memberRole != null) - guild.addRoleToMember(user.getId(), memberRole).queue(); - Role newsRole = guild.getRoleById(812440883898875914L); - if (newsRole != null) - guild.addRoleToMember(user.getId(), newsRole).queue(); - - user.openPrivateChannel().queue(privateChannel -> { - EmbedBuilder embedBuilder = EmbedUtils.defaultEmbed(); - embedBuilder.setThumbnail(mgzBot.getJda().getSelfUser().getAvatarUrl()); - embedBuilder.setDescription("Welcome to **McGamerZone** you have received News & Member role."); - privateChannel.sendMessage(embedBuilder.build()).queue(); - }, error -> { - TextChannel textChannelById = guild.getTextChannelById(767396615299923998L); - if (textChannelById != null) - textChannelById.sendMessage(user.getAsMention() + ", I could not sent a message to you due you have private messages disabled!").queue(); - }); - - TextChannel textChannelById = guild.getTextChannelById(812453030405996564L); - if (textChannelById == null) - return; - - EmbedBuilder embedBuilder = EmbedUtils.defaultEmbed(); - embedBuilder.setTitle("Welcome to McGamerZone, " + user.getAsTag()); - embedBuilder.setThumbnail(user.getAvatarUrl()); - embedBuilder.setDescription("This is the official Discord server for McGamerZone Minecraft server." + - "We are a fun Server that is focused on creativity, community-building, and keeping to the" + - "core of the game itself. Our goal here; is to maintain a friendly, fun, " + - "and equal community for anyone and everyone that joins in and " + - "give the og players of MGZ the nostalgia feeling back!"); - embedBuilder.setTimestamp(event.getMember().getTimeJoined()); - embedBuilder.setFooter("Joined at ยป "); - textChannelById.sendMessage(user.getAsMention()).queue(message -> message.delete().queue()); - textChannelById.sendMessage(embedBuilder.build()).queue(message -> { - message.addReaction(":MGZ_Icon_M:791020631042162729").queue(); - }); - - TextChannel logChannel = mgzBot.getJda().getTextChannelById(BotConstants.HAROLD_LOG); - if (logChannel == null) - return; - EmbedBuilder MemberJoinedEmbed = EmbedUtils.successEmbed(); - MemberJoinedEmbed.setTitle("Member Joined"); - MemberJoinedEmbed.addField("Member", user.getAsTag() + " (" + user.getId() + ")", true); - MemberJoinedEmbed.addField("Guild", guild.getName(), true); - MemberJoinedEmbed.addField("Status", "Success", false); - logChannel.sendMessage(MemberJoinedEmbed.build()).queue(); - } - } - - @Override - public void onGuildMemberRemove(@Nonnull GuildMemberRemoveEvent event) { - User user = event.getUser(); - Guild guild = event.getGuild(); - if (user.isBot()) - return; - TextChannel logChannel = mgzBot.getJda().getTextChannelById(BotConstants.HAROLD_LOG); - if (logChannel == null) - return; - - EmbedBuilder embedBuilder = EmbedUtils.errorEmbed(); - embedBuilder.setTitle("Member Left"); - embedBuilder.addField("Member", user.getAsTag() + " (" + user.getId() + ")", true); - embedBuilder.addField("Guild", guild.getName(), true); - embedBuilder.addField("Status", "Success", false); - logChannel.sendMessage(embedBuilder.build()).queue(); - } -} \ No newline at end of file diff --git a/discordbot/src/main/java/zone/themcgamer/discordbot/events/GuildsListener.java b/discordbot/src/main/java/zone/themcgamer/discordbot/events/GuildsListener.java new file mode 100644 index 0000000..38fa045 --- /dev/null +++ b/discordbot/src/main/java/zone/themcgamer/discordbot/events/GuildsListener.java @@ -0,0 +1,56 @@ +package zone.themcgamer.discordbot.events; + +import lombok.RequiredArgsConstructor; +import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.entities.Guild; +import net.dv8tion.jda.api.entities.TextChannel; +import net.dv8tion.jda.api.entities.User; +import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent; +import net.dv8tion.jda.api.events.guild.member.GuildMemberRemoveEvent; +import net.dv8tion.jda.api.events.guild.member.GuildMemberUpdateEvent; +import net.dv8tion.jda.api.hooks.ListenerAdapter; +import zone.themcgamer.discordbot.BotConstants; +import zone.themcgamer.discordbot.MGZBot; +import zone.themcgamer.discordbot.utilities.EmbedUtils; + +import javax.annotation.Nonnull; + +@RequiredArgsConstructor +public class GuildsListener extends ListenerAdapter { + private final MGZBot mgzBot; + @Override + public void onGuildMemberJoin(@Nonnull GuildMemberJoinEvent event) { + User user = event.getUser(); + Guild guild = event.getGuild(); + if (user.isBot()) + return; + + TextChannel logChannel = mgzBot.getJda().getTextChannelById(BotConstants.HAROLD_LOG); + if (logChannel == null) + return; + EmbedBuilder MemberJoinedEmbed = EmbedUtils.successEmbed(); + MemberJoinedEmbed.setTitle("Member Joined"); + MemberJoinedEmbed.addField("Member", user.getAsTag() + " (" + user.getId() + ")", true); + MemberJoinedEmbed.addField("Guild", guild.getName(), true); + MemberJoinedEmbed.addField("Status", "Success", false); + logChannel.sendMessage(MemberJoinedEmbed.build()).queue(); + } + + @Override + public void onGuildMemberRemove(@Nonnull GuildMemberRemoveEvent event) { + User user = event.getUser(); + Guild guild = event.getGuild(); + if (user.isBot()) + return; + TextChannel logChannel = mgzBot.getJda().getTextChannelById(BotConstants.HAROLD_LOG); + if (logChannel == null) + return; + + EmbedBuilder embedBuilder = EmbedUtils.errorEmbed(); + embedBuilder.setTitle("Member Left"); + embedBuilder.addField("Member", user.getAsTag() + " (" + user.getId() + ")", true); + embedBuilder.addField("Guild", guild.getName(), true); + embedBuilder.addField("Status", "Success", false); + logChannel.sendMessage(embedBuilder.build()).queue(); + } +} \ No newline at end of file diff --git a/discordbot/src/main/java/zone/themcgamer/discordbot/events/MainGuildListener.java b/discordbot/src/main/java/zone/themcgamer/discordbot/events/MainGuildListener.java new file mode 100644 index 0000000..1dc00df --- /dev/null +++ b/discordbot/src/main/java/zone/themcgamer/discordbot/events/MainGuildListener.java @@ -0,0 +1,98 @@ +package zone.themcgamer.discordbot.events; + +import lombok.RequiredArgsConstructor; +import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.entities.*; +import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent; +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.MGZBot; +import zone.themcgamer.discordbot.utilities.EmbedUtils; +import zone.themcgamer.discordbot.utilities.GuildUtils; + +import javax.annotation.Nonnull; + +import static zone.themcgamer.discordbot.utilities.GuildUtils.toggleRole; + +@RequiredArgsConstructor +public class MainGuildListener extends ListenerAdapter { + private final MGZBot mgzBot; + + @Override + public void onGuildMemberJoin(@Nonnull GuildMemberJoinEvent event) { + Member member = event.getMember(); + User user = event.getUser(); + Guild guild = event.getGuild(); + if (user.isBot()) + return; + + if (!guild.getId().equals(BotConstants.MAIN_GUILD_ID)) + return; + + Role memberRole = guild.getRoleById(793672609395900446L); + GuildUtils.toggleRole(guild, member, memberRole); + Role newsRole = guild.getRoleById(812440883898875914L); + GuildUtils.toggleRole(guild, member, newsRole); + + user.openPrivateChannel().queue(privateChannel -> { + EmbedBuilder embedBuilder = EmbedUtils.defaultEmbed(); + embedBuilder.setThumbnail(mgzBot.getJda().getSelfUser().getAvatarUrl()); + embedBuilder.setDescription("Welcome to **McGamerZone** you have received News & Member role."); + privateChannel.sendMessage(embedBuilder.build()).queue(); + }, error -> { + TextChannel textChannelById = guild.getTextChannelById(767396615299923998L); + if (textChannelById != null) + textChannelById.sendMessage(user.getAsMention() + ", I could not sent a message to you due you have private messages disabled!").queue(); + }); + + TextChannel textChannelById = guild.getTextChannelById(812453030405996564L); + if (textChannelById == null) + return; + + EmbedBuilder embedBuilder = EmbedUtils.defaultEmbed(); + embedBuilder.setTitle("Welcome to McGamerZone, " + user.getAsTag()); + embedBuilder.setThumbnail(user.getAvatarUrl()); + embedBuilder.setDescription("This is the official Discord server for McGamerZone Minecraft server." + + " We are a fun Server that is focused on creativity, community-building, and keeping to the" + + " core of the game itself. Our goal here; is to maintain a friendly, fun, " + + "and equal community for anyone and everyone that joins in and " + + "give the og players of MGZ the nostalgia feeling back!"); + embedBuilder.setTimestamp(event.getMember().getTimeJoined()); + embedBuilder.setFooter("Joined at ยป "); + textChannelById.sendMessage(user.getAsMention()).queue(message -> message.delete().queue()); + textChannelById.sendMessage(embedBuilder.build()).queue(message -> { + message.addReaction(":MGZ_Icon_M:791020631042162729").queue(); + }); + } + + @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(); + } + } +} diff --git a/discordbot/src/main/java/zone/themcgamer/discordbot/events/ReactionListener.java b/discordbot/src/main/java/zone/themcgamer/discordbot/events/ReactionListener.java deleted file mode 100644 index 5ccdc7e..0000000 --- a/discordbot/src/main/java/zone/themcgamer/discordbot/events/ReactionListener.java +++ /dev/null @@ -1,47 +0,0 @@ -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; - -import static zone.themcgamer.discordbot.utilities.GuildUtils.toggleRole; - -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(); - } - } -} \ No newline at end of file diff --git a/discordbot/src/main/java/zone/themcgamer/discordbot/utilities/GuildUtils.java b/discordbot/src/main/java/zone/themcgamer/discordbot/utilities/GuildUtils.java index e813802..8f028fd 100644 --- a/discordbot/src/main/java/zone/themcgamer/discordbot/utilities/GuildUtils.java +++ b/discordbot/src/main/java/zone/themcgamer/discordbot/utilities/GuildUtils.java @@ -25,6 +25,8 @@ public class GuildUtils { } public static void toggleRole(net.dv8tion.jda.api.entities.Guild guild, Member member, Role role) { + if (role == null) + return; if (member.getRoles().contains(role)) guild.removeRoleFromMember(member.getIdLong(), role).queue(); else guild.addRoleToMember(member.getIdLong(), role).queue();