Skip to content

Commit

Permalink
release (v1.0)
Browse files Browse the repository at this point in the history
i believe the distrust formula to be sufficiently working
  • Loading branch information
boonsboos committed Jul 29, 2021
1 parent 1d33558 commit d706ca0
Show file tree
Hide file tree
Showing 10 changed files with 612 additions and 12 deletions.
107 changes: 107 additions & 0 deletions src/main/java/online/monkegame/monkemodmail/DiscordHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package online.monkegame.monkemodmail;

import online.monkegame.monkemodmail.utils.ColorGenerator;
import online.monkegame.monkemodmail.utils.Database;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.bukkit.configuration.file.FileConfiguration;

import java.util.Objects;

public class DiscordHandler extends ListenerAdapter {

public FileConfiguration c;
public Database db;
public Main p;
public ColorGenerator cg;
public DiscordHandler(FileConfiguration config, Main plugin) {
this.p = plugin;
this.c = config;
this.db = new Database(p);
this.cg = new ColorGenerator();
}

Guild g;

@Override
public void onMessageReceived(MessageReceivedEvent event) {
JDA j = event.getJDA();
MessageChannel ch = event.getChannel();
User a = event.getAuthor();
String m = event.getMessage().getContentRaw();
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)) {
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;
}
ch.sendMessage("Channel set successfully! Please restart the server to apply the changes!").submit();
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("Role set successfully! Please restart the server to apply the changes!").submit();
break;
default:
ch.sendMessage("Unknown command!").submit();
break;
}
} else {
ch.sendMessage(p + "log role/channel/settings").submit();
}
break;
case "help":
MessageEmbed ca = 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)
.setColor(cg.randomColor())
.build();
ch.sendMessageEmbeds(ca).submit();
default:
ch.sendMessage("Unknown command!").submit();
break;
}
} else if (m.startsWith(p) && !a.isBot()) {
ch.sendMessage("You don't have permission to run the command!").submit();
}
}

}
68 changes: 68 additions & 0 deletions src/main/java/online/monkegame/monkemodmail/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package online.monkegame.monkemodmail;

import online.monkegame.monkemodmail.commands.ModmailCommand;
import online.monkegame.monkemodmail.commands.ReportCommand;
import online.monkegame.monkemodmail.utils.Database;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.utils.MemberCachePolicy;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.java.JavaPlugin;

import javax.security.auth.login.LoginException;
import java.io.IOException;

