final commit for tonight aaaaaaaaaaaaaaaaaaaaa

This commit is contained in:
deepCurse 2021-12-02 00:38:10 -04:00
parent 5bedad9a0f
commit 92842e12bf
10 changed files with 334 additions and 45 deletions

View file

@ -1,13 +1,12 @@
package pkg.deepCurse.nopalmo.command;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import pkg.deepCurse.nopalmo.manager.CommandBlob;
import pkg.deepCurse.nopalmo.manager.GuildCommandBlob;
import pkg.deepCurse.nopalmo.manager.GuildCommandManager;
public abstract class GuildCommand {
public abstract void run(CommandBlob blob, GuildMessageReceivedEvent guildMessage, GuildCommandManager commandManager) throws Exception;
public abstract void run(GuildCommandBlob blob, GuildCommandManager commandManager) throws Exception;
public abstract String[] getCommandCalls();

View file

@ -1,19 +1,53 @@
package pkg.deepCurse.nopalmo.command.guildCommand;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import pkg.deepCurse.nopalmo.command.GuildCommand;
import pkg.deepCurse.nopalmo.database.DatabaseTools;
import pkg.deepCurse.nopalmo.manager.CommandBlob;
import pkg.deepCurse.nopalmo.core.Boot;
import pkg.deepCurse.nopalmo.global.Tools;
import pkg.deepCurse.nopalmo.manager.GuildCommandBlob;
import pkg.deepCurse.nopalmo.manager.GuildCommandManager;
public class Ping extends GuildCommand {
@Override
public void run(CommandBlob blob, GuildMessageReceivedEvent guildMessage, GuildCommandManager commandManager)
public void run(GuildCommandBlob blob, GuildCommandManager commandManager)
throws Exception {
DatabaseTools.Tools.Guild.Prefix.createPrefix(guildMessage.getGuild().getIdLong(), blob.getArgs().get(0));
GuildMessageReceivedEvent event = blob.getGuildMessageEvent();
TextChannel channel = event.getChannel();
channel.sendMessage("You are: " + blob.getUserID()).queue();
if (blob.getArgs().size() == 0) {
// new Main();
channel.sendMessage("Pong!\n" + event.getJDA().getGatewayPing() + "ms\n"
// + "Sorry if the ping is too high, im currently hosting on an under powered
// laptop out in the countryside...\n"
// + "This will be fixed in at most 2 days..."
).queue();
// long pang = Main.bot.getGatewayPing();
} else if (blob.getArgs().get(0).contentEquals("all")) {
channel.sendMessage("Gathering data...").queue(msg -> {
try {
long timeToProcess = System.currentTimeMillis();
String out = "Pong!\n" + "Google: " + services.UptimePing.sendPing("www.google.com") + "ms\n"
+ "JDA Gateway: " + event.getJDA().getGatewayPing() + "ms\n" + "www.discord.com: "
+ services.UptimePing.sendPing("www.discord.com") + "ms";
msg.editMessage(out + "\nTime to process: " + (timeToProcess - System.currentTimeMillis()) + "ms").queue();
} catch (Exception e) {
}
});
} else
Tools.wrongUsage(channel, this);
}
@Override
@ -25,4 +59,5 @@ public class Ping extends GuildCommand {
public HelpPage getHelpPage() {
return HelpPage.Info;
}
}

View file

@ -1,5 +1,12 @@
package pkg.deepCurse.nopalmo.global;
import net.dv8tion.jda.api.entities.TextChannel;
import pkg.deepCurse.nopalmo.command.GuildCommand;
public class Tools {
public static void wrongUsage(TextChannel tc, GuildCommand c) {
tc.sendMessage("Wrong Command Usage!\n" + c.getCommandName()).queue();
}
}

View file

@ -2,13 +2,29 @@ package pkg.deepCurse.nopalmo.manager;
import java.util.ArrayList;
public class CommandBlob {
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.events.Event;
public abstract class CommandBlob {
private String modifiedRaw = null;
private String modified = null;
private ArrayList<String> args = null;
protected long userID = 0;
Event event = null;
@Deprecated
public CommandBlob(Event event) {
this.event = event;
}
public Event getEvent() {
return event;
}
public String getModifiedMessageContents() {
return this.modified;
}
@ -20,5 +36,21 @@ public class CommandBlob {
public ArrayList<String> getArgs() {
return args;
}
public void setUser(long userID) {
this.userID = userID;
}
public long getUserID() {
return this.userID;
}
public void setJDA(JDA bot) {
}
public void setArgs(ArrayList<String> newArguments) {
this.args = newArguments;
}
}

View file

@ -0,0 +1,17 @@
package pkg.deepCurse.nopalmo.manager;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
public class GuildCommandBlob extends CommandBlob {
@SuppressWarnings("deprecation")
public GuildCommandBlob(GuildMessageReceivedEvent event) {
super(event);
setUser(event.getMessage().getAuthor().getIdLong());
}
public GuildMessageReceivedEvent getGuildMessageEvent() {
return (GuildMessageReceivedEvent) this.event;
}
}

View file

@ -3,8 +3,10 @@ package pkg.deepCurse.nopalmo.manager;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
@ -19,7 +21,7 @@ import pkg.deepCurse.nopalmo.database.DatabaseTools;
public class GuildCommandManager {
private final Map<String, GuildCommand> guildCommandMap = new HashMap<>();
Executor executor = null;
private static Executor executor = null;
public GuildCommandManager() {
init();
@ -58,42 +60,80 @@ public class GuildCommandManager {
.split("\\s+");
final String command = split[0].toLowerCase();
executor.execute(() -> {
long commandStartTime = System.currentTimeMillis();
if (guildCommandMap.containsKey(command)) {
final List<String> args = Arrays.asList(split).subList(1, split.length);
try {
ArrayList<String> newArguments = new ArrayList<String>();
ArrayList<String[]> extractedFlags = new ArrayList<String[]>();
} catch (Exception e) {
if (Boot.isProd) {
guildMessage.getChannel().sendMessage("```\n" + e + "```").queue();
} else {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
guildMessage.getChannel().sendMessage("```\n" + sw.toString() + "```").queue();
System.err.println("Exception caught in: " + e.toString());
e.printStackTrace();
}
} catch (Throwable t) {
executor.execute(() -> {
long commandStartTime = System.currentTimeMillis();
try {
ArrayList<String> newArguments = new ArrayList<String>();
// ArrayList<String> commandFlags = new ArrayList<String>();
// ArrayList<String[]> extractedFlags = new ArrayList<String[]>(); // not needed currently, remnant idea of bash-ish
GuildCommandBlob commandBlob = new GuildCommandBlob(guildMessage);
boolean printTime = false;
byte argSkipCount = 0;
for (String x : args) {
switch (x) {
case "\\time":
printTime = true;
break;
case "\\perm":
// commandFlags.add("user:380045419381784576");
commandBlob.setUser(380045419381784576L);
break;
case "\\del":
guildMessage.getMessage().delete().queue();
break;
default:
if (argSkipCount<=0) {
newArguments.add(x);
}
}
}
commandBlob.setArgs(newArguments);
guildCommandMap.get(command).run(commandBlob, this);
if (printTime) {
guildMessage.getChannel()
.sendMessage("Time to run: " + (commandStartTime - System.currentTimeMillis()) + "ms")
.queue();
}
} catch (Exception e) {
if (Boot.isProd) {
guildMessage.getChannel().sendMessage("```\n" + e + "```").queue();
} else {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
guildMessage.getChannel().sendMessage("```\n" + sw.toString() + "```").queue();
System.err.println("Exception caught in: " + e.toString());
e.printStackTrace();
}
} catch (Throwable t) {
if (Boot.isProd) {
guildMessage.getChannel().sendMessage("```\n" + t + "```").queue();
} else {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
guildMessage.getChannel().sendMessage("```\n" + sw.toString() + "```").queue();
System.err.println("Error caught in: " + t.toString());
t.printStackTrace();
}
if (Boot.isProd) {
guildMessage.getChannel().sendMessage("```\n" + t + "```").queue();
} else {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
guildMessage.getChannel().sendMessage("```\n" + sw.toString() + "```").queue();
System.err.println("Error caught in: " + t.toString());
t.printStackTrace();
}
}
});
});
}
}
}

View file

@ -2,6 +2,8 @@ package pkg.deepCurse.simpleLoggingGarbage.core;
import javax.security.auth.login.LoginException;
import pkg.deepCurse.nopalmo.core.Boot;
/**
* this class exists for the sole reason of im lazy, as far as i know, this is
* really bad practice and i will replace it at some point, or at least upgrade
@ -13,17 +15,29 @@ public class Log {
public static int loggerLevel = 0;
//@formatter:off
public static boolean bootEnabled = true;
public static boolean guildCommandManagerEnabled = true;
public static void boot(String text) {
boot(text,0);
boot(text, 0);
}
public static void boot(String text, int level) {
if (bootEnabled && level <= loggerLevel) {
System.out.println(text);
System.out.println(Boot.class + ": " + text);
}
}
//@formatter:on
public static void crash(Exception e) {
}
public static void guildCommandManager(String text) {
guildCommandManager(text);
}
public static void guildCommandManager(String text, int level) {
if (guildCommandManagerEnabled && level <= loggerLevel) {
System.out.println(Boot.class + ": " + text);
}
}
}

22
src/services/CTimer.java Normal file
View file

@ -0,0 +1,22 @@
package services;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;
public class CTimer extends Object implements Serializable {
private static final long serialVersionUID = 1861304556437295528L;
int delay = 1000; // milliseconds
int timerResetCount;
ActionListener taskPerformer = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.print("timer: " + timerResetCount);
timerResetCount += 1;
}
};
}

View file

@ -0,0 +1,72 @@
package services;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.List;
public class ConsoleOut {
private ByteArrayOutputStream baos;
private PrintStream previous;
private boolean capturing;
public void start() {
if (capturing) {
return;
}
capturing = true;
previous = System.out;
baos = new ByteArrayOutputStream();
OutputStream outputStreamCombiner = new OutputStreamCombiner(Arrays.asList(previous, baos));
PrintStream custom = new PrintStream(outputStreamCombiner);
System.setOut(custom);
}
public String stop() {
if (!capturing) {
return "";
}
System.setOut(previous);
String capturedValue = baos.toString();
baos = null;
previous = null;
capturing = false;
return capturedValue;
}
private static class OutputStreamCombiner extends OutputStream {
private List<OutputStream> outputStreams;
public OutputStreamCombiner(List<OutputStream> outputStreams) {
this.outputStreams = outputStreams;
}
public void write(int b) throws IOException {
for (OutputStream os : outputStreams) {
os.write(b);
}
}
public void flush() throws IOException {
for (OutputStream os : outputStreams) {
os.flush();
}
}
public void close() throws IOException {
for (OutputStream os : outputStreams) {
os.close();
}
}
}
}

View file

@ -0,0 +1,51 @@
package services;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.nio.channels.SocketChannel;
import java.util.Date;
public class UptimePing {
public static long sendPing(String IP) throws UnknownHostException, IOException {
/*
* StopWatch stopWatch = new StopWatch(); stopWatch.start(); InetAddress sender
* = InetAddress.getByName(IP); System.out.println("Pinging: " + IP);
* stopWatch.stop(); long timeP = stopWatch.getTime(TimeUnit.MICROSECONDS);
* System.out.println(timeP); if
* (sender.isReachable(5000)){//.isReachable(5000)) {
* System.out.println("Successfully pinged: " + IP); return timeP; } else {
* System.out.println("Failed to ping: " + IP); return -1; }
*/
try {
int port = 80;
long timeToRespond = 0;
InetAddress inetAddress = InetAddress.getByName(IP);
InetSocketAddress socketAddress = new InetSocketAddress(inetAddress, port);
SocketChannel sc = SocketChannel.open();
sc.configureBlocking(true);
Date start = new Date();
if (sc.connect(socketAddress)) {
Date stop = new Date();
timeToRespond = (stop.getTime() - start.getTime());
}
System.out.println("fix me");
if (socketAddress.getAddress() == null) {
return -1;
} else {
return timeToRespond;
}
} catch (IOException ex) {
System.out.println(ex.getMessage());
return -1;
}
}
}