Add initial code

This commit is contained in:
Frankie B 2024-07-26 20:14:08 +01:00
commit 87a7aef368
No known key found for this signature in database
12 changed files with 435 additions and 0 deletions

View file

@ -0,0 +1,71 @@
package net.hypr.doki;
import com.google.gson.Gson;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
//You can add more fields in this class, if your input json matches the structure
//You will need a valid config.json in the package com.freya02.bot for this to work
public class Config {
@SuppressWarnings("unused") private String token;
@SuppressWarnings("unused") private DBConfig dbConfig;
/**
* Returns the configuration object for this bot
*
* @return The config
* @throws IOException if the config JSON could not be read
*/
public static Config readConfig() throws IOException {
System.out.println(Path.of("config.json"));
try (InputStream in=Thread.currentThread().getContextClassLoader().getResourceAsStream("config.json")) {
assert in != null;
Reader reader = new InputStreamReader(in, StandardCharsets.UTF_8);
return new Gson().fromJson(reader, Config.class);
} catch (IOException e) {
throw new IOException("""
config.json was not found in the root folder (of the project), did you forget to put it ?
Example structure:
{
"token": "[your_bot_token_here]"
}
""", e);
}
}
public String getToken() {
return token;
}
public DBConfig getDbConfig() {
return dbConfig;
}
public static class DBConfig {
@SuppressWarnings("unused") private String serverName, user, password, dbName;
@SuppressWarnings("unused") private int portNumber;
public String getServerName() {
return serverName;
}
public String getUser() {
return user;
}
public String getPassword() {
return password;
}
public String getDbName() {
return dbName;
}
public int getPortNumber() {
return portNumber;
}
}
}

View file

@ -0,0 +1,57 @@
package net.hypr.doki;
import com.freya02.botcommands.api.CommandsBuilder;
import com.freya02.botcommands.api.Logging;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.requests.GatewayIntent;
import org.slf4j.Logger;
import java.io.IOException;
public class Doki {
private static final Logger LOGGER = Logging.getLogger();
private static JDA jda;
private static Config config;
private Doki(JDA jda, Config config) {
Doki.jda = jda;
Doki.config = config;
}
public JDA getJDA() {
return jda;
}
public static Doki start() throws IOException, InterruptedException {
config = Config.readConfig();
final JDA jda = JDABuilder.createLight(config.getToken())
.setActivity(Activity.customStatus("Banned from everywhere"))
.enableIntents(GatewayIntent.MESSAGE_CONTENT)
.build()
.awaitReady();
//Print some information about the bot
LOGGER.info("Bot connected as {}", jda.getSelfUser().getAsTag());
LOGGER.info("The bot is present on these guilds :");
for (Guild guild : jda.getGuildCache()) {
LOGGER.info("\t- {} ({})", guild.getName(), guild.getId());
}
return new Doki(jda, config);
}
public static void main(String[] args) {
try {
jda = start().getJDA();
CommandsBuilder.newBuilder(437970062922612737L)
.textCommandBuilder(textCommandsBuilder -> textCommandsBuilder.addPrefix("oki!"))
.build(jda, "net.hypr.doki.commands"); //Registering listeners is taken care of by the lib
} catch (Exception e) {
LOGGER.error("Unable to start the bot", e);
System.exit(-1);
}
}
}

View file

@ -0,0 +1,43 @@
package net.hypr.doki.commands;
import com.freya02.botcommands.api.prefixed.CommandEvent;
import com.freya02.botcommands.api.prefixed.TextCommand;
import com.freya02.botcommands.api.prefixed.annotations.Category;
import com.freya02.botcommands.api.prefixed.annotations.Description;
import com.freya02.botcommands.api.prefixed.annotations.JDATextCommand;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.SelfUser;
import java.awt.*;
@Category("Misc")
@Description("Get about info")
public class About extends TextCommand {
@JDATextCommand(name = "about")
public void execute(CommandEvent event) {
JDA jda = event.getJDA();
SelfUser selfUser = jda.getSelfUser();
String jdaVersion = "";
try {
Class<?> jdaClass = Class.forName("net.dv8tion.jda.api.JDA");
Package jdaPackage = jdaClass.getPackage();
if (jdaPackage != null) {
// Attempt to get Implementation-Version from the manifest
String version = jdaPackage.getImplementationVersion();
jdaVersion = version != null ? version : "Unknown";
}
} catch (Exception ex) {
jdaVersion = "UNKNOWN";
}
EmbedBuilder aboutEmbed = new EmbedBuilder()
.setTitle("About " + selfUser.getName())
.setThumbnail(selfUser.getAvatarUrl())
.setDescription("Banned from every deli nationwide")
.addField("Guilds", String.valueOf(jda.getGuildCache().stream().count()), true)
.addField("JDA Version", jdaVersion, true)
.addField("Owner", "fwoppydwisk", true)
.setColor(new Color(210,138,39));
event.reply(aboutEmbed.build()).queue();
}
}

View file

@ -0,0 +1,19 @@
package net.hypr.doki.commands;
import com.freya02.botcommands.api.prefixed.CommandEvent;
import com.freya02.botcommands.api.prefixed.TextCommand;
import com.freya02.botcommands.api.prefixed.annotations.Category;
import com.freya02.botcommands.api.prefixed.annotations.Description;
import com.freya02.botcommands.api.prefixed.annotations.JDATextCommand;
@Category("Misc")
@Description("Pong!")
public class Ping extends TextCommand {
@JDATextCommand(name = "ping")
public void execute(CommandEvent event) {
final long gatewayPing = event.getJDA().getGatewayPing();
event.getJDA().getRestPing()
.queue(restPing -> event.respondFormat("Gateway ping: **%d ms**\nRest ping: **%d ms**", gatewayPing, restPing).queue());
}
}