public class Main extends JavaPlugin {

public FileConfiguration configuration;
public JDA j;
public Database db;
public Main main = this;
public Main() {
this.db = new Database(this);
}

@Override
public void onEnable() {

saveDefaultConfig();
configuration = getConfig();
getLogger().info("Config loaded!");
getLogger().info(" _ __ __ _ __ __ _ _");
getLogger().info(" _ __ ___ _ _ | |_____| \\/ |___ __| | \\/ |__ _(_) |");
getLogger().info("| ' \\/ _ \\ ' \\| / / -_) |\\/| / _ \\/ _` | |\\/| / _` | | |");
getLogger().info("|_|_|_\\___/_||_|_\\_\\___|_| |_\\___/\\__,_|_| |_\\__,_|_|_|");
getLogger().info("---------------------------------------------------------");
try {
j = JDABuilder.createLight(getConfig().getString("discord.bot-token"), GatewayIntent.GUILD_MESSAGES)
.addEventListeners(new DiscordHandler(configuration, main))
.setMemberCachePolicy(MemberCachePolicy.ONLINE)
.build()
.awaitReady();

getLogger().info("Connected to Discord!");

} catch (InterruptedException | LoginException e) {

getLogger().severe(e.getMessage());
getLogger().severe("Failed to start! Please configure the plugin!");

}
try {
db.createDB(this.getLogger());
} catch (IOException e) {
e.printStackTrace();
}
getCommand("modmail").setExecutor(new ModmailCommand(configuration, j, this.getLogger()));
getCommand("report").setExecutor(new ReportCommand(configuration, j, main));
}
@Override
public void onDisable() {
if (j!=null) {
j.shutdown();
}
saveConfig();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package online.monkegame.monkemodmail.commands;

import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.MessageChannel;
import net.dv8tion.jda.api.entities.MessageEmbed;
import online.monkegame.monkemodmail.utils.ColorGenerator;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;

import java.time.Instant;
import java.util.*;
import java.util.logging.Logger;
import java.util.stream.Collectors;

public class ModmailCommand implements CommandExecutor {

ColorGenerator g;
JDA j;
FileConfiguration conf;
Logger l;

public ModmailCommand(FileConfiguration c, JDA jda, Logger logger) {
this.conf = c;
this.j = jda;
this.l = logger;
this.g = new ColorGenerator();
}


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

@Override
public boolean onCommand(CommandSender s, Command n, String y, String[] args) {
int cooldownTime = conf.getInt("command-cooldowns.modmail-cooldown");
if (!(s instanceof Player)) {
return false;
} else {
List<String> l = new ArrayList<>(Arrays.asList(args));
MessageChannel ch = j.getTextChannelById(conf.getString("discord.logging-channel"));
short secondsLeft;
if (cooldowns.containsKey(s.getName())) {
secondsLeft = (short) (((cooldowns.get(s.getName()) / 1000) + cooldownTime) - (Instant.now().toEpochMilli() / 1000));
if (secondsLeft>0) {
s.sendMessage("Please wait "+secondsLeft+" more seconds!");
return true;
} else if (secondsLeft <= 0) {
cooldowns.remove(s.getName());
}
}
MessageEmbed hlepPls = new EmbedBuilder()
.setTitle("``" + s.getName() + "`` has requested your help!")
.addField("This is what's wrong: ", "``"+l.stream().map(Object::toString).collect(Collectors.joining(" "))+"``" , false)
.setColor(g.randomColor())
.build();
cooldowns.put(s.getName(), (short) (Instant.now().toEpochMilli()/1000));
ch.sendMessageEmbeds(hlepPls).submit();
return true;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package online.monkegame.monkemodmail.commands;

import online.monkegame.monkemodmail.utils.ColorGenerator;
import online.monkegame.monkemodmail.utils.Database;
import online.monkegame.monkemodmail.utils.PlayerboundMessages;
import online.monkegame.monkemodmail.utils.SuspicionLevel;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.MessageChannel;
import net.dv8tion.jda.api.entities.MessageEmbed;
import online.monkegame.monkemodmail.Main;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;

import java.sql.SQLException;
import java.time.Instant;
import java.util.*;

public class ReportCommand implements CommandExecutor {

public FileConfiguration co;
JDA j;
public Database db;
public ColorGenerator cg;
public SuspicionLevel sl;
public PlayerboundMessages pbm;
public Main plugin;
public ReportCommand(FileConfiguration config, JDA jda, Main plugin) {
this.plugin = plugin;
this.sl = new SuspicionLevel(plugin);
this.co = config;
this.j = jda;
this.db = new Database(plugin);
this.cg = new ColorGenerator();
this.pbm = new PlayerboundMessages(plugin);
}

String uAsString;
UUID u;
Short reason;
Player p;
Map<String, Long> cooldowns = new HashMap<>();
@Override
public boolean onCommand(CommandSender s, Command c, String a, String[] args) {
int cooldownTime = co.getInt("command-cooldowns.report-cooldown");
List<String> l = getReasons(co);
if (s instanceof Player && args != null && !s.getName().equals(args[0])) {
MessageChannel ch = j.getTextChannelById(co.getString("discord.logging-channel"));
long secondsLeft = 0;
if (cooldowns.containsKey(s.getName())) {
secondsLeft = ((cooldowns.get(s.getName()) / 1000) + cooldownTime) - (Instant.now().toEpochMilli() / 1000);
if (secondsLeft > 0) {
s.sendMessage("Please wait "+secondsLeft+" more seconds!");
return true;
} else if (secondsLeft <= 0) {
cooldowns.remove(s.getName());
}
}

if (args.length < 2 || !args[1].matches("[0-9]")) {
s.sendMessage("Please specify a report type!");
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;
}

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.");
return true;
}
}
return false;
}


public List<String> getReasons(FileConfiguration conf) {

List<String> l = new ArrayList<>();
for (Object r : conf.getList("report-reasons")) {
l.add((String) r);
}
return l;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package online.monkegame.monkemodmail.utils;

import net.kyori.adventure.util.HSVLike;

import java.awt.*;

public class ColorGenerator {

//generates a random color
public Color randomColor() {
int r = (int) (Math.random() * 257) - 1;
int g = (int) (Math.random() * 257) - 1;
int b = (int) (Math.random() * 257) - 1;
return new Color(r, g ,b);
}

public HSVLike randomKyoriColor() {
int r = (int) (Math.random() * 257) - 1;
int g = (int) (Math.random() * 257) - 1;
int b = (int) (Math.random() * 257) - 1;
return HSVLike.of(r, g, b);
}
}
Loading

0 comments on commit d706ca0

Please sign in to comment.