Skip to content

Commit

Permalink
v1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
boonsboos authored Aug 2, 2021
1 parent b58f2b9 commit 4a75a3e
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 103 deletions.
98 changes: 52 additions & 46 deletions src/main/java/online/monkegame/monkemodmail/DiscordHandler.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package online.monkegame.monkemodmail;

import net.dv8tion.jda.api.Permission;
import online.monkegame.monkemodmail.utils.ColorGenerator;
import online.monkegame.monkemodmail.utils.Database;
import net.dv8tion.jda.api.EmbedBuilder;
Expand Down Expand Up @@ -35,72 +36,77 @@ public void onMessageReceived(MessageReceivedEvent event) {
String p = c.getString("discord.bot-prefix");
Member am = event.getMember();
g = event.getGuild();
Role ro = g.getRoles().stream().filter(r -> r.getName().equalsIgnoreCase(c.getString("access-role"))).findFirst().orElse(null);
if (m.startsWith(p) && !a.isBot() && Objects.requireNonNull(am).getRoles().contains(ro)) {
if (m.startsWith(p) && !a.isBot() && Objects.requireNonNull(am).hasPermission(Permission.MESSAGE_MANAGE)) {
String[] co = m.substring(p.length()).split(" ");
switch (co[0]) {

case "log":
if (co.length>1) {
switch (co[1]) {
case "channel":
try {
String channel = co[2];
if (!channel.contains("<")) {
c.set("logging-channel", channel);
} else {
channel.replaceAll("<#>", "");
c.set("logging-channel", channel);
}
} catch (IndexOutOfBoundsException e) {
ch.sendMessage("Please specify a channel ID!").submit();
break;
if ("channel".equals(co[1])) {
try {
String channel = co[2];
if (!channel.contains("<")) {
c.set("discord.logging-channel", channel);
} else {
channel.replaceAll("<#>", "");
c.set("discord.logging-channel", channel);
}
ch.sendMessage("Channel set successfully! Please restart the server to apply the changes!").submit();
} catch (IndexOutOfBoundsException e) {
ch.sendMessage("Please specify a channel ID!").queue();
break;
case "settings":
MessageEmbed lSE = new EmbedBuilder()
.setTitle("Logging Settings")
.addField("Logging channel", "<#" + c.getString("logging-channel").replace("\"", "") + ">", false)
.addField("Settings role", c.getString("access-role"), false)
.setColor(0x08adf4)
.build();
ch.sendMessageEmbeds(lSE).submit();
break;
case "role":
try {
if (c.contains("@")) {
ch.sendMessage("Please specify the role's name instead of pinging it!").submit();
break;
}
c.set("role", co[2]);
} catch (IndexOutOfBoundsException e) {
ch.sendMessage("Please specify a role's name!").submit();
break;
}
ch.sendMessage("Channel set successfully! Please restart the server to apply the changes!").queue();
} else {
ch.sendMessage("Unknown command!").queue();
}
}
break;
case "modmail":
if (co.length>1) {
if ("channel".equals(co[1])) {
try {
String channel = co[2];
if (!channel.contains("<")) {
c.set("discord.modmail-channel", channel);
} else {
channel.replaceAll("<#>", "");
c.set("discord.modmail-channel", channel);
}
ch.sendMessage("Role set successfully! Please restart the server to apply the changes!").submit();
break;
default:
ch.sendMessage("Unknown command!").submit();
} catch (IndexOutOfBoundsException e) {
ch.sendMessage("Please specify a channel ID!").queue();
break;
}
ch.sendMessage("Channel set successfully! Please restart the server to apply the changes!").queue();
} else {
ch.sendMessage("Unknown command!").queue();
}
} else {
ch.sendMessage(p + "log role/channel/settings").submit();
}
break;
case "settings":
MessageEmbed settings = new EmbedBuilder()
.setTitle("Current settings")
.addField("Logging channel","<#" + c.getString("discord.logging-channel").replace("\"", "") + ">", false)
.addField("Modmail channel","<#" + c.getString("discord.modmail-channel").replace("\"", "") + ">", false)
.setColor(0x18fa91)
.build();
ch.sendMessageEmbeds(settings).queue();
break;
case "help":
MessageEmbed ca = new EmbedBuilder()
MessageEmbed hlep = new EmbedBuilder()
.setTitle("Help! What commands are there?")
.addField("log", "-``settings`` -> shows settings\n-``channel <channel>`` -> sets the channel where things will be logged\n-``role <nameOfRole>`` -> sets the role that can edit the settings",false)
.addField("log", "-``channel <channel>`` -> sets the channel where reports will be logged",false)
.addField("modmail", "-``channel <channel>`` -> sets the channel where modmails will be sent", false)
.addField("setttings", "shows the current settings", false)
.setColor(cg.randomColor())
.build();
ch.sendMessageEmbeds(ca).submit();
ch.sendMessageEmbeds(hlep).queue();
break;
default:
ch.sendMessage("Unknown command!").submit();
ch.sendMessage("Unknown command!").queue();
break;
}
} else if (m.startsWith(p) && !a.isBot()) {
ch.sendMessage("You don't have permission to run the command!").submit();
ch.sendMessage("You don't have permission to run the command!").queue();
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/online/monkegame/monkemodmail/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public void onEnable() {
getLogger().info("| ' \\/ _ \\ ' \\| / / -_) |\\/| / _ \\/ _` | |\\/| / _` | | |");
getLogger().info("|_|_|_\\___/_||_|_\\_\\___|_| |_\\___/\\__,_|_| |_\\__,_|_|_|");
getLogger().info("---------------------------------------------------------");
getLogger().info("Made by Mrs_Herobrine_");
try {
j = JDABuilder.createLight(getConfig().getString("discord.bot-token"), GatewayIntent.GUILD_MESSAGES)
.addEventListeners(new DiscordHandler(configuration, main))
Expand Down Expand Up @@ -61,8 +62,11 @@ public void onEnable() {
public void onDisable() {
if (j!=null) {
j.shutdown();
getLogger().info("Shutting down Discord connection...");
}
getLogger().info("Saving config...");
saveConfig();
getLogger().info("Done! See you next time!");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@ public ModmailCommand(FileConfiguration c, JDA jda, Logger logger) {
this.g = new ColorGenerator();
}


Map<String, Short> cooldowns = new HashMap<>();

@Override
public boolean onCommand(CommandSender s, Command n, String y, String[] args) {
public boolean onCommand(CommandSender s, Command c, String commandName, String[] args) {
int cooldownTime = conf.getInt("command-cooldowns.modmail-cooldown");
if (!(s instanceof Player)) {
return false;
Expand All @@ -57,7 +56,7 @@ public boolean onCommand(CommandSender s, Command n, String y, String[] args) {
.setColor(g.randomColor())
.build();
cooldowns.put(s.getName(), (short) (Instant.now().toEpochMilli()/1000));
ch.sendMessageEmbeds(hlepPls).submit();
ch.sendMessageEmbeds(hlepPls).queue();
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package online.monkegame.monkemodmail.commands;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import online.monkegame.monkemodmail.utils.ColorGenerator;
import online.monkegame.monkemodmail.utils.Database;
import online.monkegame.monkemodmail.utils.PlayerboundMessages;
Expand All @@ -15,6 +17,7 @@
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitScheduler;

import java.sql.SQLException;
import java.time.Instant;
Expand Down Expand Up @@ -56,52 +59,55 @@ public boolean onCommand(CommandSender s, Command c, String a, String[] args) {
if (secondsLeft > 0) {
s.sendMessage("Please wait "+secondsLeft+" more seconds!");
return true;
} else if (secondsLeft <= 0) {
} else {
cooldowns.remove(s.getName());
}
}

if (args.length < 2 || !args[1].matches("[0-9]")) {
s.sendMessage("Please specify a report type!");
s.sendMessage("Please specify a report category!");
pbm.sendReasons(s, l);
return false;
} else {
cooldowns.put(s.getName(), Instant.now().toEpochMilli());
u = Bukkit.getPlayerUniqueId(args[0]);
uAsString = u.toString();
p = Bukkit.getPlayer(u);
reason = Short.parseShort(args[1]);
if ((reason > l.size() || reason < 1) && args[1].matches("[0-9]")) {
s.sendMessage("Bad ID! Here are the report type IDs you can use:");
cooldowns.remove(s.getName());
pbm.sendReasons(s, l);
return true;
}
cooldowns.put(s.getName(), Instant.now().toEpochMilli());
u = Bukkit.getPlayerUniqueId(args[0]);
uAsString = u.toString();
int countReports = db.countReportsPerPlayer(uAsString);
p = Bukkit.getPlayer(u);
reason = Short.parseShort(args[1]);
if ((reason > l.size() || reason < 1) && args[1].matches("[0-9]")) {
s.sendMessage("Bad ID! Here are the report category IDs you can use:");
cooldowns.remove(s.getName());
pbm.sendReasons(s, l);
return true;
}

try {
db.insertReport(uAsString, reason);
} catch (SQLException e) {
e.printStackTrace();
}
try {
db.insertReport(uAsString, reason);
} catch (SQLException e) {
e.printStackTrace();
}

MessageEmbed e = new EmbedBuilder()
.setTitle("Report " + db.countReports())
.setDescription("``" + args[0] + "``" + " has been reported by ``" + s.getName() + "``")
.addField("Player has been reported "+db.countReportsPerPlayer(uAsString)+" time(s)", "", false)
.addField("Reported for", "``" + l.get(reason - 1) + "``", false)
.addField("", "Distrust: " + sl.checkSuspicion(uAsString, p), false)
.setColor(cg.randomColor())
.build();

ch.sendMessageEmbeds(e).submit();
s.sendMessage("Player reported successfully.");
MessageEmbed e = new EmbedBuilder()
.setTitle("Report " + db.countReports())
.setDescription("``" + args[0] + "``" + " has been reported by ``" + s.getName() + "``")
.addField("Player has been reported " + countReports + " time(s)", "", false)
.addField("Reported for ", "``" + l.get(reason - 1) + "``", false)
.addField("", "Distrust: " + sl.checkSuspicion(uAsString, p), false)
.setColor(cg.randomColor())
.build();
ch.sendMessageEmbeds(e).queue();
s.sendMessage(Component.text("Player reported successfully.").color(NamedTextColor.GREEN));
return true;
}
} else if (args!=null && s.getName().equals(args[0])) {
s.sendMessage(Component.text("You can't report yourself!").color(NamedTextColor.RED));
return true;
}
return false;
}


//gets the report categories from the configuration file
public List<String> getReasons(FileConfiguration conf) {

List<String> l = new ArrayList<>();
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/online/monkegame/monkemodmail/utils/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

public class Database {

//ugliest string i've ever written - Mrs_Herobrine_
public String path = System.getProperty("user.dir") + System.getProperty("file.separator") + "plugins" + System.getProperty("file.separator") + "monkeModMail" + System.getProperty("file.separator");

String reportsTable =
Expand Down Expand Up @@ -66,11 +67,10 @@ public void insertReport(String uuid, Short reason) throws SQLException {
public int countReports() {
AtomicInteger amount = new AtomicInteger();
BukkitScheduler b = Bukkit.getScheduler();
b.runTaskAsynchronously(plugin, () -> {

b.runTaskAsynchronously(plugin, ()-> {
String amountOfReports =
"SELECT COUNT(reason) AS reports " +
"FROM reports;";
"FROM reports;";
try (Connection conn = DriverManager.getConnection("jdbc:sqlite:" + path + "files.db");
Statement stmt = conn.createStatement()) {
ResultSet rs = stmt.executeQuery(amountOfReports);
Expand All @@ -81,18 +81,18 @@ public int countReports() {
e.printStackTrace();
}
});
return amount.get() +1;
return amount.get()+1;
}

//gets the amount of times a player has been reported
public int countReportsPerPlayer(String uuid) {
AtomicInteger amount = new AtomicInteger();
BukkitScheduler b = Bukkit.getScheduler();
b.runTaskAsynchronously(plugin, () -> {

b.runTaskAsynchronously(plugin, ()-> {
String amountOfReports =
"SELECT COUNT(reason) AS reports " +
"FROM reports " +
"WHERE uuid='"+uuid+"';";
"FROM reports " +
"WHERE uuid='" + uuid + "';";
try (Connection conn = DriverManager.getConnection("jdbc:sqlite:" + path + "files.db");
Statement stmt = conn.createStatement()) {
ResultSet rs = stmt.executeQuery(amountOfReports);
Expand All @@ -103,7 +103,7 @@ public int countReportsPerPlayer(String uuid) {
e.printStackTrace();
}
});
return amount.get() +1;
return amount.get()+1;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,19 @@

public class PlayerboundMessages {

private final ColorGenerator CG;
public Main m;

public PlayerboundMessages(Main plugin) {
this.m = plugin;
this.CG = new ColorGenerator();
}

//sends the report categories and the IDs you can use
public void sendReasons(CommandSender s, List<String> l) {

BukkitScheduler b = Bukkit.getScheduler();
b.runTaskAsynchronously(m, ()-> {
TextComponent c = Component.text()
.content("Valid reasons:")
.content("Valid categories:")
.color(NamedTextColor.RED)
.decoration(TextDecoration.ITALIC, false).build();
s.sendMessage(c);
Expand Down
Loading

0 comments on commit 4a75a3e

Please sign in to comment.