Updates in bot

This commit is contained in:
Joel 2021-02-21 22:40:16 +01:00
parent ce72acddf3
commit bb812615b9
7 changed files with 211 additions and 146 deletions

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

@ -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 = "<channelID> <messageID> <reaction>";
userPermissions = new Permission[] { Permission.ADMINISTRATOR };
guildOnly = true;
guilds = Arrays.asList(Guild.MAIN, Guild.TEAM, Guild.TEST);
}
@Override
protected void execute(CommandEvent event, List<String> 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());
});
}
}

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

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

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

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

@ -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();