Skip to content

Commit 4a75a3e

Browse files
authored
v1.1
1 parent b58f2b9 commit 4a75a3e

File tree

9 files changed

+118
-103
lines changed

9 files changed

+118
-103
lines changed

src/main/java/online/monkegame/monkemodmail/DiscordHandler.java

Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package online.monkegame.monkemodmail;
22

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

4343
case "log":
4444
if (co.length>1) {
45-
switch (co[1]) {
46-
case "channel":
47-
try {
48-
String channel = co[2];
49-
if (!channel.contains("<")) {
50-
c.set("logging-channel", channel);
51-
} else {
52-
channel.replaceAll("<#>", "");
53-
c.set("logging-channel", channel);
54-
}
55-
} catch (IndexOutOfBoundsException e) {
56-
ch.sendMessage("Please specify a channel ID!").submit();
57-
break;
45+
if ("channel".equals(co[1])) {
46+
try {
47+
String channel = co[2];
48+
if (!channel.contains("<")) {
49+
c.set("discord.logging-channel", channel);
50+
} else {
51+
channel.replaceAll("<#>", "");
52+
c.set("discord.logging-channel", channel);
5853
}
59-
ch.sendMessage("Channel set successfully! Please restart the server to apply the changes!").submit();
54+
} catch (IndexOutOfBoundsException e) {
55+
ch.sendMessage("Please specify a channel ID!").queue();
6056
break;
61-
case "settings":
62-
MessageEmbed lSE = new EmbedBuilder()
63-
.setTitle("Logging Settings")
64-
.addField("Logging channel", "<#" + c.getString("logging-channel").replace("\"", "") + ">", false)
65-
.addField("Settings role", c.getString("access-role"), false)
66-
.setColor(0x08adf4)
67-
.build();
68-
ch.sendMessageEmbeds(lSE).submit();
69-
break;
70-
case "role":
71-
try {
72-
if (c.contains("@")) {
73-
ch.sendMessage("Please specify the role's name instead of pinging it!").submit();
74-
break;
75-
}
76-
c.set("role", co[2]);
77-
} catch (IndexOutOfBoundsException e) {
78-
ch.sendMessage("Please specify a role's name!").submit();
79-
break;
57+
}
58+
ch.sendMessage("Channel set successfully! Please restart the server to apply the changes!").queue();
59+
} else {
60+
ch.sendMessage("Unknown command!").queue();
61+
}
62+
}
63+
break;
64+
case "modmail":
65+
if (co.length>1) {
66+
if ("channel".equals(co[1])) {
67+
try {
68+
String channel = co[2];
69+
if (!channel.contains("<")) {
70+
c.set("discord.modmail-channel", channel);
71+
} else {
72+
channel.replaceAll("<#>", "");
73+
c.set("discord.modmail-channel", channel);
8074
}
81-
ch.sendMessage("Role set successfully! Please restart the server to apply the changes!").submit();
82-
break;
83-
default:
84-
ch.sendMessage("Unknown command!").submit();
75+
} catch (IndexOutOfBoundsException e) {
76+
ch.sendMessage("Please specify a channel ID!").queue();
8577
break;
78+
}
79+
ch.sendMessage("Channel set successfully! Please restart the server to apply the changes!").queue();
80+
} else {
81+
ch.sendMessage("Unknown command!").queue();
8682
}
87-
} else {
88-
ch.sendMessage(p + "log role/channel/settings").submit();
8983
}
9084
break;
85+
case "settings":
86+
MessageEmbed settings = new EmbedBuilder()
87+
.setTitle("Current settings")
88+
.addField("Logging channel","<#" + c.getString("discord.logging-channel").replace("\"", "") + ">", false)
89+
.addField("Modmail channel","<#" + c.getString("discord.modmail-channel").replace("\"", "") + ">", false)
90+
.setColor(0x18fa91)
91+
.build();
92+
ch.sendMessageEmbeds(settings).queue();
93+
break;
9194
case "help":
92-
MessageEmbed ca = new EmbedBuilder()
95+
MessageEmbed hlep = new EmbedBuilder()
9396
.setTitle("Help! What commands are there?")
94-
.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)
97+
.addField("log", "-``channel <channel>`` -> sets the channel where reports will be logged",false)
98+
.addField("modmail", "-``channel <channel>`` -> sets the channel where modmails will be sent", false)
99+
.addField("setttings", "shows the current settings", false)
95100
.setColor(cg.randomColor())
96101
.build();
97-
ch.sendMessageEmbeds(ca).submit();
102+
ch.sendMessageEmbeds(hlep).queue();
103+
break;
98104
default:
99-
ch.sendMessage("Unknown command!").submit();
105+
ch.sendMessage("Unknown command!").queue();
100106
break;
101107
}
102108
} else if (m.startsWith(p) && !a.isBot()) {
103-
ch.sendMessage("You don't have permission to run the command!").submit();
109+
ch.sendMessage("You don't have permission to run the command!").queue();
104110
}
105111
}
106112

