From f7887de08ffa53071e8f59939b32193b0da2fb22 Mon Sep 17 00:00:00 2001 From: thmsdy Date: Fri, 29 May 2020 15:20:53 -0500 Subject: [PATCH] Add captcha, auto generate config, and Spigot support --- config.properties | 19 -- dependency-reduced-pom.xml | 104 ++++++++++- pom.xml | 38 +++- .../java/com/fpghoti/biscuit/Biscuit.java | 87 ++++++++-- src/main/java/com/fpghoti/biscuit/Main.java | 103 +++++++---- .../java/com/fpghoti/biscuit/PluginCore.java | 40 +++++ .../biscuit/commands/CommandListener.java | 2 +- .../biscuit/commands/ConsoleCommand.java | 1 + .../biscuit/commands/client/AddCommand.java | 38 ++++ .../commands/client/ChanIDCommand.java | 3 +- .../commands/client/ChnameCommand.java | 3 +- .../commands/client/DivideCommand.java | 39 +++++ .../commands/client/DontNotifyCommand.java | 7 +- .../biscuit/commands/client/HelpCommand.java | 65 +++++++ .../commands/client/MultiplyCommand.java | 39 +++++ .../commands/client/NotSpammerCommand.java | 3 +- .../commands/client/NotifyCommand.java | 7 +- .../biscuit/commands/client/PingCommand.java | 3 +- .../biscuit/commands/client/PowerCommand.java | 39 +++++ .../client/RecentSpammersCommand.java | 3 +- .../commands/client/ShutDownCommand.java | 32 ---- .../commands/client/SoftMuteCommand.java | 3 +- .../commands/client/SquareRootCommand.java | 38 ++++ .../commands/client/SubtractCommand.java | 39 +++++ .../biscuit/commands/client/UIDCommand.java | 3 +- .../commands/client/UnSoftMuteCommand.java | 3 +- .../biscuit/commands/console/SayCommand.java | 20 ++- .../console/ShutdownConsoleCommand.java | 28 +++ .../biscuit/config/ConfigRetrieval.java | 65 ++++--- .../biscuit/config/PropertiesRetrieval.java | 88 +++++----- .../fpghoti/biscuit/listener/DMListener.java | 128 ++++++++++++++ .../biscuit/listener/JoinListener.java | 10 +- .../listener/MessageReceiveListener.java | 164 ++++++++++-------- .../biscuit/timer/task/DecrementTimer.java | 22 +++ .../com/fpghoti/biscuit/user/PreUser.java | 120 +++++++++++++ .../java/com/fpghoti/biscuit/util/Util.java | 32 ++++ src/main/resources/config.properties | 44 +++++ src/main/resources/plugin.yml | 9 + 38 files changed, 1205 insertions(+), 286 deletions(-) delete mode 100644 config.properties create mode 100644 src/main/java/com/fpghoti/biscuit/PluginCore.java create mode 100644 src/main/java/com/fpghoti/biscuit/commands/client/AddCommand.java create mode 100644 src/main/java/com/fpghoti/biscuit/commands/client/DivideCommand.java create mode 100644 src/main/java/com/fpghoti/biscuit/commands/client/HelpCommand.java create mode 100644 src/main/java/com/fpghoti/biscuit/commands/client/MultiplyCommand.java create mode 100644 src/main/java/com/fpghoti/biscuit/commands/client/PowerCommand.java delete mode 100644 src/main/java/com/fpghoti/biscuit/commands/client/ShutDownCommand.java create mode 100644 src/main/java/com/fpghoti/biscuit/commands/client/SquareRootCommand.java create mode 100644 src/main/java/com/fpghoti/biscuit/commands/client/SubtractCommand.java create mode 100644 src/main/java/com/fpghoti/biscuit/commands/console/ShutdownConsoleCommand.java create mode 100644 src/main/java/com/fpghoti/biscuit/listener/DMListener.java create mode 100644 src/main/java/com/fpghoti/biscuit/timer/task/DecrementTimer.java create mode 100644 src/main/java/com/fpghoti/biscuit/user/PreUser.java create mode 100644 src/main/resources/config.properties create mode 100644 src/main/resources/plugin.yml diff --git a/config.properties b/config.properties deleted file mode 100644 index 6ba2895..0000000 --- a/config.properties +++ /dev/null @@ -1,19 +0,0 @@ -#Change this to redefine the main command signifier character/String -Command-Signifier:- - -#Automatically places spammers in spam mode -AllowSpamPunish:true - -Channels-To-Not-Chatlog:ignore_me - -#Bot token provided by Discord -Bot-Token: - -#Normal channels will have messages with these words deleted by the bot -NaughtyList:piff - -#Bot can automatically assign role upon user join -UseCustomDefaultRole:true - -#Does nothing if UseCustomDefaultRole is off -DefaultRoleName:Standard diff --git a/dependency-reduced-pom.xml b/dependency-reduced-pom.xml index 99ff5fc..a1e97dd 100644 --- a/dependency-reduced-pom.xml +++ b/dependency-reduced-pom.xml @@ -2,8 +2,8 @@ 4.0.0 com.fpghoti - MrBouncer - 3.0 + Biscuit + 1.1 src/main/java @@ -27,7 +27,7 @@ - com.fpghoti.mrbouncer.MrBouncer + com.fpghoti.biscuit.Main @@ -50,6 +50,14 @@ + + spigot-repo + https://hub.spigotmc.org/nexus/content/repositories/snapshots/ + + + bungeecord-repo + https://oss.sonatype.org/content/repositories/snapshots + oss.sonatype.org https://oss.sonatype.org/content/repositories/snapshots/ @@ -57,7 +65,7 @@ jcenter jcenter-bintray - http://jcenter.bintray.com + https://jcenter.bintray.com @@ -68,6 +76,94 @@ + + org.spigotmc + spigot-api + 1.15.1-R0.1-SNAPSHOT + provided + + + commons-lang + commons-lang + + + guava + com.google.guava + + + gson + com.google.code.gson + + + bungeecord-chat + net.md-5 + + + snakeyaml + org.yaml + + + + + org.bukkit + bukkit + 1.15.1-R0.1-SNAPSHOT + provided + + + commons-lang + commons-lang + + + guava + com.google.guava + + + gson + com.google.code.gson + + + snakeyaml + org.yaml + + + + + net.md-5 + bungeecord-api + 1.15-SNAPSHOT + provided + + + bungeecord-config + net.md-5 + + + bungeecord-event + net.md-5 + + + bungeecord-protocol + net.md-5 + + + netty-transport-native-unix-common + io.netty + + + bungeecord-chat + net.md-5 + + + snakeyaml + org.yaml + + + guava + com.google.guava + + + net.java.dev.jna jna diff --git a/pom.xml b/pom.xml index bb7007f..845ecd0 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ 4.0.0 com.fpghoti Biscuit - ALPHA-0.1 + 1.1 src/main/java @@ -30,7 +30,7 @@ - com.fpghoti.biscuit.Biscuit + com.fpghoti.biscuit.Main @@ -54,6 +54,14 @@ + + spigot-repo + https://hub.spigotmc.org/nexus/content/repositories/snapshots/ + + + bungeecord-repo + https://oss.sonatype.org/content/repositories/snapshots + oss.sonatype.org https://oss.sonatype.org/content/repositories/snapshots/ @@ -72,6 +80,25 @@ + + org.spigotmc + spigot-api + 1.15.1-R0.1-SNAPSHOT + provided + + + org.bukkit + bukkit + 1.15.1-R0.1-SNAPSHOT + provided + + + net.md-5 + bungeecord-api + 1.15-SNAPSHOT + jar + provided + com.squareup.okio okio @@ -100,7 +127,12 @@ net.dv8tion JDA - 4.0.0_46 + 4.1.1_155 + + + com.github.cage + cage + 1.0 net.java.dev.jna diff --git a/src/main/java/com/fpghoti/biscuit/Biscuit.java b/src/main/java/com/fpghoti/biscuit/Biscuit.java index 079c8c8..ff1c1fa 100644 --- a/src/main/java/com/fpghoti/biscuit/Biscuit.java +++ b/src/main/java/com/fpghoti/biscuit/Biscuit.java @@ -1,5 +1,6 @@ package com.fpghoti.biscuit; +import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.Timer; @@ -7,28 +8,48 @@ import java.util.Timer; import org.slf4j.Logger; import com.fpghoti.biscuit.commands.CommandManager; +import com.fpghoti.biscuit.config.PropertiesRetrieval; import com.fpghoti.biscuit.timer.BiscuitTimer; +import com.fpghoti.biscuit.user.PreUser; +import com.github.cage.Cage; +import com.github.cage.GCage; import net.dv8tion.jda.api.JDA; +import net.dv8tion.jda.api.entities.Guild; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.Role; import net.dv8tion.jda.api.entities.TextChannel; +import net.dv8tion.jda.api.entities.User; public class Biscuit { - + private JDA jda; private Logger logger; private CommandManager commandManager; private Timer timer; private List timers; - + private File captchaDir; + private Cage cage; + + public Biscuit(JDA jda, Logger log) { this.jda = jda; this.logger = log; - + commandManager = new CommandManager(); timer = new Timer(); timers = new ArrayList(); + if(!Main.isPlugin) { + captchaDir = new File("captcha"); + }else { + captchaDir = new File(PluginCore.plugin.getDataFolder(), "captcha"); + } + captchaDir.mkdir(); + cage = new GCage(); + wipeCaptchaDir(); + loadPreUsers(); } - + public void log(String message) { logger.info(message); } @@ -36,24 +57,24 @@ public class Biscuit { public void warn(String message) { logger.warn(message); } - + public void error(String message) { logger.error(message); } - + public JDA getJDA() { return jda; } - + public CommandManager getCommandManager() { return commandManager; } - + public void say(TextChannel channel, String message) { channel.sendMessage(message).queue(); } - - + + public void loadTimers() { timer.cancel(); timer = new Timer(); @@ -61,15 +82,53 @@ public class Biscuit { timer.schedule(t,t.getDelay(), t.getPeriod()); } } - + public void addTimer(BiscuitTimer bt) { timers.add(bt); } - + public void removeTimer(BiscuitTimer bt) { timers.remove(bt); } - - + public void wipeCaptchaDir() { + Main.log.info("Wiping captcha files..."); + boolean good = true; + File[] files = captchaDir.listFiles(); + for (File file : files){ + if (!file.delete()){ + good = false; + Main.log.error("Could not remove captcha file: " + file); + } + } + if(good) { + Main.log.info("All captcha files successfully removed!"); + }else { + Main.log.error("Some captcha file(s) could not be removed!"); + } + } + + public Cage getCage() { + return this.cage; + } + + private void loadPreUsers() { + for(Guild g : jda.getGuilds()) { + for(Member m : g.getMembers()) { + User u = m.getUser(); + if(!PreUser.preUserExists(u)) { + if(m.getRoles().size() == 1) { + for(Role role : m.getRoles()){ + if(role.getName().equalsIgnoreCase(PropertiesRetrieval.getDefaultRole())){ + PreUser.users.add(new PreUser(u)); + } + } + } + } + } + } + } + + + } diff --git a/src/main/java/com/fpghoti/biscuit/Main.java b/src/main/java/com/fpghoti/biscuit/Main.java index acbe4f9..2030792 100644 --- a/src/main/java/com/fpghoti/biscuit/Main.java +++ b/src/main/java/com/fpghoti/biscuit/Main.java @@ -8,27 +8,36 @@ import org.slf4j.LoggerFactory; import com.fpghoti.biscuit.commands.BaseCommand; import com.fpghoti.biscuit.commands.CommandListener; +import com.fpghoti.biscuit.commands.client.AddCommand; import com.fpghoti.biscuit.commands.client.ChanIDCommand; import com.fpghoti.biscuit.commands.client.ChnameCommand; +import com.fpghoti.biscuit.commands.client.DivideCommand; import com.fpghoti.biscuit.commands.client.DontNotifyCommand; +import com.fpghoti.biscuit.commands.client.HelpCommand; +import com.fpghoti.biscuit.commands.client.MultiplyCommand; import com.fpghoti.biscuit.commands.client.NotSpammerCommand; import com.fpghoti.biscuit.commands.client.NotifyCommand; import com.fpghoti.biscuit.commands.client.PingCommand; +import com.fpghoti.biscuit.commands.client.PowerCommand; import com.fpghoti.biscuit.commands.client.RecentSpammersCommand; -import com.fpghoti.biscuit.commands.client.ShutDownCommand; import com.fpghoti.biscuit.commands.client.SoftMuteCommand; +import com.fpghoti.biscuit.commands.client.SquareRootCommand; +import com.fpghoti.biscuit.commands.client.SubtractCommand; import com.fpghoti.biscuit.commands.client.UIDCommand; import com.fpghoti.biscuit.commands.client.UnSoftMuteCommand; import com.fpghoti.biscuit.commands.console.SayCommand; +import com.fpghoti.biscuit.commands.console.ShutdownConsoleCommand; import com.fpghoti.biscuit.config.ConfigRetrieval; import com.fpghoti.biscuit.config.PropertiesRetrieval; import com.fpghoti.biscuit.global.Properties; +import com.fpghoti.biscuit.listener.DMListener; import com.fpghoti.biscuit.listener.JoinListener; import com.fpghoti.biscuit.listener.MessageDeleteListener; import com.fpghoti.biscuit.listener.MessageEditListener; import com.fpghoti.biscuit.listener.MessageReceiveListener; import com.fpghoti.biscuit.timer.task.BotMsgRemoveTimer; import com.fpghoti.biscuit.timer.task.ChatCountTimer; +import com.fpghoti.biscuit.timer.task.DecrementTimer; import com.fpghoti.biscuit.timer.task.FastMsgRemoveTimer; import com.fpghoti.biscuit.timer.task.SlowMsgRemoveTimer; import com.fpghoti.biscuit.timer.task.SoftMuteTimer; @@ -42,27 +51,31 @@ import net.dv8tion.jda.api.JDABuilder; public class Main { private static JDA jda; - + public RollingFileAppender we; public SizeAndTimeBasedRollingPolicy wes; - + public static final Logger log = LoggerFactory.getLogger("ch.qos.logback.core.ConsoleAppender"); public static Scanner sc; public static Biscuit biscuit; - - + public static boolean ready = false; + public static boolean isPlugin = false; + public static boolean shutdownStarted = false; + + public static void main(String[] args){ ConfigRetrieval.generateConfig(); log.info("========================= Welcome to Biscuit ========================="); startJDA(); biscuit = new Biscuit(jda, log); startCommandListener(); - + jda.addEventListener(new MessageReceiveListener()); jda.addEventListener(new MessageEditListener()); jda.addEventListener(new MessageDeleteListener()); jda.addEventListener(new JoinListener()); - + jda.addEventListener(new DMListener()); + String link = "NULL"; Properties.naughtyList = ConfigRetrieval.getFromConfig("NaughtyList"); Properties.customdefaultrole = ConfigRetrieval.getFromConfig("UseCustomDefaultRole").equalsIgnoreCase("true"); @@ -73,28 +86,36 @@ public class Main { biscuit.addTimer(new FastMsgRemoveTimer()); biscuit.addTimer(new SlowMsgRemoveTimer()); biscuit.addTimer(new SoftMuteTimer()); - + biscuit.addTimer(new DecrementTimer()); + biscuit.loadTimers(); - + List commands = biscuit.getCommandManager().getCommands(); - + //Client - + + commands.add(new HelpCommand()); commands.add(new PingCommand()); commands.add(new SoftMuteCommand()); commands.add(new UnSoftMuteCommand()); commands.add(new NotSpammerCommand()); commands.add(new RecentSpammersCommand()); commands.add(new ChanIDCommand()); - commands.add(new ShutDownCommand()); commands.add(new UIDCommand()); commands.add(new ChnameCommand()); commands.add(new NotifyCommand()); commands.add(new DontNotifyCommand()); - + commands.add(new SquareRootCommand()); + commands.add(new AddCommand()); + commands.add(new SubtractCommand()); + commands.add(new MultiplyCommand()); + commands.add(new DivideCommand()); + commands.add(new PowerCommand()); + //Console - + commands.add(new SayCommand()); + commands.add(new ShutdownConsoleCommand()); link = "https://discordapp.com/oauth2/authorize?&client_id=" + jda.getSelfUser().getId() + "&scope=bot"; log.info("Connection successful!"); @@ -103,8 +124,9 @@ public class Main { log.info(link); log.info("======================================================================"); log.info("CHAT LOGS BEGIN HERE:"); + ready = true; } - + private static void startJDA() { String token = PropertiesRetrieval.getToken(); log.info("Connecting bot to Discord."); @@ -122,23 +144,42 @@ public class Main { shutdown(); } } - + private static void startCommandListener() { - sc = new Scanner(System.in); - CommandListener cl = new CommandListener(); - cl.sc = sc; - cl.jda = jda; - jda.addEventListener(cl); - new Thread(cl).start(); - } - - - public static void shutdown() { - log.info("Shutting down..."); - sc.close(); - System.exit(0); + if(!isPlugin) { + sc = new Scanner(System.in); + CommandListener cl = new CommandListener(); + cl.sc = sc; + cl.jda = jda; + jda.addEventListener(cl); + new Thread(cl).start(); + }else { + CommandListener cl = new CommandListener(); + cl.jda = jda; + jda.addEventListener(cl); + } } - - + public static Biscuit getBiscuit() { + return biscuit; + } + + + public static void shutdown() { + if(!shutdownStarted) { + shutdownStarted = true; + log.info("Shutting down..."); + biscuit.wipeCaptchaDir(); + if(jda != null) { + jda.shutdown(); + } + if(!isPlugin) { + sc.close(); + System.exit(0); + } + } + } + + + } diff --git a/src/main/java/com/fpghoti/biscuit/PluginCore.java b/src/main/java/com/fpghoti/biscuit/PluginCore.java new file mode 100644 index 0000000..b577800 --- /dev/null +++ b/src/main/java/com/fpghoti/biscuit/PluginCore.java @@ -0,0 +1,40 @@ +package com.fpghoti.biscuit; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; + +import com.fpghoti.biscuit.commands.CommandManager; + +public class PluginCore extends JavaPlugin{ + + public static PluginCore plugin; + + public void onEnable(){ + plugin = this; + Main.isPlugin = true; + String[] args = {}; + Main.main(args); + } + + public void onDisable() { + Main.shutdown(); + } + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + CommandManager m = Main.getBiscuit().getCommandManager(); + if(m != null) { + if(sender instanceof Player) { + Player p = (Player)sender; + if(!p.hasPermission("biscuit.admin")) { + return false; + } + } + return m.dispatch(label,args); + } + return false; + } + +} \ No newline at end of file diff --git a/src/main/java/com/fpghoti/biscuit/commands/CommandListener.java b/src/main/java/com/fpghoti/biscuit/commands/CommandListener.java index 328569d..ed8d781 100644 --- a/src/main/java/com/fpghoti/biscuit/commands/CommandListener.java +++ b/src/main/java/com/fpghoti/biscuit/commands/CommandListener.java @@ -21,7 +21,7 @@ public class CommandListener extends ListenerAdapter implements Runnable { @Override public void onMessageReceived(MessageReceivedEvent event){ if(!event.getAuthor().isBot() && event.getMessage().getContentDisplay().startsWith(PropertiesRetrieval.getCommandSignifier()) && event.getMessage().getAuthor().getId() != event.getJDA().getSelfUser().getId()){ - log.info("True"); + //log.info("True"); API.getBiscuit().getCommandManager().parse(event.getMessage().getContentRaw().toLowerCase(), event); } } diff --git a/src/main/java/com/fpghoti/biscuit/commands/ConsoleCommand.java b/src/main/java/com/fpghoti/biscuit/commands/ConsoleCommand.java index 26dcb82..d1456d5 100644 --- a/src/main/java/com/fpghoti/biscuit/commands/ConsoleCommand.java +++ b/src/main/java/com/fpghoti/biscuit/commands/ConsoleCommand.java @@ -7,4 +7,5 @@ public abstract class ConsoleCommand extends BaseCommand{ public CommandType getType() { return CommandType.CONSOLE; } + } diff --git a/src/main/java/com/fpghoti/biscuit/commands/client/AddCommand.java b/src/main/java/com/fpghoti/biscuit/commands/client/AddCommand.java new file mode 100644 index 0000000..7388964 --- /dev/null +++ b/src/main/java/com/fpghoti/biscuit/commands/client/AddCommand.java @@ -0,0 +1,38 @@ +package com.fpghoti.biscuit.commands.client; + +import com.fpghoti.biscuit.Biscuit; +import com.fpghoti.biscuit.api.API; +import com.fpghoti.biscuit.commands.ClientCommand; +import com.fpghoti.biscuit.config.PropertiesRetrieval; +import com.fpghoti.biscuit.util.Util; + +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; + +public class AddCommand extends ClientCommand{ + + public AddCommand() { + name = "Add"; + description = "Finds sum of two numbers."; + usage = PropertiesRetrieval.getCommandSignifier() + "add "; + minArgs = 2; + maxArgs = 2; + identifiers.add("add"); + } + + @Override + public void execute(String[] args, MessageReceivedEvent event) { + Biscuit b = API.getBiscuit(); + b.log(event.getAuthor().getName() + " issued a command: -add"); + if(args[0] != null && Util.isDeciDigit(args[0]) && args[1] != null && Util.isDeciDigit(args[1])) { + double num = Double.parseDouble(args[0]); + double num2 = Double.parseDouble(args[1]); + String sum = Double.toString(num + num2); + String end = sum.substring(Math.max(sum.length() - 2, 0)); + if(end.equals(".0")) { + sum = sum.replace(".0",""); + } + event.getTextChannel().sendMessage(args[0] + " + " + args[1] + " is **" + sum + "**.").queue(); + } + } + +} diff --git a/src/main/java/com/fpghoti/biscuit/commands/client/ChanIDCommand.java b/src/main/java/com/fpghoti/biscuit/commands/client/ChanIDCommand.java index 4351a62..dd2bd16 100644 --- a/src/main/java/com/fpghoti/biscuit/commands/client/ChanIDCommand.java +++ b/src/main/java/com/fpghoti/biscuit/commands/client/ChanIDCommand.java @@ -3,6 +3,7 @@ package com.fpghoti.biscuit.commands.client; import com.fpghoti.biscuit.Biscuit; import com.fpghoti.biscuit.api.API; import com.fpghoti.biscuit.commands.ClientCommand; +import com.fpghoti.biscuit.config.PropertiesRetrieval; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; @@ -11,7 +12,7 @@ public class ChanIDCommand extends ClientCommand{ public ChanIDCommand() { name = "Channel ID"; description = "Retrieves the channel ID."; - usage = "-chanid"; + usage = PropertiesRetrieval.getCommandSignifier() + "chanid"; minArgs = 0; maxArgs = 0; identifiers.add("chanid"); diff --git a/src/main/java/com/fpghoti/biscuit/commands/client/ChnameCommand.java b/src/main/java/com/fpghoti/biscuit/commands/client/ChnameCommand.java index f7440f7..ab7ae99 100644 --- a/src/main/java/com/fpghoti/biscuit/commands/client/ChnameCommand.java +++ b/src/main/java/com/fpghoti/biscuit/commands/client/ChnameCommand.java @@ -3,6 +3,7 @@ package com.fpghoti.biscuit.commands.client; import com.fpghoti.biscuit.Biscuit; import com.fpghoti.biscuit.api.API; import com.fpghoti.biscuit.commands.ClientCommand; +import com.fpghoti.biscuit.config.PropertiesRetrieval; import com.fpghoti.biscuit.util.PermUtil; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; @@ -12,7 +13,7 @@ public class ChnameCommand extends ClientCommand{ public ChnameCommand() { name = "Channel Name"; description = "Retrieves the channel name."; - usage = "-chname"; + usage = PropertiesRetrieval.getCommandSignifier() + "chname"; minArgs = 0; maxArgs = 0; identifiers.add("chname"); diff --git a/src/main/java/com/fpghoti/biscuit/commands/client/DivideCommand.java b/src/main/java/com/fpghoti/biscuit/commands/client/DivideCommand.java new file mode 100644 index 0000000..cb1b9d5 --- /dev/null +++ b/src/main/java/com/fpghoti/biscuit/commands/client/DivideCommand.java @@ -0,0 +1,39 @@ +package com.fpghoti.biscuit.commands.client; + +import com.fpghoti.biscuit.Biscuit; +import com.fpghoti.biscuit.api.API; +import com.fpghoti.biscuit.commands.ClientCommand; +import com.fpghoti.biscuit.config.PropertiesRetrieval; +import com.fpghoti.biscuit.util.Util; + +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; + +public class DivideCommand extends ClientCommand{ + + public DivideCommand() { + name = "Divide"; + description = "Divides two numbers."; + usage = PropertiesRetrieval.getCommandSignifier() + "divide "; + minArgs = 2; + maxArgs = 2; + identifiers.add("divide"); + identifiers.add("div"); + } + + @Override + public void execute(String[] args, MessageReceivedEvent event) { + Biscuit b = API.getBiscuit(); + b.log(event.getAuthor().getName() + " issued a command: -divide"); + if(args[0] != null && Util.isDeciDigit(args[0]) && args[1] != null && Util.isDeciDigit(args[1])) { + double num = Double.parseDouble(args[0]); + double num2 = Double.parseDouble(args[1]); + String divide = Double.toString(num / num2); + String end = divide.substring(Math.max(divide.length() - 2, 0)); + if(end.equals(".0")) { + divide = divide.replace(".0",""); + } + event.getTextChannel().sendMessage(args[0] + " / " + args[1] + " is **" + divide + "**.").queue(); + } + } + +} diff --git a/src/main/java/com/fpghoti/biscuit/commands/client/DontNotifyCommand.java b/src/main/java/com/fpghoti/biscuit/commands/client/DontNotifyCommand.java index 933a24a..0c2c990 100644 --- a/src/main/java/com/fpghoti/biscuit/commands/client/DontNotifyCommand.java +++ b/src/main/java/com/fpghoti/biscuit/commands/client/DontNotifyCommand.java @@ -3,6 +3,7 @@ package com.fpghoti.biscuit.commands.client; import com.fpghoti.biscuit.Biscuit; import com.fpghoti.biscuit.api.API; import com.fpghoti.biscuit.commands.ClientCommand; +import com.fpghoti.biscuit.config.PropertiesRetrieval; import net.dv8tion.jda.api.entities.Emote; import net.dv8tion.jda.api.entities.Role; @@ -13,7 +14,7 @@ public class DontNotifyCommand extends ClientCommand{ public DontNotifyCommand() { name = "Don't Notify"; description = "Puts user in Don't Notify status."; - usage = "-dontnotify"; + usage = PropertiesRetrieval.getCommandSignifier() + "dontnotify"; minArgs = 0; maxArgs = 0; identifiers.add("dontnotify"); @@ -26,7 +27,7 @@ public class DontNotifyCommand extends ClientCommand{ b.log(event.getAuthor().getName() + " issued a command: -dontnotify"); Role role = null; for(Role r : event.getGuild().getRoles()) { - if(r.getName().toLowerCase().contains("don't notify")) { + if(r.getName().toLowerCase().contains(PropertiesRetrieval.getDontNotify())) { role = r; } } @@ -37,7 +38,7 @@ public class DontNotifyCommand extends ClientCommand{ Emote done = null; for(Emote e : event.getGuild().getEmotes()) { - if(e.getName().contains("ActionComplete")) { + if(e.getName().contains(PropertiesRetrieval.getDoneEmote())) { done = e; } } diff --git a/src/main/java/com/fpghoti/biscuit/commands/client/HelpCommand.java b/src/main/java/com/fpghoti/biscuit/commands/client/HelpCommand.java new file mode 100644 index 0000000..cea7f62 --- /dev/null +++ b/src/main/java/com/fpghoti/biscuit/commands/client/HelpCommand.java @@ -0,0 +1,65 @@ +package com.fpghoti.biscuit.commands.client; + +import java.util.List; + +import com.fpghoti.biscuit.Biscuit; +import com.fpghoti.biscuit.Main; +import com.fpghoti.biscuit.commands.BaseCommand; +import com.fpghoti.biscuit.commands.ClientCommand; +import com.fpghoti.biscuit.config.PropertiesRetrieval; +import com.fpghoti.biscuit.util.Util; + +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; + +public class HelpCommand extends ClientCommand { + + public HelpCommand() { + name = "Help"; + description = "Pulls up help menu"; + usage = PropertiesRetrieval.getCommandSignifier() + "help [Page #]"; + minArgs = 0; + maxArgs = 1; + identifiers.add("help"); + } + + @Override + public void execute(String[] args, MessageReceivedEvent event) { + + Biscuit biscuit = Main.getBiscuit(); + + int pg = 1; + if (args.length > 0) { + if(Util.isDigit(args[0])) { + pg = Integer.parseInt(args[0]); + }else { + event.getTextChannel().sendMessage("Usage: ``" + usage + "``").queue(); + } + } + + List commands = biscuit.getCommandManager().getCommands(); + + int pageCount = (int) Math.ceil((double) commands.size() / 8); + if (pg > pageCount) { + pg = pageCount; + } + + event.getTextChannel().sendMessage("[" + Integer.toString(pg) + "/" + Integer.toString(pageCount) + "] **Bot Commands:**").queue(); + String msg = ""; + for (int i = 0; i < 8; i++) { + int index = (pg - 1) * 8 + i; + String line = ""; + if (index < commands.size()) { + line = "**-** ``" + commands.get(index).getUsage() + "``"; + } + if(!(index + 1 >= commands.size() || index == 7)) { + line = line + "\n"; + } + if (index < commands.size()) { + msg = msg + line; + } + } + event.getTextChannel().sendMessage(msg).queue(); + + } + +} diff --git a/src/main/java/com/fpghoti/biscuit/commands/client/MultiplyCommand.java b/src/main/java/com/fpghoti/biscuit/commands/client/MultiplyCommand.java new file mode 100644 index 0000000..3c5ba21 --- /dev/null +++ b/src/main/java/com/fpghoti/biscuit/commands/client/MultiplyCommand.java @@ -0,0 +1,39 @@ +package com.fpghoti.biscuit.commands.client; + +import com.fpghoti.biscuit.Biscuit; +import com.fpghoti.biscuit.api.API; +import com.fpghoti.biscuit.commands.ClientCommand; +import com.fpghoti.biscuit.config.PropertiesRetrieval; +import com.fpghoti.biscuit.util.Util; + +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; + +public class MultiplyCommand extends ClientCommand{ + + public MultiplyCommand() { + name = "Multiply"; + description = "Multiplies two numbers."; + usage = PropertiesRetrieval.getCommandSignifier() + "multiply "; + minArgs = 2; + maxArgs = 2; + identifiers.add("multiply"); + identifiers.add("mul"); + } + + @Override + public void execute(String[] args, MessageReceivedEvent event) { + Biscuit b = API.getBiscuit(); + b.log(event.getAuthor().getName() + " issued a command: -multiply"); + if(args[0] != null && Util.isDeciDigit(args[0]) && args[1] != null && Util.isDeciDigit(args[1])) { + double num = Double.parseDouble(args[0]); + double num2 = Double.parseDouble(args[1]); + String prod = Double.toString(num * num2); + String end = prod.substring(Math.max(prod.length() - 2, 0)); + if(end.equals(".0")) { + prod = prod.replace(".0",""); + } + event.getTextChannel().sendMessage(args[0] + " x " + args[1] + " is **" + prod + "**.").queue(); + } + } + +} diff --git a/src/main/java/com/fpghoti/biscuit/commands/client/NotSpammerCommand.java b/src/main/java/com/fpghoti/biscuit/commands/client/NotSpammerCommand.java index 7c67235..ccbd161 100644 --- a/src/main/java/com/fpghoti/biscuit/commands/client/NotSpammerCommand.java +++ b/src/main/java/com/fpghoti/biscuit/commands/client/NotSpammerCommand.java @@ -3,6 +3,7 @@ package com.fpghoti.biscuit.commands.client; import com.fpghoti.biscuit.Biscuit; import com.fpghoti.biscuit.api.API; import com.fpghoti.biscuit.commands.ClientCommand; +import com.fpghoti.biscuit.config.PropertiesRetrieval; import com.fpghoti.biscuit.global.SpamRecords; import com.fpghoti.biscuit.util.PermUtil; @@ -15,7 +16,7 @@ public class NotSpammerCommand extends ClientCommand{ public NotSpammerCommand() { name = "Not Spammer"; description = "Delists user as spammer."; - usage = "-notspammer @"; + usage = PropertiesRetrieval.getCommandSignifier() + "notspammer @"; minArgs = 1; maxArgs = 1; identifiers.add("notspammer"); diff --git a/src/main/java/com/fpghoti/biscuit/commands/client/NotifyCommand.java b/src/main/java/com/fpghoti/biscuit/commands/client/NotifyCommand.java index 5498bac..f2feb85 100644 --- a/src/main/java/com/fpghoti/biscuit/commands/client/NotifyCommand.java +++ b/src/main/java/com/fpghoti/biscuit/commands/client/NotifyCommand.java @@ -3,6 +3,7 @@ package com.fpghoti.biscuit.commands.client; import com.fpghoti.biscuit.Biscuit; import com.fpghoti.biscuit.api.API; import com.fpghoti.biscuit.commands.ClientCommand; +import com.fpghoti.biscuit.config.PropertiesRetrieval; import net.dv8tion.jda.api.entities.Emote; import net.dv8tion.jda.api.entities.Role; @@ -13,7 +14,7 @@ public class NotifyCommand extends ClientCommand{ public NotifyCommand() { name = "Notify"; description = "Puts user in Notify status."; - usage = "-notify"; + usage = PropertiesRetrieval.getCommandSignifier() + "notify"; minArgs = 0; maxArgs = 0; identifiers.add("notify"); @@ -26,7 +27,7 @@ public class NotifyCommand extends ClientCommand{ b.log(event.getAuthor().getName() + " issued a command: -notify"); Role role = null; for(Role r : event.getGuild().getRoles()) { - if(r.getName().toLowerCase().contains("don't notify")) { + if(r.getName().toLowerCase().contains(PropertiesRetrieval.getDontNotify())) { role = r; } } @@ -37,7 +38,7 @@ public class NotifyCommand extends ClientCommand{ Emote done = null; for(Emote e : event.getGuild().getEmotes()) { - if(e.getName().contains("ActionComplete")) { + if(e.getName().contains(PropertiesRetrieval.getDoneEmote())) { done = e; } } diff --git a/src/main/java/com/fpghoti/biscuit/commands/client/PingCommand.java b/src/main/java/com/fpghoti/biscuit/commands/client/PingCommand.java index 09889de..879a9e9 100644 --- a/src/main/java/com/fpghoti/biscuit/commands/client/PingCommand.java +++ b/src/main/java/com/fpghoti/biscuit/commands/client/PingCommand.java @@ -3,6 +3,7 @@ package com.fpghoti.biscuit.commands.client; import com.fpghoti.biscuit.Biscuit; import com.fpghoti.biscuit.api.API; import com.fpghoti.biscuit.commands.ClientCommand; +import com.fpghoti.biscuit.config.PropertiesRetrieval; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; @@ -11,7 +12,7 @@ public class PingCommand extends ClientCommand{ public PingCommand() { name = "Ping"; description = "Pings the bot."; - usage = "-ping"; + usage = PropertiesRetrieval.getCommandSignifier() + "ping"; minArgs = 0; maxArgs = 0; identifiers.add("ping"); diff --git a/src/main/java/com/fpghoti/biscuit/commands/client/PowerCommand.java b/src/main/java/com/fpghoti/biscuit/commands/client/PowerCommand.java new file mode 100644 index 0000000..8e4e5ce --- /dev/null +++ b/src/main/java/com/fpghoti/biscuit/commands/client/PowerCommand.java @@ -0,0 +1,39 @@ +package com.fpghoti.biscuit.commands.client; + +import com.fpghoti.biscuit.Biscuit; +import com.fpghoti.biscuit.api.API; +import com.fpghoti.biscuit.commands.ClientCommand; +import com.fpghoti.biscuit.config.PropertiesRetrieval; +import com.fpghoti.biscuit.util.Util; + +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; + +public class PowerCommand extends ClientCommand{ + + public PowerCommand() { + name = "Power"; + description = "Finds Num1 ^ Num2"; + usage = PropertiesRetrieval.getCommandSignifier() + "power "; + minArgs = 2; + maxArgs = 2; + identifiers.add("power"); + identifiers.add("pow"); + } + + @Override + public void execute(String[] args, MessageReceivedEvent event) { + Biscuit b = API.getBiscuit(); + b.log(event.getAuthor().getName() + " issued a command: -power"); + if(args[0] != null && Util.isDeciDigit(args[0]) && args[1] != null && Util.isDeciDigit(args[1])) { + double num = Double.parseDouble(args[0]); + double num2 = Double.parseDouble(args[1]); + String pow = Double.toString( Math.pow(num,num2)); + String end = pow.substring(Math.max(pow.length() - 2, 0)); + if(end.equals(".0")) { + pow = pow.replace(".0",""); + } + event.getTextChannel().sendMessage(args[0] + "^" + args[1] + " is **" + pow + "**.").queue(); + } + } + +} diff --git a/src/main/java/com/fpghoti/biscuit/commands/client/RecentSpammersCommand.java b/src/main/java/com/fpghoti/biscuit/commands/client/RecentSpammersCommand.java index f0ba055..d9100b8 100644 --- a/src/main/java/com/fpghoti/biscuit/commands/client/RecentSpammersCommand.java +++ b/src/main/java/com/fpghoti/biscuit/commands/client/RecentSpammersCommand.java @@ -3,6 +3,7 @@ package com.fpghoti.biscuit.commands.client; import com.fpghoti.biscuit.Biscuit; import com.fpghoti.biscuit.api.API; import com.fpghoti.biscuit.commands.ClientCommand; +import com.fpghoti.biscuit.config.PropertiesRetrieval; import com.fpghoti.biscuit.global.SpamRecords; import net.dv8tion.jda.api.entities.User; @@ -13,7 +14,7 @@ public class RecentSpammersCommand extends ClientCommand{ public RecentSpammersCommand() { name = "Recent Spammers"; description = "Retrieves a list of recent spammers."; - usage = "-recentspammers"; + usage = PropertiesRetrieval.getCommandSignifier() + "recentspammers"; minArgs = 0; maxArgs = 0; identifiers.add("recentspammers"); diff --git a/src/main/java/com/fpghoti/biscuit/commands/client/ShutDownCommand.java b/src/main/java/com/fpghoti/biscuit/commands/client/ShutDownCommand.java deleted file mode 100644 index 3302319..0000000 --- a/src/main/java/com/fpghoti/biscuit/commands/client/ShutDownCommand.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.fpghoti.biscuit.commands.client; - -import com.fpghoti.biscuit.Biscuit; -import com.fpghoti.biscuit.api.API; -import com.fpghoti.biscuit.commands.ClientCommand; -import com.fpghoti.biscuit.util.PermUtil; - -import net.dv8tion.jda.api.events.message.MessageReceivedEvent; - -public class ShutDownCommand extends ClientCommand{ - - public ShutDownCommand() { - name = "Shut Down"; - description = "Shuts down the bot."; - usage = "-shutdown"; - minArgs = 0; - maxArgs = 0; - identifiers.add("shutdown"); - } - - @Override - public void execute(String[] args, MessageReceivedEvent event) { - Biscuit b = API.getBiscuit(); - b.log(event.getAuthor().getName() + " issued a command: -shutdown"); - if(PermUtil.isAdmin(event.getMember())) { - - event.getTextChannel().sendMessage("Shutting down Mr. Bouncer...").queue(); - - } - } - -} diff --git a/src/main/java/com/fpghoti/biscuit/commands/client/SoftMuteCommand.java b/src/main/java/com/fpghoti/biscuit/commands/client/SoftMuteCommand.java index 2de877f..56343dd 100644 --- a/src/main/java/com/fpghoti/biscuit/commands/client/SoftMuteCommand.java +++ b/src/main/java/com/fpghoti/biscuit/commands/client/SoftMuteCommand.java @@ -3,6 +3,7 @@ package com.fpghoti.biscuit.commands.client; import com.fpghoti.biscuit.Biscuit; import com.fpghoti.biscuit.api.API; import com.fpghoti.biscuit.commands.ClientCommand; +import com.fpghoti.biscuit.config.PropertiesRetrieval; import com.fpghoti.biscuit.global.SpamRecords; import com.fpghoti.biscuit.util.PermUtil; @@ -15,7 +16,7 @@ public class SoftMuteCommand extends ClientCommand{ public SoftMuteCommand() { name = "Soft Mute"; description = "Soft mutes a user. In this state, they will only be able to send a message every two minutes."; - usage = "-softmute @"; + usage = PropertiesRetrieval.getCommandSignifier() + "softmute @"; minArgs = 1; maxArgs = 1; identifiers.add("softmute"); diff --git a/src/main/java/com/fpghoti/biscuit/commands/client/SquareRootCommand.java b/src/main/java/com/fpghoti/biscuit/commands/client/SquareRootCommand.java new file mode 100644 index 0000000..a0f3c91 --- /dev/null +++ b/src/main/java/com/fpghoti/biscuit/commands/client/SquareRootCommand.java @@ -0,0 +1,38 @@ +package com.fpghoti.biscuit.commands.client; + +import com.fpghoti.biscuit.Biscuit; +import com.fpghoti.biscuit.api.API; +import com.fpghoti.biscuit.commands.ClientCommand; +import com.fpghoti.biscuit.config.PropertiesRetrieval; +import com.fpghoti.biscuit.util.Util; + +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; + +public class SquareRootCommand extends ClientCommand{ + + public SquareRootCommand() { + name = "Square Root"; + description = "Finds square root."; + usage = PropertiesRetrieval.getCommandSignifier() + "squareroot "; + minArgs = 1; + maxArgs = 1; + identifiers.add("squareroot"); + identifiers.add("sqrt"); + } + + @Override + public void execute(String[] args, MessageReceivedEvent event) { + Biscuit b = API.getBiscuit(); + b.log(event.getAuthor().getName() + " issued a command: -squareroot"); + if(args[0] != null && Util.isDeciDigit(args[0])) { + double num = Double.parseDouble(args[0]); + String root = Double.toString(Math.sqrt(num)); + String end = root.substring(Math.max(root.length() - 2, 0)); + if(end.equals(".0")) { + root = root.replace(".0",""); + } + event.getTextChannel().sendMessage("The sqaure root of " + args[0] + " is **" + root + "**.").queue(); + } + } + +} diff --git a/src/main/java/com/fpghoti/biscuit/commands/client/SubtractCommand.java b/src/main/java/com/fpghoti/biscuit/commands/client/SubtractCommand.java new file mode 100644 index 0000000..e560dba --- /dev/null +++ b/src/main/java/com/fpghoti/biscuit/commands/client/SubtractCommand.java @@ -0,0 +1,39 @@ +package com.fpghoti.biscuit.commands.client; + +import com.fpghoti.biscuit.Biscuit; +import com.fpghoti.biscuit.api.API; +import com.fpghoti.biscuit.commands.ClientCommand; +import com.fpghoti.biscuit.config.PropertiesRetrieval; +import com.fpghoti.biscuit.util.Util; + +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; + +public class SubtractCommand extends ClientCommand{ + + public SubtractCommand() { + name = "Subtract"; + description = "Subtracts two numbers."; + usage = PropertiesRetrieval.getCommandSignifier() + "subtract "; + minArgs = 2; + maxArgs = 2; + identifiers.add("subtract"); + identifiers.add("sub"); + } + + @Override + public void execute(String[] args, MessageReceivedEvent event) { + Biscuit b = API.getBiscuit(); + b.log(event.getAuthor().getName() + " issued a command: -subtract"); + if(args[0] != null && Util.isDeciDigit(args[0]) && args[1] != null && Util.isDeciDigit(args[1])) { + double num = Double.parseDouble(args[0]); + double num2 = Double.parseDouble(args[1]); + String sub = Double.toString(num - num2); + String end = sub.substring(Math.max(sub.length() - 2, 0)); + if(end.equals(".0")) { + sub = sub.replace(".0",""); + } + event.getTextChannel().sendMessage(args[0] + " - " + args[1] + " is **" + sub + "**.").queue(); + } + } + +} diff --git a/src/main/java/com/fpghoti/biscuit/commands/client/UIDCommand.java b/src/main/java/com/fpghoti/biscuit/commands/client/UIDCommand.java index 00dff41..2cc2202 100644 --- a/src/main/java/com/fpghoti/biscuit/commands/client/UIDCommand.java +++ b/src/main/java/com/fpghoti/biscuit/commands/client/UIDCommand.java @@ -3,6 +3,7 @@ package com.fpghoti.biscuit.commands.client; import com.fpghoti.biscuit.Biscuit; import com.fpghoti.biscuit.api.API; import com.fpghoti.biscuit.commands.ClientCommand; +import com.fpghoti.biscuit.config.PropertiesRetrieval; import com.fpghoti.biscuit.util.PermUtil; import net.dv8tion.jda.api.entities.Member; @@ -14,7 +15,7 @@ public class UIDCommand extends ClientCommand{ public UIDCommand() { name = "User ID"; description = "Retrieves a user's ID."; - usage = "-uid @"; + usage = PropertiesRetrieval.getCommandSignifier() + "uid @"; minArgs = 1; maxArgs = 1; identifiers.add("uid"); diff --git a/src/main/java/com/fpghoti/biscuit/commands/client/UnSoftMuteCommand.java b/src/main/java/com/fpghoti/biscuit/commands/client/UnSoftMuteCommand.java index aed6c9a..6081820 100644 --- a/src/main/java/com/fpghoti/biscuit/commands/client/UnSoftMuteCommand.java +++ b/src/main/java/com/fpghoti/biscuit/commands/client/UnSoftMuteCommand.java @@ -3,6 +3,7 @@ package com.fpghoti.biscuit.commands.client; import com.fpghoti.biscuit.Biscuit; import com.fpghoti.biscuit.api.API; import com.fpghoti.biscuit.commands.ClientCommand; +import com.fpghoti.biscuit.config.PropertiesRetrieval; import com.fpghoti.biscuit.global.SpamRecords; import com.fpghoti.biscuit.util.PermUtil; @@ -15,7 +16,7 @@ public class UnSoftMuteCommand extends ClientCommand{ public UnSoftMuteCommand() { name = "Un Soft Mute"; description = "Removes a soft mute from a user."; - usage = "-unsoftmute @"; + usage = PropertiesRetrieval.getCommandSignifier() + "unsoftmute @"; minArgs = 1; maxArgs = 1; identifiers.add("unsoftmute"); diff --git a/src/main/java/com/fpghoti/biscuit/commands/console/SayCommand.java b/src/main/java/com/fpghoti/biscuit/commands/console/SayCommand.java index a8cbc5c..2173407 100644 --- a/src/main/java/com/fpghoti/biscuit/commands/console/SayCommand.java +++ b/src/main/java/com/fpghoti/biscuit/commands/console/SayCommand.java @@ -1,6 +1,7 @@ package com.fpghoti.biscuit.commands.console; import com.fpghoti.biscuit.Biscuit; +import com.fpghoti.biscuit.Main; import com.fpghoti.biscuit.api.API; import com.fpghoti.biscuit.commands.ConsoleCommand; @@ -9,14 +10,17 @@ import net.dv8tion.jda.api.entities.TextChannel; public class SayCommand extends ConsoleCommand{ - public SayCommand() { - name = "Say"; - description = "Makes bot send message on specified channel."; - usage = "say "; - minArgs = 2; - maxArgs = 2000; - identifiers.add("say"); - } + public SayCommand() { + name = "Say (Console)"; + description = "Makes bot send message on specified channel."; + usage = "[CONSOLE] say "; + minArgs = 2; + maxArgs = 2000; + if(!Main.isPlugin) { + identifiers.add("say"); + } + identifiers.add("bsay"); + } public void execute(String[] args) { Biscuit b = API.getBiscuit(); diff --git a/src/main/java/com/fpghoti/biscuit/commands/console/ShutdownConsoleCommand.java b/src/main/java/com/fpghoti/biscuit/commands/console/ShutdownConsoleCommand.java new file mode 100644 index 0000000..a2e630b --- /dev/null +++ b/src/main/java/com/fpghoti/biscuit/commands/console/ShutdownConsoleCommand.java @@ -0,0 +1,28 @@ +package com.fpghoti.biscuit.commands.console; + +import com.fpghoti.biscuit.Biscuit; +import com.fpghoti.biscuit.Main; +import com.fpghoti.biscuit.api.API; +import com.fpghoti.biscuit.commands.ConsoleCommand; + +public class ShutdownConsoleCommand extends ConsoleCommand{ + + public ShutdownConsoleCommand() { + name = "Shutdown (Console)"; + description = "Shuts down the bot."; + usage = "[CONSOLE] shutdown"; + minArgs = 0; + maxArgs = 0; + identifiers.add("shutdown"); + } + + public void execute(String[] args) { + Biscuit b = API.getBiscuit(); + if(args.length == 0) { + Main.shutdown(); + }else{ + b.log("INCORRECT USAGE! TRY: say "); + } + } + +} diff --git a/src/main/java/com/fpghoti/biscuit/config/ConfigRetrieval.java b/src/main/java/com/fpghoti/biscuit/config/ConfigRetrieval.java index 492a333..002ae6a 100644 --- a/src/main/java/com/fpghoti/biscuit/config/ConfigRetrieval.java +++ b/src/main/java/com/fpghoti/biscuit/config/ConfigRetrieval.java @@ -1,54 +1,41 @@ package com.fpghoti.biscuit.config; +import java.io.File; import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.Properties; import org.slf4j.Logger; import com.fpghoti.biscuit.Main; +import com.fpghoti.biscuit.PluginCore; public class ConfigRetrieval { static Logger log = Main.log; - public static void generateConfig(){ - Properties prop = new Properties(); - OutputStream output = null; - - try { - FileInputStream inputStream = - new FileInputStream("config.properties"); - inputStream.close(); - - } - catch(FileNotFoundException ex) { - log.info("config.ini missing...\nCreating file..."); - - try { - output = new FileOutputStream("config.properties"); - prop.setProperty("Bot-Token", ""); - prop.setProperty("AllowSpamPunish", "true"); - prop.store(output, null); - - } catch (IOException io) { - io.printStackTrace(); - } finally { - if (output != null) { - try { - output.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } + public static void generateConfig() { + File config; + if(Main.isPlugin) { + config = new File(PluginCore.plugin.getDataFolder(), "config.properties"); + if(!config.exists()) { + config.getParentFile().mkdir(); } + }else { + config = new File("config.properties"); } - catch(IOException ex) { - log.info("CANNOT READ CONFIG!"); + if (!config.exists()) { + try { + Path path = config.toPath(); + InputStream is = ConfigRetrieval.class.getClassLoader().getResourceAsStream("config.properties"); + Files.copy( is, path); + is.close(); + } catch (Exception e) { + e.printStackTrace(); + } } } @@ -57,9 +44,17 @@ public class ConfigRetrieval { Properties prop = new Properties(); InputStream input = null; + + File config; + + if(Main.isPlugin) { + config = new File(PluginCore.plugin.getDataFolder(), "config.properties"); + }else { + config = new File("config.properties"); + } try { - input = new FileInputStream("config.properties"); + input = new FileInputStream(config); prop.load(input); setting = prop.getProperty(property); } catch (IOException ex) { diff --git a/src/main/java/com/fpghoti/biscuit/config/PropertiesRetrieval.java b/src/main/java/com/fpghoti/biscuit/config/PropertiesRetrieval.java index e414387..4adc677 100644 --- a/src/main/java/com/fpghoti/biscuit/config/PropertiesRetrieval.java +++ b/src/main/java/com/fpghoti/biscuit/config/PropertiesRetrieval.java @@ -1,60 +1,54 @@ package com.fpghoti.biscuit.config; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.Properties; +import com.fpghoti.biscuit.util.Util; public class PropertiesRetrieval { public static String getToken(){ - String token = ""; - - Properties prop = new Properties(); - InputStream input = null; - - try { - input = new FileInputStream("config.properties"); - prop.load(input); - token = prop.getProperty("Bot-Token"); - } catch (IOException ex) { - ex.printStackTrace(); - } finally { - if (input != null) { - try { - input.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } - - return token; + return ConfigRetrieval.getFromConfig("Bot-Token"); } public static String getCommandSignifier(){ - String signifier = ""; - - Properties prop = new Properties(); - InputStream input = null; - - try { - input = new FileInputStream("config.properties"); - prop.load(input); - signifier = signifier + prop.getProperty("Command-Signifier").charAt(0); - } catch (IOException ex) { - ex.printStackTrace(); - } finally { - if (input != null) { - try { - input.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } + return ConfigRetrieval.getFromConfig("Command-Signifier"); + } + + public static String getDoneEmote(){ + return ConfigRetrieval.getFromConfig("Done-Emote"); + } + + public static String getDontNotify(){ + return ConfigRetrieval.getFromConfig("Dont-Notify-Role"); + } + + public static boolean captchaEnabled(){ + String value = ConfigRetrieval.getFromConfig("Captcha"); + return value.equalsIgnoreCase("true"); + } + + public static String getCaptchaReward(){ + return ConfigRetrieval.getFromConfig("Captcha-Reward-Role"); + } + + public static String getDefaultRole(){ + return ConfigRetrieval.getFromConfig("DefaultRoleName"); + } + + public static boolean noCaptchaKick(){ + String value = ConfigRetrieval.getFromConfig("No-Captcha-Kick"); + return value.equalsIgnoreCase("true"); + } + + public static Integer noCaptchaKickTime(){ + String value = ConfigRetrieval.getFromConfig("No-Captcha-Kick-Time"); + if(!Util.isDigit(value)) { + return 0; } - - return signifier; + return Integer.parseInt(value); + } + + public static boolean logChat(){ + String value = ConfigRetrieval.getFromConfig("ChatLog"); + return value.equalsIgnoreCase("true"); } } diff --git a/src/main/java/com/fpghoti/biscuit/listener/DMListener.java b/src/main/java/com/fpghoti/biscuit/listener/DMListener.java new file mode 100644 index 0000000..3718ca1 --- /dev/null +++ b/src/main/java/com/fpghoti/biscuit/listener/DMListener.java @@ -0,0 +1,128 @@ +package com.fpghoti.biscuit.listener; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; + +import org.slf4j.Logger; + +import com.fpghoti.biscuit.Main; +import com.fpghoti.biscuit.PluginCore; +import com.fpghoti.biscuit.config.PropertiesRetrieval; +import com.fpghoti.biscuit.user.PreUser; +import com.github.cage.Cage; + +import net.dv8tion.jda.api.entities.ChannelType; +import net.dv8tion.jda.api.entities.Guild; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.PrivateChannel; +import net.dv8tion.jda.api.entities.Role; +import net.dv8tion.jda.api.entities.User; +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +import net.dv8tion.jda.api.hooks.ListenerAdapter; + +public class DMListener extends ListenerAdapter{ + + Logger log = Main.log; + + @Override + public void onMessageReceived(MessageReceivedEvent event){ + if (event.isFromType(ChannelType.PRIVATE) && !event.getAuthor().isBot()) { + if(PropertiesRetrieval.logChat()) { + log.info( "NEW PRIVATE MESSAGE - MSGID: " + event.getMessageId() + "- @" + event.getAuthor().getName() + " " + event.getAuthor().getAsMention() + " - " + event.getMessage().getContentDisplay()); + } + handleCaptcha(event); + } + } + + private void handleCaptcha(MessageReceivedEvent event) { + PrivateChannel channel = event.getPrivateChannel(); + User author = event.getAuthor(); + if(PreUser.getPreUser(author) != null) { + PreUser preu = PreUser.getPreUser(author); + if(preu.getToken() == null || !event.getMessage().getContentDisplay().equals(preu.getToken())) { + + if(preu.getToken() != null) { + channel.sendMessage("Sorry! That's not quite right! Please try again.").queue(); + } + Main.log.info("Generating captcha challenge for user " + author.getName() + " " + author.getAsMention() + "..."); + + Cage cage = Main.getBiscuit().getCage(); + + preu.genToken(); + + OutputStream os; + try { + //os = new FileOutputStream("captcha/" + author.getId() + ".jpg", false); + if(!Main.isPlugin) { + os = new FileOutputStream("captcha/" + author.getId() + ".jpg", false); + }else { + File c = new File(PluginCore.plugin.getDataFolder(), "captcha/" + author.getId() + ".jpg"); + os = new FileOutputStream(c, false); + } + } catch (FileNotFoundException e) { + e.printStackTrace(); + return; + } + try { + cage.draw(preu.getToken() , os); + } catch (IOException e) { + e.printStackTrace(); + return; + } finally { + try { + os.close(); + } catch (IOException e) { + e.printStackTrace(); + return; + } + } + File captcha; + if(!Main.isPlugin) { + captcha = new File("captcha/" + author.getId() + ".jpg"); + }else { + captcha = new File(PluginCore.plugin.getDataFolder(), "captcha/" + author.getId() + ".jpg"); + } + channel.sendMessage("Respond with the exact text in this image (case-sensitive)").queue(); + channel.sendFile(captcha).queue(); + + }else { + preu.setDone(); + Main.log.info(author.getName() + " successfully completed a captcha challenge. Granting role."); + + Role newrole = null; + Role defaultrole = null; + + for(Guild g : preu.getGuilds()) { + for(Role r : g.getRoles()) { + if(r.getName().toLowerCase().contains(PropertiesRetrieval.getCaptchaReward().toLowerCase())) { + newrole = r; + }else if(r.getName().toLowerCase().contains(PropertiesRetrieval.getDefaultRole().toLowerCase())) { + defaultrole = r; + } + } + if(newrole == null) { + Main.log.error("Cannot find captcha reward role!"); + return; + } + + if(defaultrole == null) { + Main.log.error("Cannot find captcha default role!"); + return; + } + + Member member = g.getMemberById(author.getId()); + + g.addRoleToMember(member, newrole).queue(); + g.removeRoleFromMember(member, defaultrole).queue(); + preu.remove(); + } + channel.sendMessage("Well done, " + author.getAsMention() + "!").queue(); + } + + } + } + +} diff --git a/src/main/java/com/fpghoti/biscuit/listener/JoinListener.java b/src/main/java/com/fpghoti/biscuit/listener/JoinListener.java index a191279..c5cbce5 100644 --- a/src/main/java/com/fpghoti/biscuit/listener/JoinListener.java +++ b/src/main/java/com/fpghoti/biscuit/listener/JoinListener.java @@ -3,9 +3,12 @@ package com.fpghoti.biscuit.listener; import org.slf4j.Logger; import com.fpghoti.biscuit.Main; +import com.fpghoti.biscuit.config.PropertiesRetrieval; import com.fpghoti.biscuit.global.Properties; +import com.fpghoti.biscuit.user.PreUser; import net.dv8tion.jda.api.entities.Role; +import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; @@ -15,7 +18,8 @@ public class JoinListener extends ListenerAdapter { @Override public void onGuildMemberJoin(GuildMemberJoinEvent event) { - log.info("MEMBER JOINED: " + event.getMember().getUser().getName()); + User user = event.getMember().getUser(); + log.info("MEMBER JOINED: " + user.getName() + " " + user.getAsMention()); if(Properties.customdefaultrole) { if(!event.getMember().getUser().isBot()) { log.info("Issuing a role.."); @@ -30,6 +34,10 @@ public class JoinListener extends ListenerAdapter { return; } + if(PropertiesRetrieval.captchaEnabled()) { + new PreUser(event.getMember().getUser()); + } + event.getGuild().addRoleToMember(event.getMember(), role).queue(); } diff --git a/src/main/java/com/fpghoti/biscuit/listener/MessageReceiveListener.java b/src/main/java/com/fpghoti/biscuit/listener/MessageReceiveListener.java index 13eefe5..ea81d91 100644 --- a/src/main/java/com/fpghoti/biscuit/listener/MessageReceiveListener.java +++ b/src/main/java/com/fpghoti/biscuit/listener/MessageReceiveListener.java @@ -4,12 +4,14 @@ import org.slf4j.Logger; import com.fpghoti.biscuit.Main; import com.fpghoti.biscuit.config.ConfigRetrieval; +import com.fpghoti.biscuit.config.PropertiesRetrieval; import com.fpghoti.biscuit.global.MessageQueue; import com.fpghoti.biscuit.global.SpamRecords; import com.fpghoti.biscuit.util.ChatFilter; import com.fpghoti.biscuit.util.Util; import net.dv8tion.jda.api.Permission; +import net.dv8tion.jda.api.entities.ChannelType; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.exceptions.PermissionException; import net.dv8tion.jda.api.hooks.ListenerAdapter; @@ -18,96 +20,104 @@ public class MessageReceiveListener extends ListenerAdapter{ Logger log = Main.log; + + /*TODO Cleanup*/ @Override public void onMessageReceived(MessageReceivedEvent event){ - - if(Util.isLoggable(event.getTextChannel())) { - log.info( "NEW MSG - MSGID: " + event.getMessageId() + "- @" + event.getAuthor().getName() + " " + event.getAuthor().getAsMention() + " - CHANNEL: #" + event.getChannel().getName() + " - " + event.getMessage().getContentDisplay()); - } - - if(event.getAuthor().isBot() && event.getMessage().getContentRaw().contains("This message contains words not appropriate for this channel.") || (ChatFilter.isNaughty(event.getMessage().getContentDisplay()))){ - MessageQueue.removemessages.put(event.getMessage().getId(), event.getTextChannel()); - } - - //staff channels do not need filtering, as the filter could actually be a hinderance - if(!event.getChannel().getName().toLowerCase().contains("staff") && ChatFilter.isNaughty(event.getMessage().getContentDisplay())){ - String text = event.getMessage().getContentDisplay(); - log.info("Removed Msg - REASON NAUGHTY WORD(S) - by " + event.getAuthor().getName() + ": " + text); - event.getTextChannel().sendMessage(event.getAuthor().getAsMention() + " This message contains words not appropriate for this channel.").complete(); - MessageQueue.fastremovemessages.put(event.getMessage().getId(), event.getTextChannel()); - } - - if(!event.getAuthor().isBot() && !MessageQueue.chatssent.containsKey(event.getAuthor())){ - MessageQueue.chatssent.put(event.getAuthor(), 0); - } - - if(!event.getAuthor().isBot() && !MessageQueue.spammsgs.containsKey(event.getAuthor())){ - MessageQueue.spammsgs.put(event.getAuthor(), 0); - } - - if(!MessageQueue.chatssent10s.containsKey(event.getAuthor()) && SpamRecords.spammers.contains(event.getAuthor())){ - MessageQueue.chatssent10s.put(event.getAuthor(), 0); - } - - if(!MessageQueue.chatssent2m.containsKey(event.getAuthor()) && SpamRecords.softmute.contains(event.getAuthor())){ - MessageQueue.chatssent2m.put(event.getAuthor(), 0); - } - - if(!event.getAuthor().isBot()){ - MessageQueue.spammsgs.put(event.getAuthor(), MessageQueue.spammsgs.get(event.getAuthor()) + 1); - MessageQueue.chatssent.put(event.getAuthor(), MessageQueue.chatssent.get(event.getAuthor()) + 1); - if(SpamRecords.softmute.contains(event.getAuthor())){ - MessageQueue.chatssent2m.put(event.getAuthor(), MessageQueue.chatssent2m.get(event.getAuthor()) + 1); + if (event.isFromType(ChannelType.TEXT)) { + if(Util.isLoggable(event.getTextChannel())) { + if(PropertiesRetrieval.logChat()) { + log.info( "NEW MSG - MSGID: " + event.getMessageId() + "- @" + event.getAuthor().getName() + " " + event.getAuthor().getAsMention() + " - CHANNEL: #" + event.getChannel().getName() + " - " + event.getMessage().getContentDisplay()); + } } - if(SpamRecords.spammers.contains(event.getAuthor())){ - MessageQueue.chatssent10s.put(event.getAuthor(), MessageQueue.chatssent10s.get(event.getAuthor()) + 1); + + if(event.getAuthor().isBot() && event.getMessage().getContentRaw().contains("This message contains words not appropriate for this channel.") || (ChatFilter.isNaughty(event.getMessage().getContentDisplay()))){ + MessageQueue.removemessages.put(event.getMessage().getId(), event.getTextChannel()); } - } - if(SpamRecords.softmute.contains(event.getAuthor()) && MessageQueue.chatssent2m.get(event.getAuthor()) > 1){ - String text = event.getMessage().getContentDisplay(); - log.info("Removed Msg - REASON SOFTMUTED - by " + event.getAuthor().getName() + ": " + text); - MessageQueue.fastremovemessages.put(event.getMessage().getId(), event.getTextChannel()); - } - if(SpamRecords.spammers.contains(event.getAuthor())){ - if(MessageQueue.chatssent10s.get(event.getAuthor()) > 1){ + + //staff channels do not need filtering, as the filter could actually be a hinderance + if(!event.getChannel().getName().toLowerCase().contains("staff") && ChatFilter.isNaughty(event.getMessage().getContentDisplay())){ String text = event.getMessage().getContentDisplay(); - log.info("Removed Msg - REASON FLAGGED AS SPAM - by " + event.getAuthor().getName() + ": " + text); + log.info("Removed Msg - REASON NAUGHTY WORD(S) - by " + event.getAuthor().getName() + ": " + text); + event.getTextChannel().sendMessage(event.getAuthor().getAsMention() + " This message contains words not appropriate for this channel.").complete(); MessageQueue.fastremovemessages.put(event.getMessage().getId(), event.getTextChannel()); } - } - if(ConfigRetrieval.getFromConfig("AllowSpamPunish").equalsIgnoreCase("true")){ - if(!event.getAuthor().isBot() && !SpamRecords.softmute.contains(event.getAuthor()) && !MessageQueue.chatssent.isEmpty() && MessageQueue.chatssent.get(event.getAuthor()) > 7){ - if(!SpamRecords.spammers.contains(event.getAuthor())){ - if(event.getGuild().getSelfMember().hasPermission(Permission.NICKNAME_MANAGE)){ - try{ - //ignores music channels so that music bots can operate normally - if(!event.getChannel().getName().toLowerCase().contains("music")){ - if(SpamRecords.warnedspm.contains(event.getAuthor())){ - MessageQueue.removemessages.put(event.getMessage().getId(), event.getTextChannel()); - SpamRecords.spammers.add(event.getAuthor()); - event.getTextChannel().sendMessage("*Flagging " + event.getAuthor().getAsMention() + " as spam!*").queue(); - log.info("User " + event.getAuthor().getName() + " has been flagged as spam!"); - }else{ - MessageQueue.chatssent.remove(event.getAuthor()); - SpamRecords.warnedspm.add(event.getAuthor()); - event.getTextChannel().sendMessage("**STOP spamming, " + event.getAuthor().getAsMention() + "! You have been warned!**").queue(); - log.info("User " + event.getAuthor().getName() + " has been warned for spam!"); - } - } - }catch(PermissionException e){ - log.info("Bot does not have permission to change the nick name of " + event.getAuthor().getName() + "!"); - } - } + if(!event.getAuthor().isBot() && !MessageQueue.chatssent.containsKey(event.getAuthor())){ + MessageQueue.chatssent.put(event.getAuthor(), 0); + } + + if(!event.getAuthor().isBot() && !MessageQueue.spammsgs.containsKey(event.getAuthor())){ + MessageQueue.spammsgs.put(event.getAuthor(), 0); + } + + if(!MessageQueue.chatssent10s.containsKey(event.getAuthor()) && SpamRecords.spammers.contains(event.getAuthor())){ + MessageQueue.chatssent10s.put(event.getAuthor(), 0); + } + + if(!MessageQueue.chatssent2m.containsKey(event.getAuthor()) && SpamRecords.softmute.contains(event.getAuthor())){ + MessageQueue.chatssent2m.put(event.getAuthor(), 0); + } + + if(!event.getAuthor().isBot()){ + MessageQueue.spammsgs.put(event.getAuthor(), MessageQueue.spammsgs.get(event.getAuthor()) + 1); + MessageQueue.chatssent.put(event.getAuthor(), MessageQueue.chatssent.get(event.getAuthor()) + 1); + if(SpamRecords.softmute.contains(event.getAuthor())){ + MessageQueue.chatssent2m.put(event.getAuthor(), MessageQueue.chatssent2m.get(event.getAuthor()) + 1); } - - if(event.getGuild().getSelfMember().hasPermission(Permission.MESSAGE_MANAGE)){ + if(SpamRecords.spammers.contains(event.getAuthor())){ + MessageQueue.chatssent10s.put(event.getAuthor(), MessageQueue.chatssent10s.get(event.getAuthor()) + 1); + } + } + if(SpamRecords.softmute.contains(event.getAuthor()) && MessageQueue.chatssent2m.get(event.getAuthor()) > 1){ + String text = event.getMessage().getContentDisplay(); + log.info("Removed Msg - REASON SOFTMUTED - by " + event.getAuthor().getName() + ": " + text); + MessageQueue.fastremovemessages.put(event.getMessage().getId(), event.getTextChannel()); + } + if(SpamRecords.spammers.contains(event.getAuthor())){ + if(MessageQueue.chatssent10s.get(event.getAuthor()) > 1){ + String text = event.getMessage().getContentDisplay(); + log.info("Removed Msg - REASON FLAGGED AS SPAM - by " + event.getAuthor().getName() + ": " + text); MessageQueue.fastremovemessages.put(event.getMessage().getId(), event.getTextChannel()); } } - } - + ////// Listen for spammers + + if(ConfigRetrieval.getFromConfig("AllowSpamPunish").equalsIgnoreCase("true")){ + if(!event.getAuthor().isBot() && !SpamRecords.softmute.contains(event.getAuthor()) && !MessageQueue.chatssent.isEmpty() && MessageQueue.chatssent.get(event.getAuthor()) > 7){ + if(!SpamRecords.spammers.contains(event.getAuthor())){ + if(event.getGuild().getSelfMember().hasPermission(Permission.NICKNAME_MANAGE)){ + try{ + //ignores music channels so that music bots can operate normally + if(!event.getChannel().getName().toLowerCase().contains("music")){ + if(SpamRecords.warnedspm.contains(event.getAuthor())){ + MessageQueue.removemessages.put(event.getMessage().getId(), event.getTextChannel()); + SpamRecords.spammers.add(event.getAuthor()); + event.getTextChannel().sendMessage("*Flagging " + event.getAuthor().getAsMention() + " as spam!*").queue(); + log.info("User " + event.getAuthor().getName() + " has been flagged as spam!"); + }else{ + MessageQueue.chatssent.remove(event.getAuthor()); + SpamRecords.warnedspm.add(event.getAuthor()); + event.getTextChannel().sendMessage("**STOP spamming, " + event.getAuthor().getAsMention() + "! You have been warned!**").queue(); + log.info("User " + event.getAuthor().getName() + " has been warned for spam!"); + } + } + }catch(PermissionException e){ + log.info("Bot does not have permission to change the nick name of " + event.getAuthor().getName() + "!"); + } + } + } + + if(event.getGuild().getSelfMember().hasPermission(Permission.MESSAGE_MANAGE)){ + MessageQueue.fastremovemessages.put(event.getMessage().getId(), event.getTextChannel()); + } + } + } + + ///// + + } } } diff --git a/src/main/java/com/fpghoti/biscuit/timer/task/DecrementTimer.java b/src/main/java/com/fpghoti/biscuit/timer/task/DecrementTimer.java new file mode 100644 index 0000000..436d91e --- /dev/null +++ b/src/main/java/com/fpghoti/biscuit/timer/task/DecrementTimer.java @@ -0,0 +1,22 @@ +package com.fpghoti.biscuit.timer.task; + +import com.fpghoti.biscuit.Main; +import com.fpghoti.biscuit.timer.BiscuitTimer; +import com.fpghoti.biscuit.user.PreUser; + +public class DecrementTimer extends BiscuitTimer{ + + public DecrementTimer(){ + delay = (long) 0; + period = (long) 60*1000; + } + + public void run() { + if(Main.ready) { + for(PreUser p : PreUser.users) { + p.decrementTime(); + } + } + } + +} diff --git a/src/main/java/com/fpghoti/biscuit/user/PreUser.java b/src/main/java/com/fpghoti/biscuit/user/PreUser.java new file mode 100644 index 0000000..8055698 --- /dev/null +++ b/src/main/java/com/fpghoti/biscuit/user/PreUser.java @@ -0,0 +1,120 @@ +package com.fpghoti.biscuit.user; + +import java.io.File; +import java.util.ArrayList; +import java.util.concurrent.CopyOnWriteArrayList; + +import com.fpghoti.biscuit.Main; +import com.fpghoti.biscuit.PluginCore; +import com.fpghoti.biscuit.config.PropertiesRetrieval; +import com.fpghoti.biscuit.util.Util; + +import net.dv8tion.jda.api.JDA; +import net.dv8tion.jda.api.entities.Guild; +import net.dv8tion.jda.api.entities.User; + +public class PreUser { + + public static CopyOnWriteArrayList users = new CopyOnWriteArrayList(); + + public static PreUser getPreUser(User user) { + for(PreUser u : users) { + if(u.getUser().getId().equals(user.getId())) { + return u; + } + } + return null; + } + + public static boolean preUserExists(User user) { + return getPreUser(user) != null; + } + + private User user; + private String token; + private int timeLeft; + private boolean done; + + public PreUser(User user) { + this.user = user; + this.token = null; + this.done = false; + this.timeLeft = PropertiesRetrieval.noCaptchaKickTime() + 1; + users.add(this); + } + + public User getUser() { + return this.user; + } + + public String getToken() { + return this.token; + } + + //Going to use custom string gen for more char types in captcha + public void genToken() { + //token = Main.getBiscuit().getCage().getTokenGenerator().next(); + token = Util.randomString(6); + } + + public void setDone() { + this.done = true; + } + + public void decrementTime() { + if(!shareGuild()) { + remove(); + return; + } + + if(!done) { + if(PropertiesRetrieval.noCaptchaKick()) { + timeLeft = timeLeft - 1; + if(timeLeft <= 0) { + for(Guild g : getGuilds()) { + Main.log.info(user.getName() + " " + user.getAsMention() + " waited too long to complete the captcha! Kicking..."); + g.kick(user.getId()).queue(); + remove(); + } + } + } + } + + } + + public boolean shareGuild() { + JDA jda = Main.getBiscuit().getJDA(); + for(Guild g : jda.getGuilds()) { + if(g.isMember(user)){ + return true; + } + } + return false; + } + + public ArrayList getGuilds(){ + ArrayList guilds = new ArrayList(); + JDA jda = Main.getBiscuit().getJDA(); + for(Guild g : jda.getGuilds()) { + if(g.isMember(user)){ + guilds.add(g); + } + } + return guilds; + } + + public void remove() { + setDone(); + Main.log.info("Removing captcha data for user " + user.getName() + " " + user.getAsMention()); + File captcha; + if(!Main.isPlugin) { + captcha = new File("captcha/" + user.getId() + ".jpg"); + }else { + captcha = new File(PluginCore.plugin.getDataFolder(), "captcha/" + user.getId() + ".jpg"); + } + token = null; + captcha.delete(); + users.remove(this); + } + +} diff --git a/src/main/java/com/fpghoti/biscuit/util/Util.java b/src/main/java/com/fpghoti/biscuit/util/Util.java index b1937d4..a1b609b 100644 --- a/src/main/java/com/fpghoti/biscuit/util/Util.java +++ b/src/main/java/com/fpghoti/biscuit/util/Util.java @@ -28,4 +28,36 @@ public class Util { return a; } + public static boolean isDigit(String s){ + return s.matches("[0-9]+"); + } + + public static boolean isDeciDigit(String s){ + int i = 0; + String s2 = ""; + for(Character c : s.toCharArray()) { + if(!(i == 0 && c == '-')) { + s2 = s2 + c; + } + i++; + } + return s2.replace(".", "").matches("[0-9]+"); + } + + private static String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; + + public static String randomString(int size) { + Random rand = new Random(); + if (size <= 0) { + return ""; + } + String str = ""; + for (int i = 0; i < size; i++) { + int ind = rand.nextInt(chars.length()); + char rchar = chars.charAt(ind); + str = str + rchar; + } + return str; + } + } diff --git a/src/main/resources/config.properties b/src/main/resources/config.properties new file mode 100644 index 0000000..9ec533b --- /dev/null +++ b/src/main/resources/config.properties @@ -0,0 +1,44 @@ +#Change this to redefine the main command signifier character/String +Command-Signifier:- + +#Set to false to disable chat logging +ChatLog:true + +#Automatically places spammers in spam mode +AllowSpamPunish:true + +Channels-To-Not-Chatlog:ignore_me + +#Bot token provided by Discord +Bot-Token: + +#Normal channels will have messages with these words deleted by the bot +NaughtyList:piff + +#Bot can automatically assign role upon user join +UseCustomDefaultRole:true + +#Does nothing if UseCustomDefaultRole is off +DefaultRoleName:Standard + +#Emote to react with when action is complete +Done-Emote:ActionComplete + +#Role to disable notifications +Dont-Notify-Role:dont_notify + +#Set true to enable captcha +#Use with UseCustomDefaultRole enabled +#Will require user to have that role for captcha check +#Role will be replaced with reward role on clear +Captcha=false + +#Reward this role when captcha cleared +Captcha-Reward-Role:cleared + +#Kick if Captcha is not completed in a timely manner +No-Captcha-Kick:false + +#Kick user after this number of minutes has passed without captcha clear +#Due to the way the countdown works, the kick may not happen until at most one minute after +No-Captcha-Kick-Time:10 \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..1ca8c28 --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,9 @@ +name: Biscuit +author: Ghoti_Mayo +version: ${project.version} +main: com.fpghoti.biscuit.PluginCore +description: Discord bot. Also runs as a Spigot plugin. +commands: + bsay: + description: Send a message to a channel as a bot. Perm biscuit.admin + usage: /bsay \ No newline at end of file