From 22d307241659aa3cc5bb3095e87aa7dfd418ddf7 Mon Sep 17 00:00:00 2001 From: GitHub Date: Tue, 20 Apr 2021 14:59:47 +0200 Subject: [PATCH 1/6] =?UTF-8?q?=F0=9F=90=9E=20Fixed=20issue=20#29:=20?= =?UTF-8?q?=E2=86=B3=20Added=20try=20catch=20to=20handling=20creation=20of?= =?UTF-8?q?=20roles.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/github/mafelp/discord/RoleAdmin.java | 6 ++- .../discord/commands/CreateRoleListener.java | 45 ++++++++++++------ .../discord/commands/SetupListener.java | 47 +++++++++++++------ 3 files changed, 66 insertions(+), 32 deletions(-) diff --git a/src/main/java/com/github/mafelp/discord/RoleAdmin.java b/src/main/java/com/github/mafelp/discord/RoleAdmin.java index 4dbb812..39bf792 100644 --- a/src/main/java/com/github/mafelp/discord/RoleAdmin.java +++ b/src/main/java/com/github/mafelp/discord/RoleAdmin.java @@ -1,10 +1,12 @@ package com.github.mafelp.discord; +import com.github.mafelp.utils.Logging; import org.bukkit.ChatColor; import org.javacord.api.entity.channel.ServerTextChannel; import org.javacord.api.entity.message.embed.EmbedBuilder; import org.javacord.api.entity.permission.*; import org.javacord.api.entity.server.Server; +import org.javacord.api.exception.MissingPermissionsException; import java.awt.*; @@ -25,7 +27,7 @@ public class RoleAdmin { * @return The newly created role */ public static Role createNewRole(Server server, String name, - EmbedBuilder successEmbed, ServerTextChannel successChannel) { + EmbedBuilder successEmbed, ServerTextChannel successChannel) throws MissingPermissionsException { Permissions permissions = new PermissionsBuilder() .setAllowed(PermissionType.ADD_REACTIONS) .setDenied(PermissionType.ADMINISTRATOR) @@ -73,7 +75,7 @@ public static Role createNewRole(Server server, String name, if (successEmbed != null) successChannel.sendMessage(successEmbed.addField("New Role", "The new role is: " + role.getMentionTag() + "!") - .addField("Usage:","Give the role to any members that should be allowed to view and write to the minecraft channel. Later this will get added automatically with linking!")); + .addField("Usage:", "Give the role to any members that should be allowed to view and write to the minecraft channel. Later this will get added automatically with linking!")); discordApi.getYourself().addRole(role, "MCDC needs to see the channel as well!"); info("Added role \"" + role.getName() + "\" to the discord API."); diff --git a/src/main/java/com/github/mafelp/discord/commands/CreateRoleListener.java b/src/main/java/com/github/mafelp/discord/commands/CreateRoleListener.java index f8d6206..5743032 100644 --- a/src/main/java/com/github/mafelp/discord/commands/CreateRoleListener.java +++ b/src/main/java/com/github/mafelp/discord/commands/CreateRoleListener.java @@ -8,6 +8,7 @@ import org.javacord.api.entity.message.embed.EmbedBuilder; import org.javacord.api.entity.permission.*; import org.javacord.api.event.message.MessageCreateEvent; +import org.javacord.api.exception.MissingPermissionsException; import org.javacord.api.listener.message.MessageCreateListener; import java.awt.*; @@ -78,6 +79,15 @@ public void onMessageCreate(MessageCreateEvent event) { .setFooter("") ; + // Embed to send, when the bot does not have the required Permissions. + EmbedBuilder noPermissionEmbed = new EmbedBuilder() + .setAuthor(discordApi.getYourself()) + .setTitle("Error!") + .addField("PermissionDeniedException","Could not execute this command, because the bot lacks permissions to do so!") + .addField("how to fix", "Please refer to this projects website https://mafelp.github.io/MCDC/create-admin-role for instructions, on how to do so.") + .setColor(Color.RED) + ; + // If the message is empty/if the arguments are none, return if (command.getCommand() == null) return; @@ -106,21 +116,26 @@ public void onMessageCreate(MessageCreateEvent event) { // TODO add linking and automatic linking of roles. - if (event.getChannel().asServerTextChannel().isPresent()) { - Role role = RoleAdmin.createNewRole(event.getServer().get(), command.getStringArgument(0).get(), successEmbed, event.getChannel().asServerTextChannel().get()); - event.getMessageAuthor().asUser().ifPresent(user -> { - user.addRole(role, "MCDC role creation: Person who created the role should get the role assigned, as well."); - info("Added role \"" + role.getName() + "\" to player \"" + user.getName() + "\"."); - }); - } else { - minecraftServer.getLogger().warning(prefix + "Could not get the ServerTextChannel. Sending error embed."); - event.getChannel().sendMessage( - new EmbedBuilder() - .setAuthor(event.getMessageAuthor()) - .setColor(Color.RED) - .setTitle("Error!") - .addField("ServerTextChannelNotPresentError","Could not get this Channel as a server text channel. Maybe you sent this message in private message?") - ); + try { + if (event.getChannel().asServerTextChannel().isPresent()) { + Role role = RoleAdmin.createNewRole(event.getServer().get(), command.getStringArgument(0).get(), successEmbed, event.getChannel().asServerTextChannel().get()); + event.getMessageAuthor().asUser().ifPresent(user -> { + user.addRole(role, "MCDC role creation: Person who created the role should get the role assigned, as well."); + info("Added role \"" + role.getName() + "\" to player \"" + user.getName() + "\"."); + }); + } else { + minecraftServer.getLogger().warning(prefix + "Could not get the ServerTextChannel. Sending error embed."); + event.getChannel().sendMessage( + new EmbedBuilder() + .setAuthor(event.getMessageAuthor()) + .setColor(Color.RED) + .setTitle("Error!") + .addField("ServerTextChannelNotPresentError", "Could not get this Channel as a server text channel. Maybe you sent this message in private message?") + ); + } + } catch (MissingPermissionsException exception) { + event.getChannel().sendMessage(noPermissionEmbed); + Logging.info(ChatColor.RED + "Could not execute Setup command. Do not have the required permissions."); } } } diff --git a/src/main/java/com/github/mafelp/discord/commands/SetupListener.java b/src/main/java/com/github/mafelp/discord/commands/SetupListener.java index 2aa67d6..9beed43 100644 --- a/src/main/java/com/github/mafelp/discord/commands/SetupListener.java +++ b/src/main/java/com/github/mafelp/discord/commands/SetupListener.java @@ -10,9 +10,11 @@ import com.github.mafelp.utils.exceptions.NoCommandGivenException; import org.bukkit.ChatColor; import org.javacord.api.entity.channel.ServerTextChannel; +import org.javacord.api.entity.message.embed.Embed; import org.javacord.api.entity.message.embed.EmbedBuilder; import org.javacord.api.entity.permission.Role; import org.javacord.api.event.message.MessageCreateEvent; +import org.javacord.api.exception.MissingPermissionsException; import org.javacord.api.listener.message.MessageCreateListener; import java.awt.*; @@ -91,6 +93,15 @@ public void onMessageCreate(MessageCreateEvent event) { .setFooter("Made by MaFeLP: https://github.com/mafelp") ; + // Embed to send, when the bot does not have the required Permissions. + EmbedBuilder noPermissionEmbed = new EmbedBuilder() + .setAuthor(discordApi.getYourself()) + .setTitle("Error!") + .addField("PermissionDeniedException","Could not execute this command, because the bot lacks permissions to do so!") + .addField("how to fix", "Please refer to this projects website https://mafelp.github.io/MCDC/create-admin-role for instructions, on how to do so.") + .setColor(Color.RED) + ; + // If the message is empty/if the arguments are none, return if (command.getCommand() == null) return; @@ -138,25 +149,31 @@ public void onMessageCreate(MessageCreateEvent event) { String name = command.getStringArgument(0).get(); - Role role = RoleAdmin.createNewRole(event.getServer().get(), name, null, event.getChannel().asServerTextChannel().get()); - event.getMessageAuthor().asUser().ifPresent(user -> { - user.addRole(role, "MCDC role creation: Person who created the role should get the role assigned, as well."); - info("Added role \"" + role.getName() + "\" to player \"" + user.getName() + "\"."); - }); + try { + Role role = RoleAdmin.createNewRole(event.getServer().get(), name, null, event.getChannel().asServerTextChannel().get()); - successEmbed.addField("Successful role creation", "Successfully created the new role " + role.getMentionTag() + " to sync permissions for the Minecraft Channels to."); + event.getMessageAuthor().asUser().ifPresent(user -> { + user.addRole(role, "MCDC role creation: Person who created the role should get the role assigned, as well."); + info("Added role \"" + role.getName() + "\" to player \"" + user.getName() + "\"."); + }); - if (event.getServerTextChannel().isEmpty()) { - return; - } + successEmbed.addField("Successful role creation", "Successfully created the new role " + role.getMentionTag() + " to sync permissions for the Minecraft Channels to."); - ServerTextChannel serverTextChannel = ChannelAdmin.createChannel(name, event.getServer().get(), "Minecraft Cross platform communication.", successEmbed, event.getServerTextChannel().get(), welcomeEmbed); + if (event.getServerTextChannel().isEmpty()) { + return; + } - if (serverTextChannel == null) { - minecraftServer.getLogger().warning("Could not create the server Text channel. Unknown error!"); - return; - } + ServerTextChannel serverTextChannel = ChannelAdmin.createChannel(name, event.getServer().get(), "Minecraft Cross platform communication.", successEmbed, event.getServerTextChannel().get(), welcomeEmbed); - Logging.info("Added channel \"" + serverTextChannel.getName() + "\" and role \"" + role.getName() + "\" to server \"" + event.getServer().get().getName() + "\"!"); + if (serverTextChannel == null) { + minecraftServer.getLogger().warning("Could not create the server Text channel. Unknown error!"); + return; + } + + Logging.info("Added channel \"" + serverTextChannel.getName() + "\" and role \"" + role.getName() + "\" to server \"" + event.getServer().get().getName() + "\"!"); + } catch (MissingPermissionsException exception) { + event.getChannel().sendMessage(noPermissionEmbed); + Logging.info(ChatColor.RED + "Could not execute Setup command. Do not have the required permissions."); + } } } From 56e54cc8b64379891f04faccee841b4398f06324 Mon Sep 17 00:00:00 2001 From: GitHub Date: Tue, 20 Apr 2021 21:09:43 +0200 Subject: [PATCH 2/6] =?UTF-8?q?=F0=9F=90=9E=20Further=20fixed=20issue=20#2?= =?UTF-8?q?9=20=E2=86=B3=20The=20bot=20now=20sends=20you=20an=20error=20me?= =?UTF-8?q?ssage,=20when=20it=20doesn't=20have=20the=20required=20permissi?= =?UTF-8?q?ons.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../github/mafelp/discord/ChannelAdmin.java | 62 +++++++++---------- .../com/github/mafelp/discord/RoleAdmin.java | 5 +- .../commands/CreateChannelListener.java | 35 +++++++---- .../discord/commands/CreateRoleListener.java | 6 +- .../discord/commands/SetupListener.java | 5 +- 5 files changed, 58 insertions(+), 55 deletions(-) diff --git a/src/main/java/com/github/mafelp/discord/ChannelAdmin.java b/src/main/java/com/github/mafelp/discord/ChannelAdmin.java index 70ee1cf..8c2ba0d 100644 --- a/src/main/java/com/github/mafelp/discord/ChannelAdmin.java +++ b/src/main/java/com/github/mafelp/discord/ChannelAdmin.java @@ -15,7 +15,7 @@ import java.awt.*; import java.util.ArrayList; import java.util.List; -import java.util.concurrent.atomic.AtomicReference; +import java.util.concurrent.CompletionException; /** * The class that manages the discord channels, whose messages are relayed to the @@ -58,39 +58,33 @@ private static List getMessageChannels() { */ public static ServerTextChannel createChannel(String name, Server server, String topic, EmbedBuilder successEmbed, TextChannel successChannel, - EmbedBuilder welcomeEmbed) { - try { - // Create the Channel - ServerTextChannel serverTextChannel = new ServerTextChannelBuilder(server) - .setName(name) - .setTopic(topic) - .setAuditLogReason("Creating Channel for communication with Minecraft Server") - .create() - .join(); - - // After the channel has been created - // TODO Add: Only the role specified in RoleAdmin can see and write to the channel - Logging.info("Added channel " + serverTextChannel.getName() + " to server " + serverTextChannel.getServer().getName()); - - // Add a field containing a link to the new channel and send the embed - successEmbed.addField("New Channel", - "The new channel is: <#" + serverTextChannel.getIdAsString() + ">"); - successChannel.sendMessage(successEmbed); - - // Also send the welcome embed into the newly created channel - serverTextChannel.sendMessage(welcomeEmbed); - - // Add the id of the new channel to the list of ids in the configuration and save/reload it - List ids = Settings.getConfiguration().getLongList("channelIDs"); - ids.add(serverTextChannel.getId()); - Settings.getConfiguration().set("channelIDs", ids); - - return serverTextChannel; - - } catch (Exception exception) { - Logging.logException(exception, "Something went wrong while trying to create a text channel."); - return null; - } + EmbedBuilder welcomeEmbed) throws CompletionException { + // Create the Channel + ServerTextChannel serverTextChannel = new ServerTextChannelBuilder(server) + .setName(name) + .setTopic(topic) + .setAuditLogReason("Creating Channel for communication with Minecraft Server") + .create() + .join(); + + // After the channel has been created + // TODO Add: Only the role specified in RoleAdmin can see and write to the channel + Logging.info("Added channel " + serverTextChannel.getName() + " to server " + serverTextChannel.getServer().getName()); + + // Add a field containing a link to the new channel and send the embed + successEmbed.addField("New Channel", + "The new channel is: <#" + serverTextChannel.getIdAsString() + ">"); + successChannel.sendMessage(successEmbed); + + // Also send the welcome embed into the newly created channel + serverTextChannel.sendMessage(welcomeEmbed); + + // Add the id of the new channel to the list of ids in the configuration and save/reload it + List ids = Settings.getConfiguration().getLongList("channelIDs"); + ids.add(serverTextChannel.getId()); + Settings.getConfiguration().set("channelIDs", ids); + + return serverTextChannel; } /** diff --git a/src/main/java/com/github/mafelp/discord/RoleAdmin.java b/src/main/java/com/github/mafelp/discord/RoleAdmin.java index 39bf792..8fba134 100644 --- a/src/main/java/com/github/mafelp/discord/RoleAdmin.java +++ b/src/main/java/com/github/mafelp/discord/RoleAdmin.java @@ -1,14 +1,13 @@ package com.github.mafelp.discord; -import com.github.mafelp.utils.Logging; import org.bukkit.ChatColor; import org.javacord.api.entity.channel.ServerTextChannel; import org.javacord.api.entity.message.embed.EmbedBuilder; import org.javacord.api.entity.permission.*; import org.javacord.api.entity.server.Server; -import org.javacord.api.exception.MissingPermissionsException; import java.awt.*; +import java.util.concurrent.CompletionException; import static com.github.mafelp.utils.Logging.info; import static com.github.mafelp.utils.Settings.discordApi; @@ -27,7 +26,7 @@ public class RoleAdmin { * @return The newly created role */ public static Role createNewRole(Server server, String name, - EmbedBuilder successEmbed, ServerTextChannel successChannel) throws MissingPermissionsException { + EmbedBuilder successEmbed, ServerTextChannel successChannel) throws CompletionException { Permissions permissions = new PermissionsBuilder() .setAllowed(PermissionType.ADD_REACTIONS) .setDenied(PermissionType.ADMINISTRATOR) diff --git a/src/main/java/com/github/mafelp/discord/commands/CreateChannelListener.java b/src/main/java/com/github/mafelp/discord/commands/CreateChannelListener.java index cd10b57..e6a75f8 100644 --- a/src/main/java/com/github/mafelp/discord/commands/CreateChannelListener.java +++ b/src/main/java/com/github/mafelp/discord/commands/CreateChannelListener.java @@ -4,13 +4,14 @@ import com.github.mafelp.discord.ChannelAdmin; import com.github.mafelp.utils.exceptions.CommandNotFinishedException; import com.github.mafelp.utils.exceptions.NoCommandGivenException; +import org.bukkit.ChatColor; import org.javacord.api.entity.message.embed.EmbedBuilder; import org.javacord.api.event.message.MessageCreateEvent; import org.javacord.api.listener.message.MessageCreateListener; import java.awt.*; import java.util.Arrays; -import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.CompletionException; import static com.github.mafelp.utils.Logging.info; import static com.github.mafelp.utils.Settings.*; @@ -124,7 +125,7 @@ public void onMessageCreate(MessageCreateEvent event) { } // Saves the states of the channel creation - AtomicBoolean success = new AtomicBoolean(false); + boolean success = false; String name; @@ -136,16 +137,26 @@ public void onMessageCreate(MessageCreateEvent event) { } // If the server is present, create a new channel - final String finalName = name; - event.getServer().ifPresent(server -> {success.set(true); - ChannelAdmin.createChannel(finalName, server, - "Cross communication channel with the Minecraft Server " + Settings.serverName, - successEmbed, event.getChannel(), - welcomeEmbed); - }); - - // If the channel could not be created, print an error warning - if(!success.get()) { + if (event.getServer().isPresent()) { + try { + ChannelAdmin.createChannel(name, event.getServer().get(), + "Cross communication channel with the Minecraft Server " + Settings.serverName, + successEmbed, event.getChannel(), + welcomeEmbed); + } catch (CompletionException exception) { + // Embed to send, when the bot does not have the required Permissions. + EmbedBuilder noPermissionEmbed = new EmbedBuilder() + .setAuthor(discordApi.getYourself()) + .setTitle("Error!") + .addField("PermissionDeniedException","Could not execute this command, because the bot lacks permissions to do so!") + .addField("how to fix", "Please refer to this projects website https://mafelp.github.io/MCDC/create-admin-role for instructions, on how to do so.") + .setColor(Color.RED) + ; + + event.getChannel().sendMessage(noPermissionEmbed); + Logging.info(ChatColor.RED + "Could not execute createChannel command. Do not have the required permissions."); + } + } else { Settings.minecraftServer.getLogger().warning(Settings.prefix + "Channel not present."); Settings.minecraftServer.getLogger().warning(Settings.prefix + "Could not execute Command createChannel"); diff --git a/src/main/java/com/github/mafelp/discord/commands/CreateRoleListener.java b/src/main/java/com/github/mafelp/discord/commands/CreateRoleListener.java index 5743032..661e208 100644 --- a/src/main/java/com/github/mafelp/discord/commands/CreateRoleListener.java +++ b/src/main/java/com/github/mafelp/discord/commands/CreateRoleListener.java @@ -8,10 +8,10 @@ import org.javacord.api.entity.message.embed.EmbedBuilder; import org.javacord.api.entity.permission.*; import org.javacord.api.event.message.MessageCreateEvent; -import org.javacord.api.exception.MissingPermissionsException; import org.javacord.api.listener.message.MessageCreateListener; import java.awt.*; +import java.util.concurrent.CompletionException; import static com.github.mafelp.utils.Logging.info; import static com.github.mafelp.utils.Settings.*; @@ -133,9 +133,9 @@ public void onMessageCreate(MessageCreateEvent event) { .addField("ServerTextChannelNotPresentError", "Could not get this Channel as a server text channel. Maybe you sent this message in private message?") ); } - } catch (MissingPermissionsException exception) { + } catch (CompletionException exception) { event.getChannel().sendMessage(noPermissionEmbed); - Logging.info(ChatColor.RED + "Could not execute Setup command. Do not have the required permissions."); + Logging.info(ChatColor.RED + "Could not execute createRole command. Do not have the required permissions."); } } } diff --git a/src/main/java/com/github/mafelp/discord/commands/SetupListener.java b/src/main/java/com/github/mafelp/discord/commands/SetupListener.java index 9beed43..6685e40 100644 --- a/src/main/java/com/github/mafelp/discord/commands/SetupListener.java +++ b/src/main/java/com/github/mafelp/discord/commands/SetupListener.java @@ -10,14 +10,13 @@ import com.github.mafelp.utils.exceptions.NoCommandGivenException; import org.bukkit.ChatColor; import org.javacord.api.entity.channel.ServerTextChannel; -import org.javacord.api.entity.message.embed.Embed; import org.javacord.api.entity.message.embed.EmbedBuilder; import org.javacord.api.entity.permission.Role; import org.javacord.api.event.message.MessageCreateEvent; -import org.javacord.api.exception.MissingPermissionsException; import org.javacord.api.listener.message.MessageCreateListener; import java.awt.*; +import java.util.concurrent.CompletionException; import static com.github.mafelp.utils.Logging.info; import static com.github.mafelp.utils.Settings.*; @@ -171,7 +170,7 @@ public void onMessageCreate(MessageCreateEvent event) { } Logging.info("Added channel \"" + serverTextChannel.getName() + "\" and role \"" + role.getName() + "\" to server \"" + event.getServer().get().getName() + "\"!"); - } catch (MissingPermissionsException exception) { + } catch (CompletionException exception) { event.getChannel().sendMessage(noPermissionEmbed); Logging.info(ChatColor.RED + "Could not execute Setup command. Do not have the required permissions."); } From dd3a58a5f2f2264f3cfd837b12010233fec7b6a0 Mon Sep 17 00:00:00 2001 From: GitHub Date: Tue, 20 Apr 2021 22:13:48 +0200 Subject: [PATCH 3/6] =?UTF-8?q?=E2=9E=95=20Added=20starting=20the=20discor?= =?UTF-8?q?d=20bot=20instance=20in=20a=20new=20thread=20to=20not=20halt=20?= =?UTF-8?q?the=20server=20while=20the=20bot=20is=20being=20logged=20in.=20?= =?UTF-8?q?=E2=9E=A1=EF=B8=8FMoved=20to=20version=200.8.3.1-beta,=20which?= =?UTF-8?q?=20includes=20the=20bug=20fix=20for=20#29.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- src/main/java/com/github/mafelp/discord/DiscordMain.java | 5 +++-- src/main/java/com/github/mafelp/minecraft/Main.java | 4 +++- .../java/com/github/mafelp/minecraft/commands/Config.java | 4 +++- .../java/com/github/mafelp/minecraft/commands/Token.java | 4 +++- src/main/java/com/github/mafelp/utils/Settings.java | 2 +- 6 files changed, 14 insertions(+), 7 deletions(-) diff --git a/pom.xml b/pom.xml index bdb7fd1..637b47d 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.github.mafelp mcdc - 0.8.3-beta + 0.8.3.1-beta jar Mcdc diff --git a/src/main/java/com/github/mafelp/discord/DiscordMain.java b/src/main/java/com/github/mafelp/discord/DiscordMain.java index 8f849a4..fdfc6c5 100644 --- a/src/main/java/com/github/mafelp/discord/DiscordMain.java +++ b/src/main/java/com/github/mafelp/discord/DiscordMain.java @@ -16,11 +16,12 @@ /** * The class that handles initiation and destruction of the discord bot instance(s) */ -public class DiscordMain { +public class DiscordMain extends Thread { /** * Method used to create the bot instance and log it in */ - public static void init() { + @Override + public void run() { // check if a token was specified if (Settings.getApiToken() == null) { // if no token was specified: diff --git a/src/main/java/com/github/mafelp/minecraft/Main.java b/src/main/java/com/github/mafelp/minecraft/Main.java index d3b77f7..1feb0df 100644 --- a/src/main/java/com/github/mafelp/minecraft/Main.java +++ b/src/main/java/com/github/mafelp/minecraft/Main.java @@ -45,7 +45,9 @@ public void onEnable() { commandRegistration(); // Initialize and try starting up the discord bot. - DiscordMain.init(); + Thread discordInitThread = new DiscordMain(); + discordInitThread.setName("Initializing the Discord instance."); + discordInitThread.start(); } /** diff --git a/src/main/java/com/github/mafelp/minecraft/commands/Config.java b/src/main/java/com/github/mafelp/minecraft/commands/Config.java index cb0e844..fee5178 100644 --- a/src/main/java/com/github/mafelp/minecraft/commands/Config.java +++ b/src/main/java/com/github/mafelp/minecraft/commands/Config.java @@ -66,7 +66,9 @@ public boolean onCommand(@NotNull CommandSender commandSender, @NotNull org.bukk DiscordMain.shutdown(); // Reload sequence init(); - DiscordMain.init(); + Thread discordInitThread = new DiscordMain(); + discordInitThread.setName("Initializing the Discord instance."); + discordInitThread.start(); commandSender.sendMessage(prefix + ChatColor.GREEN + "Reload done!"); return true; } diff --git a/src/main/java/com/github/mafelp/minecraft/commands/Token.java b/src/main/java/com/github/mafelp/minecraft/commands/Token.java index d54c1e5..d8f6d52 100644 --- a/src/main/java/com/github/mafelp/minecraft/commands/Token.java +++ b/src/main/java/com/github/mafelp/minecraft/commands/Token.java @@ -66,7 +66,9 @@ public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command // reload configuration Settings.init(); // try to log the bot in - DiscordMain.init(); + Thread discordInitThread = new DiscordMain(); + discordInitThread.setName("Initializing the Discord instance."); + discordInitThread.start(); // send success messages and return commandSender.sendMessage(Settings.prefix + ChatColor.GREEN + "Successfully saved config file!"); commandSender.sendMessage(Settings.prefix + ChatColor.GREEN + "Using token: " + ChatColor.GRAY + Settings.getApiToken()); diff --git a/src/main/java/com/github/mafelp/utils/Settings.java b/src/main/java/com/github/mafelp/utils/Settings.java index 1a3facc..1b1c6ec 100644 --- a/src/main/java/com/github/mafelp/utils/Settings.java +++ b/src/main/java/com/github/mafelp/utils/Settings.java @@ -34,7 +34,7 @@ public class Settings { /** * version number of the plugin - displayed to users */ - public static final String version = "v0.8.3-beta"; + public static final String version = "v0.8.3.1-beta"; /** * enables more information being displayed while executing events From 5af6c0e10b53219f8df4a30a6ef983889d7af7cb Mon Sep 17 00:00:00 2001 From: GitHub Date: Tue, 20 Apr 2021 22:24:01 +0200 Subject: [PATCH 4/6] =?UTF-8?q?=E2=9E=95=20Added=20Permissions=20to=20invi?= =?UTF-8?q?te=20the=20bot=20to=20a=20Server,=20by=20requiring=20additional?= =?UTF-8?q?=20permissions.=20(See=20#30=20for=20more=20information)=20?= =?UTF-8?q?=E2=9E=A1=EF=B8=8FMoved=20to=20version=200.8.4-beta?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- .../github/mafelp/discord/DiscordMain.java | 22 ++++++++++++++++++- .../com/github/mafelp/utils/Settings.java | 2 +- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index 637b47d..a05bd9e 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.github.mafelp mcdc - 0.8.3.1-beta + 0.8.4-beta jar Mcdc diff --git a/src/main/java/com/github/mafelp/discord/DiscordMain.java b/src/main/java/com/github/mafelp/discord/DiscordMain.java index fdfc6c5..8f3270d 100644 --- a/src/main/java/com/github/mafelp/discord/DiscordMain.java +++ b/src/main/java/com/github/mafelp/discord/DiscordMain.java @@ -7,6 +7,9 @@ import com.github.mafelp.discord.commands.CreateChannelListener; import org.bukkit.ChatColor; import org.javacord.api.DiscordApiBuilder; +import org.javacord.api.entity.permission.PermissionType; +import org.javacord.api.entity.permission.Permissions; +import org.javacord.api.entity.permission.PermissionsBuilder; import java.util.concurrent.CompletionException; @@ -31,6 +34,23 @@ public void run() { return; } + // Permissions, the bot needs to have + Permissions botPermissions = new PermissionsBuilder() + .setAllowed(PermissionType.VIEW_AUDIT_LOG) + .setAllowed(PermissionType.MANAGE_ROLES) + .setAllowed(PermissionType.MANAGE_CHANNELS) + .setAllowed(PermissionType.CREATE_INSTANT_INVITE) + .setAllowed(PermissionType.CHANGE_NICKNAME) + .setAllowed(PermissionType.SEND_MESSAGES) + .setAllowed(PermissionType.EMBED_LINKS) + .setAllowed(PermissionType.ATTACH_FILE) + .setAllowed(PermissionType.READ_MESSAGE_HISTORY) + .setAllowed(PermissionType.READ_MESSAGES) + .setAllowed(PermissionType.MENTION_EVERYONE) + .setAllowed(PermissionType.USE_EXTERNAL_EMOJIS) + .setAllowed(PermissionType.ADD_REACTIONS) + .build(); + // TODO Change: make this function a thread / use bukkit scheduler // Log that the instance is being started Logging.info(ChatColor.DARK_GRAY + "Starting Discord Instance..."); @@ -50,7 +70,7 @@ public void run() { // TODO Add: activity Logging.info(ChatColor.GREEN + "Successfully started the discord instance!"); - Logging.info(ChatColor.RESET + "The bot invitation token is: " + discordApi.createBotInvite()); + Logging.info(ChatColor.RESET + "The bot invitation token is: " + discordApi.createBotInvite(botPermissions)); } catch (IllegalStateException | CompletionException exception) { // If the API creation fails, // log an error to the console. diff --git a/src/main/java/com/github/mafelp/utils/Settings.java b/src/main/java/com/github/mafelp/utils/Settings.java index 1b1c6ec..a8df3b7 100644 --- a/src/main/java/com/github/mafelp/utils/Settings.java +++ b/src/main/java/com/github/mafelp/utils/Settings.java @@ -34,7 +34,7 @@ public class Settings { /** * version number of the plugin - displayed to users */ - public static final String version = "v0.8.3.1-beta"; + public static final String version = "v0.8.4-beta"; /** * enables more information being displayed while executing events From ceb4a7a33ab7487075b24baed726d7e3aa70b545 Mon Sep 17 00:00:00 2001 From: GitHub Date: Wed, 21 Apr 2021 12:53:47 +0200 Subject: [PATCH 5/6] =?UTF-8?q?=E2=9E=95=20Added=20threading=20to=20the=20?= =?UTF-8?q?sending=20of=20messages=20from=20Minecraft=20to=20Discord,=20to?= =?UTF-8?q?=20not=20hang=20the=20Server.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../github/mafelp/discord/ChannelAdmin.java | 36 ++-------- .../discord/DiscordMessageBroadcast.java | 67 +++++++++++++++++++ 2 files changed, 72 insertions(+), 31 deletions(-) create mode 100644 src/main/java/com/github/mafelp/discord/DiscordMessageBroadcast.java diff --git a/src/main/java/com/github/mafelp/discord/ChannelAdmin.java b/src/main/java/com/github/mafelp/discord/ChannelAdmin.java index 8c2ba0d..2585654 100644 --- a/src/main/java/com/github/mafelp/discord/ChannelAdmin.java +++ b/src/main/java/com/github/mafelp/discord/ChannelAdmin.java @@ -26,7 +26,7 @@ public class ChannelAdmin { * gets all the channels whose IDs were defined in the config.yml * @return list of all channels of the ids */ - private static List getMessageChannels() { + protected static List getMessageChannels() { // creating new list to return List serverTextChannels = new ArrayList<>(); @@ -91,36 +91,10 @@ public static ServerTextChannel createChannel(String name, Server server, String * Sends an embed with the message string to all channels returned by getMessageChannels() * @param messageAuthor messageAuthor who sent the message to the minecraft chat * @param message the message String to broadcast to the channels - * @return success state */ - public static boolean broadcastMessage(Player messageAuthor, String message) { - // get all channels, where the message should be send to - List channels = getMessageChannels(); - - // if the list is empty, return a failure and log an error - if (channels.isEmpty()) { - Settings.minecraftServer.getLogger().warning("Could not broadcast message: No Channels were selected"); - return false; - } - - // create an embed for the message - //TODO Add: show head of player as author picture - EmbedBuilder embed = new EmbedBuilder() - // .setAuthor(messageAuthor.getDisplayName()) - .setAuthor(messageAuthor.getDisplayName(), "", new Skin(messageAuthor, false).getHead(), ".png") - .setColor(Color.YELLOW) - .setFooter("On " + Settings.serverName) - .addInlineField("Message:", message); - - // send the embed to all channels in the list - for (Channel channel : channels) { - // only send the embed, if the channel is present, to avoid exceptions - if (channel.asServerTextChannel().isPresent()) { - channel.asServerTextChannel().get().sendMessage(embed); - } - } - - // return a success - return true; + public static void broadcastMessage(Player messageAuthor, String message) { + Thread broadcastingThread = new DiscordMessageBroadcast(messageAuthor, message); + broadcastingThread.setName("DiscordMessageBroadcastingThread"); + broadcastingThread.start(); } } diff --git a/src/main/java/com/github/mafelp/discord/DiscordMessageBroadcast.java b/src/main/java/com/github/mafelp/discord/DiscordMessageBroadcast.java new file mode 100644 index 0000000..7fce737 --- /dev/null +++ b/src/main/java/com/github/mafelp/discord/DiscordMessageBroadcast.java @@ -0,0 +1,67 @@ +package com.github.mafelp.discord; + +import com.github.mafelp.minecraft.skins.Skin; +import com.github.mafelp.utils.Settings; +import org.bukkit.entity.Player; +import org.javacord.api.entity.channel.Channel; +import org.javacord.api.entity.message.embed.EmbedBuilder; + +import java.awt.*; +import java.util.List; + +/** + * The Thread to handle sending of discord messages, to not hang the server on Message Sending. + */ +public class DiscordMessageBroadcast extends Thread { + /** + * The person, who sent the message. + */ + private final Player messageAuthor; + + /** + * What the player has sent. + */ + private final String message; + + /** + * Constructor to give the function all needed information. + * @param messageAuthor The person, who sent the message. + * @param message The message, the messageAuthor has sent. + */ + public DiscordMessageBroadcast(Player messageAuthor, String message) { + this.messageAuthor = messageAuthor; + this.message = message; + } + + /** + * The executing method to run the thread and sent all the messages. + */ + @Override + public void run() { + // get all channels, where the message should be send to + List channels = ChannelAdmin.getMessageChannels(); + + // if the list is empty, return a failure and log an error + if (channels.isEmpty()) { + Settings.minecraftServer.getLogger().warning("Could not broadcast message: No Channels were selected"); + return; + } + + // create an embed for the message + //TODO Add: show head of player as author picture + EmbedBuilder embed = new EmbedBuilder() + // .setAuthor(messageAuthor.getDisplayName()) + .setAuthor(messageAuthor.getDisplayName(), "", new Skin(messageAuthor, false).getHead(), ".png") + .setColor(Color.YELLOW) + .setFooter("On " + Settings.serverName) + .addInlineField("Message:", message); + + // send the embed to all channels in the list + for (Channel channel : channels) { + // only send the embed, if the channel is present, to avoid exceptions + if (channel.asServerTextChannel().isPresent()) { + channel.asServerTextChannel().get().sendMessage(embed); + } + } + } +} From 3d50f18111d7f625c23e2b7f1066f5333a655d4e Mon Sep 17 00:00:00 2001 From: GitHub Date: Wed, 21 Apr 2021 20:53:34 +0200 Subject: [PATCH 6/6] =?UTF-8?q?=E2=9E=95=20Added=20CHANGELOG=20for=20lates?= =?UTF-8?q?t=20version.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7340514..03159d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,13 @@ # Changelog +## Small bug fixes +### v0.8.4-beta +➕ Added Error message, when then bot does not have the required permissions, to create Channels or/and roles (See issue [29](https://github.com/MaFeLP/MCDC/issues/29)).
+➕ Added threading to the bot, where the logging in and message sending are going to be handled off the main thread and should not lag the server.
+ +--- + ## More Bug fixes -### v0.8.2-beta and v0.8.3-beta (stable) +### v0.8.2-beta and v0.8.3-beta ➕ Added permission checking to the commands `/token`, `/config`,`.setup`,`.createChannel` and `.createRole`.
➕ Added differentiation of `'` and `"` in arguments. You can now use commands like this: `hello "mafelp's friend!"` with no problems!
➕ Added escape sequences for quotation marks.