From 81a2459f89807385cccdf498d57c46a35140bc4e Mon Sep 17 00:00:00 2001 From: Joel Date: Sat, 20 Feb 2021 00:01:43 +0100 Subject: [PATCH] Users will receive news & member role on join, added welcome channel. Added aliasses to the SuggestCommand --- .../themcgamer/discordbot/BotConstants.java | 2 +- .../zone/themcgamer/discordbot/MGZBot.java | 7 +- .../command/impl/SuggestCommand.java | 1 + .../command/impl/ToggleNewsRoleCommand.java | 47 +++++++++++++ .../events/GuildMemberJoinQuitListener.java | 66 +++++++++++++++++++ 5 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/ToggleNewsRoleCommand.java create mode 100644 discordbot/src/main/java/zone/themcgamer/discordbot/events/GuildMemberJoinQuitListener.java diff --git a/discordbot/src/main/java/zone/themcgamer/discordbot/BotConstants.java b/discordbot/src/main/java/zone/themcgamer/discordbot/BotConstants.java index e19067c..a518f3e 100644 --- a/discordbot/src/main/java/zone/themcgamer/discordbot/BotConstants.java +++ b/discordbot/src/main/java/zone/themcgamer/discordbot/BotConstants.java @@ -6,7 +6,7 @@ import java.util.Calendar; * @author Nicholas */ public class BotConstants { - public static final String TOKEN = "ODA5NjMxMzcxNzg1Nzk3NjMz.YCX5-Q.t4S8qOmhAc98DKKw9rBsPNv82xM"; + public static final String TOKEN = "NzY1NTI5MTM2NzgxMDY2MjQ2.X4WIkQ.L7pszAdOq1cbqwyhxtr_-qBULpA"; public static final String PREFIX = "."; public static final String OWNER_ID = "504069946528104471"; // Joel diff --git a/discordbot/src/main/java/zone/themcgamer/discordbot/MGZBot.java b/discordbot/src/main/java/zone/themcgamer/discordbot/MGZBot.java index 7fc9716..9bb66d3 100644 --- a/discordbot/src/main/java/zone/themcgamer/discordbot/MGZBot.java +++ b/discordbot/src/main/java/zone/themcgamer/discordbot/MGZBot.java @@ -9,6 +9,8 @@ import net.dv8tion.jda.api.entities.Activity; import net.dv8tion.jda.api.requests.GatewayIntent; import zone.themcgamer.discordbot.command.impl.SetActivityCommand; import zone.themcgamer.discordbot.command.impl.SuggestCommand; +import zone.themcgamer.discordbot.command.impl.ToggleNewsRoleCommand; +import zone.themcgamer.discordbot.events.GuildMemberJoinQuitListener; import javax.security.auth.login.LoginException; import java.util.concurrent.Executors; @@ -34,6 +36,7 @@ public class MGZBot { commandClientBuilder.addCommand(new SuggestCommand()); commandClientBuilder.addCommand(new SetActivityCommand()); + commandClientBuilder.addCommand(new ToggleNewsRoleCommand()); try { jda = JDABuilder.createDefault(BotConstants.TOKEN) @@ -41,7 +44,9 @@ public class MGZBot { .setActivity(Activity.playing("Booting up...")) .setStatus(OnlineStatus.IDLE) .enableIntents(GatewayIntent.GUILD_MEMBERS, GatewayIntent.GUILD_EMOJIS) - .addEventListeners(commandClientBuilder.build()) + .addEventListeners( + commandClientBuilder.build(), + new GuildMemberJoinQuitListener(this)) .build(); jda.awaitReady(); } catch (LoginException | InterruptedException ex) { diff --git a/discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/SuggestCommand.java b/discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/SuggestCommand.java index ed1c6a1..eb83ff3 100644 --- a/discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/SuggestCommand.java +++ b/discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/SuggestCommand.java @@ -18,6 +18,7 @@ import java.util.stream.Collectors; public class SuggestCommand extends BaseCommand { public SuggestCommand() { name = "suggest"; + aliases = new String[]{"suggestion"}; help = "Share a suggestion!"; arguments = ""; guildOnly = true; diff --git a/discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/ToggleNewsRoleCommand.java b/discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/ToggleNewsRoleCommand.java new file mode 100644 index 0000000..3598c23 --- /dev/null +++ b/discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/ToggleNewsRoleCommand.java @@ -0,0 +1,47 @@ +package zone.themcgamer.discordbot.command.impl; + +import com.jagrosh.jdautilities.command.CommandEvent; +import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.entities.Guild; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.Role; +import zone.themcgamer.discordbot.command.BaseCommand; +import zone.themcgamer.discordbot.utilities.EmbedUtils; + +import java.util.Collections; +import java.util.List; + +public class ToggleNewsRoleCommand extends BaseCommand { + + public ToggleNewsRoleCommand() { + name = "togglenews"; + aliases = new String[]{"news"}; + help = "Turn on or off news role."; + guildOnly = true; + guilds = Collections.singletonList(zone.themcgamer.discordbot.guild.Guild.MAIN); + } + + @Override + protected void execute(CommandEvent event, List args) { + Member member = event.getMember(); + Guild guild = event.getGuild(); + if (member == null) + return; + + Role newsRole = guild.getRoleById(812440883898875914L); + if (newsRole == null) + return; + + if (member.getRoles().contains(newsRole)) { + guild.removeRoleFromMember(member.getIdLong(), newsRole).queue(); + EmbedBuilder embedBuilder = EmbedUtils.successEmbed(); + embedBuilder.setDescription("You have successfully removed the " + newsRole.getAsMention() + " role from your account!"); + event.reply(embedBuilder.build()); + } else { + guild.addRoleToMember(member.getIdLong(), newsRole).queue(); + EmbedBuilder embedBuilder = EmbedUtils.successEmbed(); + embedBuilder.setDescription("You have successfully added the " + newsRole.getAsMention() + " role to your account!"); + 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 new file mode 100644 index 0000000..3c8f412 --- /dev/null +++ b/discordbot/src/main/java/zone/themcgamer/discordbot/events/GuildMemberJoinQuitListener.java @@ -0,0 +1,66 @@ +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.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(); + }); + } + } +} \ No newline at end of file