Skip to content

Commit

Permalink
Version 4.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Smudgge committed Oct 22, 2023
1 parent cf21824 commit 208b32a
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 24 deletions.
11 changes: 11 additions & 0 deletions src/main/java/com/github/smuddgge/leaf/Leaf.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class Leaf {

private static Leaf plugin;
private static ProxyServer server;
private static Path folder;

private static CommandHandler commandHandler;
private static Database database;
Expand All @@ -63,6 +64,7 @@ public class Leaf {
public Leaf(ProxyServer server, @DataDirectory final Path folder, Metrics.Factory metricsFactory) {
Leaf.server = server;
Leaf.plugin = this;
Leaf.folder = folder;

// Set up the configuration files.
ConfigurationManager.initialise(folder.toFile());
Expand Down Expand Up @@ -189,6 +191,15 @@ public static ProxyServer getServer() {
return Leaf.server;
}

/**
* Used to get the instance of the folder.
*
* @return The instance of the folder.
*/
public static Path getFolder() {
return Leaf.folder;
}

/**
* Used to get the proxy server interface.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public String getMessage() {
if (this.hasStopIncreaseLimit()) return this;

// Check if the command doesn't have a limit.
if (!command.hasLimit()) return this;
if (command.getSection().getSection("discord_bot").getInteger("limit", -1) == -1) return this;

// Increase the limit.
// Check if the database is disabled.
Expand Down
31 changes: 27 additions & 4 deletions src/main/java/com/github/smuddgge/leaf/commands/types/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@
import com.github.smuddgge.leaf.datatype.User;
import com.github.smuddgge.leaf.discord.DiscordBotMessageAdapter;
import com.github.smuddgge.leaf.utility.CommandUtility;
import com.github.smuddgge.leaf.utility.LoggerUtility;
import com.github.smuddgge.leaf.utility.PlayerUtility;
import com.github.smuddgge.squishyconfiguration.interfaces.ConfigurationSection;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.InteractionHook;
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.requests.restaction.CommandCreateAction;
import net.dv8tion.jda.api.utils.messages.MessageEditData;
import org.jetbrains.annotations.NotNull;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -117,11 +121,30 @@ public CommandStatus onDiscordRun(ConfigurationSection section, SlashCommandInte
CommandUtility.executeCommandInConsole(command);
}

// Respond message.
event.reply(new DiscordBotMessageAdapter(
InteractionHook hook = event.reply(new DiscordBotMessageAdapter(
section, "discord_bot.message",
"Ran commands."
).buildMessage()).queue();
"```py\n%Console%\n```".replace("%Console%", "Loading...")
).buildMessage()).complete();

// Run in another thread.
new Thread(() -> {

try {

Thread.sleep(Duration.ofSeconds(2));

// Respond message.
hook.editOriginal(MessageEditData.fromCreateData(new DiscordBotMessageAdapter(
section, "discord_bot.message",
"```py\n%Console%\n```".replace("%Console%", LoggerUtility.getLastLines(
section.getSection("discord_bot").getInteger("lines", 10)
))
).buildMessage())).queue();

} catch (Exception exception) {
exception.printStackTrace();
}
}).start();

return new CommandStatus();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class CommandLimitTable extends TableAdapter<CommandLimitRecord> {
public int getAmountExecuted(@NotNull UUID uuid, @NotNull String commandId) {
CommandLimitRecord record = this.getFirstRecord(
new Query().match(
"uuidPlusCommand",
"primaryKey",
CommandLimitRecord.createPrimaryKey(uuid, commandId)
)
);
Expand All @@ -52,7 +52,7 @@ public int getAmountExecuted(@NotNull UUID uuid, @NotNull String commandId) {
public int getAmountExecuted(@NotNull Member member, @NotNull String commandId) {
CommandLimitRecord record = this.getFirstRecord(
new Query().match(
"uuidPlusCommand",
"primaryKey",
CommandLimitRecord.createPrimaryKey(member, commandId)
)
);
Expand All @@ -72,7 +72,7 @@ public void increaseAmountExecuted(@NotNull UUID uuid, @NotNull String commandId
// Attempt to get the record.
CommandLimitRecord record = this.getFirstRecord(
new Query().match(
"uuidPlusCommand",
"primaryKey",
CommandLimitRecord.createPrimaryKey(uuid, commandId)
)
);
Expand Down Expand Up @@ -103,7 +103,7 @@ public void increaseAmountExecuted(@NotNull Member member, @NotNull String comma
// Attempt to get the record.
CommandLimitRecord record = this.getFirstRecord(
new Query().match(
"uuidPlusCommand",
"primaryKey",
CommandLimitRecord.createPrimaryKey(member, commandId)
)
);
Expand Down
21 changes: 10 additions & 11 deletions src/main/java/com/github/smuddgge/leaf/discord/DiscordBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.github.smuddgge.leaf.MessageManager;
import com.github.smuddgge.leaf.commands.Command;
import com.github.smuddgge.leaf.events.EventManager;
import com.github.smuddgge.squishydatabase.console.Console;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
Expand Down Expand Up @@ -57,11 +56,11 @@ public DiscordBot(@Nullable String token) {
MessageManager.log("&7[Discord Bot] &aEnabled");

} catch (Exception exception) {
Console.warn("Unable to login to the discord bot. Please ensure the following:");
Console.warn("- The discord bot token is correct.");
Console.warn("- The discord bot has the correct &fPrivileged Gateway Intents.");
Console.warn("- The discord bot has the correct permissions on the server.");
Console.warn("Exception for debugging:");
MessageManager.warn("Unable to login to the discord bot. Please ensure the following:");
MessageManager.warn("- The discord bot token is correct.");
MessageManager.warn("- The discord bot has the correct &fPrivileged Gateway Intents.");
MessageManager.warn("- The discord bot has the correct permissions on the server.");
MessageManager.warn("Exception for debugging:");
exception.printStackTrace();
}
}
Expand Down Expand Up @@ -104,11 +103,11 @@ public DiscordBot(@Nullable String token) {
this.discordCommandList.add(discordCommand);

} catch (Exception exception) {
Console.warn("Unable to load command " + command.getName() + ". Please ensure the following:");
Console.warn("- The discord bot token is correct.");
Console.warn("- The discord bot has the correct &fPrivileged Gateway Intents.");
Console.warn("- The discord bot has the correct permissions on the server.");
Console.warn("Exception for debugging:");
MessageManager.warn("Unable to load command " + command.getName() + ". Please ensure the following:");
MessageManager.warn("- The discord bot token is correct.");
MessageManager.warn("- The discord bot has the correct &fPrivileged Gateway Intents.");
MessageManager.warn("- The discord bot has the correct permissions on the server.");
MessageManager.warn("Exception for debugging:");
exception.printStackTrace();
}
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ public void execute(@NotNull SlashCommandInteractionEvent event) {
if (!this.hasRoleFromList(event.getMember())) {
event.reply(new DiscordBotMessageAdapter(
this.command.getSection(),
"discord_bot.no_permission",
"You do not have permission to run this command."
"discord_bot.no_roles",
"You do not have the correct roles to run this command."
).buildMessage()).queue();
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static boolean hasRoleFromList(@NotNull List<String> list, @NotNull Membe
for (Role role : member.getRoles()) {

// Check if the member has the role.
if (roleName.toLowerCase().equals(role.getName().toUpperCase())) return true;
if (roleName.equalsIgnoreCase(role.getName())) return true;
}
}
return false;
Expand Down
46 changes: 46 additions & 0 deletions src/main/java/com/github/smuddgge/leaf/utility/LoggerUtility.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.github.smuddgge.leaf.utility;

import com.github.smuddgge.leaf.Leaf;
import org.jetbrains.annotations.NotNull;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.List;

public class LoggerUtility {

public static @NotNull String getLastLines(int amount) {
StringBuilder stringBuilder = new StringBuilder();

try {

// Open the file.
FileInputStream fileInputStream = new FileInputStream(
Leaf.getFolder().toAbsolutePath().toString()
.replace("/plugins/leaf", "") + "/logs/latest.log"
);

// Get the list of lines in the file.
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
List<String> lineList = bufferedReader.lines().toList();

// Append the last amount.
int position = 0;
for (String line : lineList) {
position++;

if (lineList.size() - amount > position) continue;
stringBuilder.append(line).append("\n");
}

// Close the input stream.
fileInputStream.close();
} catch (Exception exception) {
exception.printStackTrace();
}

// Return the lines.
return stringBuilder.toString();
}
}
2 changes: 1 addition & 1 deletion src/main/resources/commands.yml
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ command:
commands:
- "find %arg_1%"
- "lobby"
message: "Ran commands."
message: "```py\n%Console%\n```"



Expand Down

0 comments on commit 208b32a

Please sign in to comment.