mirror of
https://code.chipmunk.land/Parker2991/NitoriBot/
synced 2026-12-05 08:35:39 -05:00
Night of Nights build: 5318
This commit is contained in:
@@ -18,3 +18,6 @@ config.yml
|
||||
|
||||
# ignore chat attestation
|
||||
chatattestation
|
||||
|
||||
# ignore the logs folder
|
||||
logs
|
||||
|
||||
@@ -157,6 +157,5 @@ public @NullMarked class Client extends NitoriBot {
|
||||
options.reconnectDelay,
|
||||
TimeUnit.MILLISECONDS
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ public class Config {
|
||||
public List<String> prefixes;
|
||||
public final Keys keys = new Keys();
|
||||
public final Discord discord = new Discord();
|
||||
public String weatherApiToken;
|
||||
|
||||
public static class DiscordRoles {
|
||||
public String trusted;
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.google.gson.Gson;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
||||
import land.chipmunk.parker2991.nitoribot.nightofnights.boot.GetBotBuildInfo;
|
||||
import land.chipmunk.parker2991.nitoribot.nightofnights.boot.LoadConfig;
|
||||
import land.chipmunk.parker2991.nitoribot.nightofnights.console.CreateConsole;
|
||||
@@ -12,6 +13,8 @@ import land.chipmunk.parker2991.nitoribot.nightofnights.data.buildstring.BotBuil
|
||||
import land.chipmunk.parker2991.nitoribot.nightofnights.data.buildstring.RepoCommitInfo;
|
||||
import land.chipmunk.parker2991.nitoribot.nightofnights.discord.MessageLogger;
|
||||
import land.chipmunk.parker2991.nitoribot.nightofnights.util.ComponentUtil;
|
||||
import land.chipmunk.parker2991.nitoribot.nightofnights.console.FileLogging;
|
||||
import static land.chipmunk.parker2991.nitoribot.nightofnights.console.FileLogging.compress;
|
||||
import net.dv8tion.jda.api.JDA;
|
||||
import net.dv8tion.jda.api.JDABuilder;
|
||||
import net.dv8tion.jda.api.requests.GatewayIntent;
|
||||
@@ -30,11 +33,11 @@ public class Main {
|
||||
private static final Gson GSON = new Gson();
|
||||
public static final ExecutorService executorService = Executors.newSingleThreadExecutor();
|
||||
public static final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
|
||||
public static final List<NitoriBot> Bots = new ArrayList<>();
|
||||
public static final List<NitoriBot> Bots = new ObjectArrayList<>();
|
||||
public static RepoCommitInfo repoCommitInfo = null;
|
||||
public static BotBuildInfo botBuildInfo = null;
|
||||
public static CreateConsole console;
|
||||
public Config config;
|
||||
public static Config config;
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
|
||||
public static final List<BlockRegistries> blocks = new ArrayList<>();
|
||||
|
||||
@@ -121,6 +124,22 @@ public class Main {
|
||||
}
|
||||
}
|
||||
|
||||
public static void stop () {
|
||||
for (final NitoriBot eachBot : Bots) {
|
||||
eachBot.stop();
|
||||
}
|
||||
|
||||
if (config.discord.enabled) {
|
||||
discordClient.shutdown();
|
||||
}
|
||||
|
||||
executorService.shutdown();
|
||||
scheduler.shutdown();
|
||||
|
||||
compress();
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
void main () {
|
||||
try {
|
||||
System.setProperty(
|
||||
|
||||
@@ -90,7 +90,12 @@ public class NitoriBot extends SessionAdapter implements MCProtocolLibMessagingE
|
||||
|
||||
client.connect();
|
||||
} catch (Exception ignore) {}
|
||||
}
|
||||
|
||||
public void stop () {
|
||||
session.removeListener(this);
|
||||
listenerManager.clearListener();
|
||||
session.disconnect("Killing Bot Process");
|
||||
}
|
||||
|
||||
public void connect () throws IOException {
|
||||
|
||||
+134
-2
@@ -1,4 +1,136 @@
|
||||
package land.chipmunk.parker2991.nitoribot.nightofnights.commands.Public;
|
||||
|
||||
public class WeatherCommand {
|
||||
}
|
||||
import com.google.gson.Gson;
|
||||
import land.chipmunk.parker2991.nitoribot.nightofnights.Config;
|
||||
import land.chipmunk.parker2991.nitoribot.nightofnights.NitoriBot;
|
||||
import land.chipmunk.parker2991.nitoribot.nightofnights.command.CommandContext;
|
||||
import land.chipmunk.parker2991.nitoribot.nightofnights.command.CommandError;
|
||||
import land.chipmunk.parker2991.nitoribot.nightofnights.command.CommandInfo;
|
||||
import land.chipmunk.parker2991.nitoribot.nightofnights.command.CommandTrustLevels;
|
||||
import land.chipmunk.parker2991.nitoribot.nightofnights.data.https.WeatherData;
|
||||
import static land.chipmunk.parker2991.nitoribot.nightofnights.util.ComponentUtil.convertColorString;
|
||||
|
||||
import net.kyori.adventure.text.Component;
|
||||
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.List;
|
||||
|
||||
import static land.chipmunk.parker2991.nitoribot.nightofnights.util.HttpsClientUtil.getRequest;
|
||||
|
||||
public class WeatherCommand implements CommandInfo {
|
||||
@Override
|
||||
public CommandTrustLevels getTrustLevel () {
|
||||
return CommandTrustLevels.PUBLIC;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getAliases () {
|
||||
return List.of("weather");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription () {
|
||||
return "check the weather in a area";
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getUsages () {
|
||||
return List.of("<location>");
|
||||
}
|
||||
|
||||
private final Gson gson = new Gson();
|
||||
|
||||
@Override
|
||||
public Component execute (CommandContext context) throws CommandError {
|
||||
final NitoriBot bot = context.bot();
|
||||
final Config config = bot.config;
|
||||
final String args = String.join(
|
||||
" ",
|
||||
context.args()
|
||||
);
|
||||
Component component = null;
|
||||
|
||||
try {
|
||||
final String response = getRequest(String.format(
|
||||
"https://api.weatherapi.com/v1/current.json?key=%s&q=%s",
|
||||
config.weatherApiToken,
|
||||
args.replace(
|
||||
" ",
|
||||
"%20"
|
||||
)
|
||||
));
|
||||
final WeatherData parseResponse = gson.fromJson(response, WeatherData.class);
|
||||
final var zoneDate = ZonedDateTime.now(ZoneId.of(parseResponse.location.tz_id));
|
||||
final var formatTime = DateTimeFormatter.ofPattern("MMMM dd, yyyy hh:mm:ss a");
|
||||
final var time = formatTime.format(zoneDate);
|
||||
|
||||
component = Component.empty()
|
||||
.append(
|
||||
Component.translatable(
|
||||
"%s: %s %s, %s\n",
|
||||
Component.text("Location").color(convertColorString(config.colors.commands.primary)),
|
||||
Component.text(parseResponse.location.name).color(convertColorString(config.colors.commands.primary)),
|
||||
Component.text(parseResponse.location.region).color(convertColorString(config.colors.commands.primary)),
|
||||
Component.text(parseResponse.location.country).color(convertColorString(config.colors.commands.primary))
|
||||
)
|
||||
)
|
||||
.append(
|
||||
Component.translatable(
|
||||
"%s: %s, %s\n",
|
||||
Component.text("Coords").color(convertColorString(config.colors.commands.secondary)),
|
||||
Component.text(parseResponse.location.lat).color(convertColorString(config.colors.integer)),
|
||||
Component.text(parseResponse.location.lon).color(convertColorString(config.colors.integer))
|
||||
)
|
||||
|
||||
)
|
||||
.append(
|
||||
Component.translatable(
|
||||
"%s: %s (%s)\n",
|
||||
Component.text("Time").color(convertColorString(config.colors.commands.primary)),
|
||||
Component.text(time).color(convertColorString(config.colors.commands.primary)),
|
||||
Component.text(parseResponse.location.tz_id).color(convertColorString(config.colors.commands.primary))
|
||||
)
|
||||
)
|
||||
.append(
|
||||
Component.translatable(
|
||||
"%s: %s°C, %s°F\n",
|
||||
Component.text("Temp").color(convertColorString(config.colors.commands.primary)),
|
||||
Component.text(parseResponse.current.temp_c).color(convertColorString(config.colors.integer)),
|
||||
Component.text(parseResponse.current.temp_f).color(convertColorString(config.colors.integer))
|
||||
)
|
||||
)
|
||||
.append(
|
||||
Component.translatable(
|
||||
"%s: %s (kph, %s), %s (mph, %s)\n",
|
||||
Component.text("Wind Speed").color(convertColorString(config.colors.commands.primary)),
|
||||
Component.text(Math.floor(Double.parseDouble(parseResponse.current.wind_kph))).color(convertColorString(config.colors.integer)),
|
||||
Component.text(parseResponse.current.wind_dir).color(convertColorString(config.colors.commands.primary)),
|
||||
Component.text(Math.floor(Double.parseDouble(parseResponse.current.wind_mph))).color(convertColorString(config.colors.integer)),
|
||||
Component.text(parseResponse.current.wind_dir).color(convertColorString(config.colors.commands.primary))
|
||||
)
|
||||
|
||||
)
|
||||
.append(
|
||||
Component.translatable(
|
||||
"%s: %s\n",
|
||||
Component.text("Condition").color(convertColorString(config.colors.commands.primary)),
|
||||
Component.text(parseResponse.current.condition.text).color(convertColorString(config.colors.commands.primary))
|
||||
)
|
||||
)
|
||||
.append(
|
||||
Component.translatable(
|
||||
"%s: %s",
|
||||
Component.text("Humidity").color(convertColorString(config.colors.commands.primary)),
|
||||
Component.text(Math.floor(Double.parseDouble(parseResponse.current.humidity))).color(convertColorString(config.colors.commands.primary))
|
||||
)
|
||||
)
|
||||
.color(convertColorString(config.colors.commands.tertiary));
|
||||
} catch (Exception e) {
|
||||
throw new CommandError(Component.text(e.getMessage()));
|
||||
}
|
||||
|
||||
return component;
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -1,5 +1,6 @@
|
||||
package land.chipmunk.parker2991.nitoribot.nightofnights.commands.owner;
|
||||
|
||||
import land.chipmunk.parker2991.nitoribot.nightofnights.Main;
|
||||
import land.chipmunk.parker2991.nitoribot.nightofnights.NitoriBot;
|
||||
import land.chipmunk.parker2991.nitoribot.nightofnights.command.*;
|
||||
|
||||
@@ -46,7 +47,7 @@ public class KillCommand implements CommandInfo {
|
||||
.color(convertColorString(bot.config.colors.commands.primary))
|
||||
);
|
||||
|
||||
System.exit(1);
|
||||
Main.stop();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
+1
@@ -4,6 +4,7 @@ import land.chipmunk.parker2991.nitoribot.nightofnights.NitoriBot;
|
||||
import land.chipmunk.parker2991.nitoribot.nightofnights.Main;
|
||||
import org.jline.reader.*;
|
||||
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
import java.util.List;
|
||||
|
||||
public class CreateConsole implements Completer {
|
||||
|
||||
+128
@@ -0,0 +1,128 @@
|
||||
package land.chipmunk.parker2991.nitoribot.nightofnights.console;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.time.LocalDate;
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
|
||||
public class FileLogging {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(FileLogging.class);
|
||||
public static final Path logFolderPath = Path.of(
|
||||
System.getProperty("user.dir"),
|
||||
"logs"
|
||||
);
|
||||
|
||||
public static void compress () {
|
||||
final String date = LocalDate
|
||||
.now()
|
||||
.toString();
|
||||
Path newFilePath = Path.of(
|
||||
logFolderPath.toString(),
|
||||
date + ".log"
|
||||
);
|
||||
|
||||
final Path path = Path.of(
|
||||
logFolderPath.toString(),
|
||||
"latest.log"
|
||||
);
|
||||
try {
|
||||
LOGGER.info("compressing log file");
|
||||
if (!Files.exists(newFilePath)) Files.move(
|
||||
path,
|
||||
newFilePath
|
||||
);
|
||||
|
||||
final byte[] buffer = new byte[1024];
|
||||
int counter = 1;
|
||||
String name = date + ".log.gz";
|
||||
Path newFileName = Path.of(logFolderPath.toString(), name);
|
||||
while (Files.exists(newFileName)) {
|
||||
name = String.format(
|
||||
"%s-%s.log.gz",
|
||||
date,
|
||||
counter
|
||||
);
|
||||
newFileName = Path.of(logFolderPath.toString(), name);
|
||||
counter++;
|
||||
}
|
||||
|
||||
GZIPOutputStream os = new GZIPOutputStream(new FileOutputStream(newFileName
|
||||
.toAbsolutePath()
|
||||
.toString()));
|
||||
|
||||
FileInputStream in = new FileInputStream(newFilePath
|
||||
.toAbsolutePath()
|
||||
.toString());
|
||||
|
||||
int totalSize;
|
||||
while ((totalSize = in.read(buffer)) > 0) {
|
||||
os.write(
|
||||
buffer,
|
||||
0,
|
||||
totalSize
|
||||
);
|
||||
}
|
||||
|
||||
in.close();
|
||||
os.finish();
|
||||
os.close();
|
||||
|
||||
Files.delete(newFilePath);
|
||||
|
||||
} catch (Exception e) {
|
||||
LOGGER.error(
|
||||
"{}",
|
||||
e.getClass(),
|
||||
e
|
||||
);
|
||||
}
|
||||
|
||||
LOGGER.info("Finished compressing log");
|
||||
}
|
||||
|
||||
public static void writeLog (String message) {
|
||||
final Path path = Path.of(logFolderPath + "/" + "latest.log");
|
||||
try {
|
||||
Files.writeString(
|
||||
path,
|
||||
message.replaceAll(
|
||||
"\u001B\\[[;\\d]*m",
|
||||
""
|
||||
) + "\n",
|
||||
StandardOpenOption.CREATE,
|
||||
StandardOpenOption.APPEND
|
||||
);
|
||||
} catch (Exception e) {
|
||||
LOGGER.error(
|
||||
"{}",
|
||||
e.getClass(),
|
||||
e
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private void createLogFolder () {
|
||||
if (!Files.exists(logFolderPath)) {
|
||||
LOGGER.info("Log folder not found! creating it now");
|
||||
try {
|
||||
Files.createDirectory(logFolderPath);
|
||||
} catch (Exception e) {
|
||||
LOGGER.error(
|
||||
"{}",
|
||||
e.getClass(),
|
||||
e
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public FileLogging () {
|
||||
createLogFolder();
|
||||
}
|
||||
}
|
||||
+10
-1
@@ -3,15 +3,23 @@ package land.chipmunk.parker2991.nitoribot.nightofnights.console;
|
||||
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
|
||||
import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
import ch.qos.logback.core.AppenderBase;
|
||||
import land.chipmunk.parker2991.nitoribot.nightofnights.Main;
|
||||
import org.jline.reader.LineReader;
|
||||
import org.jspecify.annotations.Nullable;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import static land.chipmunk.parker2991.nitoribot.nightofnights.console.FileLogging.writeLog;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
|
||||
public class JlineAppender extends AppenderBase<ILoggingEvent> {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(JlineAppender.class);
|
||||
private static LineReader reader;
|
||||
private PatternLayoutEncoder encoder;
|
||||
//private PatternLayoutEncoder encoder = new PatternLayoutEncoder();
|
||||
|
||||
public static void setLineReader (@Nullable LineReader lineReader) {
|
||||
reader = lineReader;
|
||||
@@ -42,6 +50,7 @@ public class JlineAppender extends AppenderBase<ILoggingEvent> {
|
||||
StandardCharsets.UTF_8
|
||||
).stripTrailing();
|
||||
|
||||
writeLog(message);
|
||||
if (reader != null) {
|
||||
reader.printAbove(message);
|
||||
} else {
|
||||
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package land.chipmunk.parker2991.nitoribot.nightofnights.data.https;
|
||||
|
||||
public class WeatherData {
|
||||
public final Location location = new Location();
|
||||
public final Current current = new Current();
|
||||
public final class Location {
|
||||
public String name;
|
||||
public String region;
|
||||
public String country;
|
||||
public String lat;
|
||||
public String lon;
|
||||
public String tz_id;
|
||||
}
|
||||
|
||||
public final class Condition {
|
||||
public String text;
|
||||
}
|
||||
|
||||
public final class Current {
|
||||
public String temp_c;
|
||||
public String temp_f;
|
||||
public final Condition condition = new Condition();
|
||||
public String wind_kph;
|
||||
public String wind_dir;
|
||||
public String wind_mph;
|
||||
public String humidity;
|
||||
}
|
||||
}
|
||||
+1
@@ -48,6 +48,7 @@ public class CommandManagerModule {
|
||||
registerCommand(new RTPCommand());
|
||||
registerCommand(new ServerEvalCommand());
|
||||
registerCommand(new NetcmdCommand());
|
||||
registerCommand(new WeatherCommand());
|
||||
}
|
||||
|
||||
public void registerCommand (CommandInfo command) {
|
||||
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package land.chipmunk.parker2991.nitoribot.nightofnights.util;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.time.Duration;
|
||||
|
||||
public class HttpsClientUtil {
|
||||
public static String getRequest (String url) throws IOException, InterruptedException {
|
||||
final HttpClient client = HttpClient
|
||||
.newBuilder()
|
||||
.followRedirects(HttpClient.Redirect.NORMAL)
|
||||
.connectTimeout(Duration.ofSeconds(20))
|
||||
.build();
|
||||
|
||||
final HttpRequest request = HttpRequest
|
||||
.newBuilder()
|
||||
.uri(URI.create(url))
|
||||
.timeout(Duration.ofSeconds(20))
|
||||
.header(
|
||||
"accept",
|
||||
"application/json"
|
||||
)
|
||||
.build();
|
||||
|
||||
final HttpResponse<String> response = client.send(
|
||||
request,
|
||||
HttpResponse.BodyHandlers.ofString()
|
||||
);
|
||||
|
||||
return response.body();
|
||||
}
|
||||
}
|
||||
@@ -26,7 +26,7 @@
|
||||
"color": "blue"
|
||||
}
|
||||
],
|
||||
"build": "5317",
|
||||
"build": "5318",
|
||||
"versionReleaseDate": "N\\A",
|
||||
"initialBotRelease": "November 22, 2022"
|
||||
},
|
||||
|
||||
@@ -9,4 +9,4 @@
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT"/>
|
||||
</root>
|
||||
</configuration>
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user