src/main/java/online/monkegame/monkemodmail/Main.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public void onEnable() {
3434
getLogger().info("| ' \\/ _ \\ ' \\| / / -_) |\\/| / _ \\/ _` | |\\/| / _` | | |");
3535
getLogger().info("|_|_|_\\___/_||_|_\\_\\___|_| |_\\___/\\__,_|_| |_\\__,_|_|_|");
3636
getLogger().info("---------------------------------------------------------");
37+
getLogger().info("Made by Mrs_Herobrine_");
3738
try {
3839
j = JDABuilder.createLight(getConfig().getString("discord.bot-token"), GatewayIntent.GUILD_MESSAGES)
3940
.addEventListeners(new DiscordHandler(configuration, main))
@@ -61,8 +62,11 @@ public void onEnable() {
6162
public void onDisable() {
6263
if (j!=null) {
6364
j.shutdown();
65+
getLogger().info("Shutting down Discord connection...");
6466
}
67+
getLogger().info("Saving config...");
6568
saveConfig();
69+
getLogger().info("Done! See you next time!");
6670
}
6771

6872
}

src/main/java/online/monkegame/monkemodmail/commands/ModmailCommand.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,10 @@ public ModmailCommand(FileConfiguration c, JDA jda, Logger logger) {
3030
this.g = new ColorGenerator();
3131
}
3232

33-
3433
Map<String, Short> cooldowns = new HashMap<>();
3534

3635
@Override
37-
public boolean onCommand(CommandSender s, Command n, String y, String[] args) {
36+
public boolean onCommand(CommandSender s, Command c, String commandName, String[] args) {
3837
int cooldownTime = conf.getInt("command-cooldowns.modmail-cooldown");
3938
if (!(s instanceof Player)) {
4039
return false;
@@ -57,7 +56,7 @@ public boolean onCommand(CommandSender s, Command n, String y, String[] args) {
5756
.setColor(g.randomColor())
5857
.build();
5958
cooldowns.put(s.getName(), (short) (Instant.now().toEpochMilli()/1000));
60-
ch.sendMessageEmbeds(hlepPls).submit();
59+
ch.sendMessageEmbeds(hlepPls).queue();
6160
return true;
6261
}
6362
}

src/main/java/online/monkegame/monkemodmail/commands/ReportCommand.java

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package online.monkegame.monkemodmail.commands;
22

3+
import net.kyori.adventure.text.Component;
4+
import net.kyori.adventure.text.format.NamedTextColor;
35
import online.monkegame.monkemodmail.utils.ColorGenerator;
46
import online.monkegame.monkemodmail.utils.Database;
57
import online.monkegame.monkemodmail.utils.PlayerboundMessages;
@@ -15,6 +17,7 @@
1517
import org.bukkit.command.CommandSender;
1618
import org.bukkit.configuration.file.FileConfiguration;
1719
import org.bukkit.entity.Player;
20+
import org.bukkit.scheduler.BukkitScheduler;
1821

1922
import java.sql.SQLException;
2023
import java.time.Instant;
@@ -56,52 +59,55 @@ public boolean onCommand(CommandSender s, Command c, String a, String[] args) {
5659
if (secondsLeft > 0) {
5760
s.sendMessage("Please wait "+secondsLeft+" more seconds!");
5861
return true;
59-
} else if (secondsLeft <= 0) {
62+
} else {
6063
cooldowns.remove(s.getName());
6164
}
6265
}
6366

6467
if (args.length < 2 || !args[1].matches("[0-9]")) {
65-
s.sendMessage("Please specify a report type!");
68+
s.sendMessage("Please specify a report category!");
6669
pbm.sendReasons(s, l);
6770
return false;
6871
} else {
69-
cooldowns.put(s.getName(), Instant.now().toEpochMilli());
70-
u = Bukkit.getPlayerUniqueId(args[0]);
71-
uAsString = u.toString();
72-
p = Bukkit.getPlayer(u);
73-
reason = Short.parseShort(args[1]);
74-
if ((reason > l.size() || reason < 1) && args[1].matches("[0-9]")) {
75-
s.sendMessage("Bad ID! Here are the report type IDs you can use:");
76-
cooldowns.remove(s.getName());
77-
pbm.sendReasons(s, l);
78-
return true;
79-
}
72+
cooldowns.put(s.getName(), Instant.now().toEpochMilli());
73+
u = Bukkit.getPlayerUniqueId(args[0]);
74+
uAsString = u.toString();
75+
int countReports = db.countReportsPerPlayer(uAsString);
76+
p = Bukkit.getPlayer(u);
77+
reason = Short.parseShort(args[1]);
78+
if ((reason > l.size() || reason < 1) && args[1].matches("[0-9]")) {
79+
s.sendMessage("Bad ID! Here are the report category IDs you can use:");
80+
cooldowns.remove(s.getName());
81+
pbm.sendReasons(s, l);
82+
return true;
83+
}
8084

81-
try {
82-
db.insertReport(uAsString, reason);
83-
} catch (SQLException e) {
84-
e.printStackTrace();
85-
}
85+
try {
86+
db.insertReport(uAsString, reason);
87+
} catch (SQLException e) {
88+
e.printStackTrace();
89+
}
8690

87-
MessageEmbed e = new EmbedBuilder()
88-
.setTitle("Report " + db.countReports())
89-
.setDescription("``" + args[0] + "``" + " has been reported by ``" + s.getName() + "``")
90-
.addField("Player has been reported "+db.countReportsPerPlayer(uAsString)+" time(s)", "", false)
91-
.addField("Reported for", "``" + l.get(reason - 1) + "``", false)
92-
.addField("", "Distrust: " + sl.checkSuspicion(uAsString, p), false)
93-
.setColor(cg.randomColor())
94-
.build();
95-
96-
ch.sendMessageEmbeds(e).submit();
97-
s.sendMessage("Player reported successfully.");
91+
MessageEmbed e = new EmbedBuilder()
92+
.setTitle("Report " + db.countReports())
93+
.setDescription("``" + args[0] + "``" + " has been reported by ``" + s.getName() + "``")
94+
.addField("Player has been reported " + countReports + " time(s)", "", false)
95+
.addField("Reported for ", "``" + l.get(reason - 1) + "``", false)
96+
.addField("", "Distrust: " + sl.checkSuspicion(uAsString, p), false)
97+
.setColor(cg.randomColor())
98+
.build();
99+
ch.sendMessageEmbeds(e).queue();
100+
s.sendMessage(Component.text("Player reported successfully.").color(NamedTextColor.GREEN));
98101
return true;
99102
}
103+
} else if (args!=null && s.getName().equals(args[0])) {
104+
s.sendMessage(Component.text("You can't report yourself!").color(NamedTextColor.RED));
105+
return true;
100106
}
101107
return false;
102108
}
103109

104-
110+
//gets the report categories from the configuration file
105111
public List<String> getReasons(FileConfiguration conf) {
106112

107113
List<String> l = new ArrayList<>();

src/main/java/online/monkegame/monkemodmail/utils/Database.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
public class Database {
1515

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

1819
String reportsTable =
@@ -66,11 +67,10 @@ public void insertReport(String uuid, Short reason) throws SQLException {
6667
public int countReports() {
6768
AtomicInteger amount = new AtomicInteger();
6869
BukkitScheduler b = Bukkit.getScheduler();
69-
b.runTaskAsynchronously(plugin, () -> {
70-
70+
b.runTaskAsynchronously(plugin, ()-> {
7171
String amountOfReports =
7272
"SELECT COUNT(reason) AS reports " +
73-
"FROM reports;";
73+
"FROM reports;";
7474
try (Connection conn = DriverManager.getConnection("jdbc:sqlite:" + path + "files.db");
7575
Statement stmt = conn.createStatement()) {
7676
ResultSet rs = stmt.executeQuery(amountOfReports);
@@ -81,18 +81,18 @@ public int countReports() {
8181
e.printStackTrace();
8282
}
8383
});
84-
return amount.get() +1;
84+
return amount.get()+1;
8585
}
8686

87+
//gets the amount of times a player has been reported
8788
public int countReportsPerPlayer(String uuid) {
8889
AtomicInteger amount = new AtomicInteger();
8990
BukkitScheduler b = Bukkit.getScheduler();
90-
b.runTaskAsynchronously(plugin, () -> {
91-
91+
b.runTaskAsynchronously(plugin, ()-> {
9292
String amountOfReports =
9393
"SELECT COUNT(reason) AS reports " +
94-
"FROM reports " +
95-
"WHERE uuid='"+uuid+"';";
94+
"FROM reports " +
95+
"WHERE uuid='" + uuid + "';";
9696
try (Connection conn = DriverManager.getConnection("jdbc:sqlite:" + path + "files.db");
9797
Statement stmt = conn.createStatement()) {
9898
ResultSet rs = stmt.executeQuery(amountOfReports);
@@ -103,7 +103,7 @@ public int countReportsPerPlayer(String uuid) {
103103
e.printStackTrace();
104104
}
105105
});
106-
return amount.get() +1;
106+
return amount.get()+1;
107107
}
108108

109109
}

src/main/java/online/monkegame/monkemodmail/utils/PlayerboundMessages.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,19 @@
1414

1515
public class PlayerboundMessages {
1616

17-
private final ColorGenerator CG;
1817
public Main m;
1918

2019
public PlayerboundMessages(Main plugin) {
2120
this.m = plugin;
22-
this.CG = new ColorGenerator();
2321
}
2422

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

2726
BukkitScheduler b = Bukkit.getScheduler();
2827
b.runTaskAsynchronously(m, ()-> {
2928
TextComponent c = Component.text()
30-
.content("Valid reasons:")
29+
.content("Valid categories:")
3130
.color(NamedTextColor.RED)
3231
.decoration(TextDecoration.ITALIC, false).build();
3332
s.sendMessage(c);

0 commit comments

Comments
 (0)