Started Command Handling, subject to change but additions are welcome

This commit is contained in:
unknown 2022-03-26 22:53:00 -07:00
parent 38a3b5e57c
commit 7c5840c8c1
8 changed files with 207 additions and 7 deletions

View file

@ -19,7 +19,7 @@
<dependency>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId>
<version>4.2.0_247</version>
<version>5.0.0-alpha.9</version>
</dependency>
<dependency>
<groupId>me.carleslc.Simple-YAML</groupId>

View file

@ -1,5 +1,6 @@
package moe.oko.Kiafumi;
import moe.oko.Kiafumi.command.PingCommand;
import moe.oko.Kiafumi.listener.MainListener;
import moe.oko.Kiafumi.model.KiafumiDB;
import moe.oko.Kiafumi.model.ServerManager;
@ -8,6 +9,7 @@ import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.utils.ChunkingFilter;
import net.dv8tion.jda.api.utils.MemberCachePolicy;
@ -16,6 +18,7 @@ import org.simpleyaml.exceptions.InvalidConfigurationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.awt.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
@ -145,9 +148,18 @@ public class Kiafumi {
ex.printStackTrace();
return;
}
PingCommand pc = new PingCommand();
JDA.addEventListener(pc);
for(Guild guild : JDA.getGuilds()) {
for (String name : pc.getSlashCommandInfo().keySet()) {
JDA.upsertCommand(name, pc.getSlashCommandInfo().get(name)).complete();
info("Upsert command " + name + " on guild: " + guild.getId());
}
}
EmbedBuilder eb = new EmbedBuilder().setTitle("Kiafumi Online")
.setAuthor("Created by Oko, Laika, and Tiddy");
JDA.getTextChannelById(config.getLogChannel()).sendMessage(eb.build()).queue();
.setFooter("Created by Oko, Laika, and Tiddy").setColor(new Color(0x6271c1));
JDA.getTextChannelById(config.getLogChannel()).sendMessageEmbeds(eb.build()).queue();
}
/*

View file

@ -4,6 +4,8 @@ import moe.oko.Kiafumi.model.KiafumiDB;
import org.simpleyaml.configuration.ConfigurationSection;
import org.simpleyaml.configuration.file.YamlConfiguration;
import java.util.List;
import static moe.oko.Kiafumi.Kiafumi.error;
import static moe.oko.Kiafumi.Kiafumi.info;
@ -34,6 +36,7 @@ public class KiafumiConfig {
private String activityType;
private String activityMsg;
private String statusType;
private List<String> pingResponses;
/*
SQL Variable Section
@ -73,6 +76,7 @@ public class KiafumiConfig {
activityType = main.getString("activityType");
activityMsg = main.getString("activityMsg");
statusType = main.getString("statusType");
pingResponses = main.getStringList("pingResponses");
//SQL loaders
ConfigurationSection sql = configuration.getConfigurationSection("sql");
host = sql.getString("host");
@ -90,7 +94,7 @@ public class KiafumiConfig {
}
public String assembleDefaultInvite() {
return "https://discord.com/oauth2/authorize?client_id=" + clientId + "&scope=bot&permissions=" + defaultInvitePermissionLevel;
return "https://discord.com/oauth2/authorize?client_id=" + clientId + "&scope=bot+applications.commands&permissions=" + defaultInvitePermissionLevel;
}
public String getPrefix() {
@ -107,5 +111,7 @@ public class KiafumiConfig {
public String getLogChannel() { return logChannel; }
public List<String> getPingResponses() { return pingResponses; }
public KiafumiDB createDb() { return new KiafumiDB(username, password, host, port, database); }
}

View file

@ -0,0 +1,74 @@
package moe.oko.Kiafumi.command;
import moe.oko.Kiafumi.Kiafumi;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import java.util.HashMap;
import java.util.List;
public abstract class CommandClass extends ListenerAdapter {
//Is the command enabled?
public abstract boolean isEnabled();
//Whats the name of the Command Package (Specify if multiple, like "Utility" or "Moderation")
public abstract String getName();
/**
* Your execution function. What the CommandPlugin will include.
* Use a CASE statement to help it out. (Soon to be deprecated in favour of slash cmds)
* @param args - basic arguments send with the command for QOL.
* @param e - MessageRecievedEvent for the sake of getting author or whatever.
*/
@Deprecated
public abstract void legacyCommand(String[] args, MessageReceivedEvent e, boolean prefix);
/**
* For new slash commands
* @param name - name of the command executed
* @param e - SlashCommandInteractionEvent, used for all references.
*/
public abstract void newCommand(String name, SlashCommandInteractionEvent e);
/**
* Overriding our GuildMessageReceivedEvent that way we can execute a cmd...
* @param event - Message received event
*/
@Override
public void onMessageReceived(MessageReceivedEvent event) {
if(event.getAuthor().isBot() || event.getAuthor().isSystem()) return;
if(event.getAuthor().getId().equals(Kiafumi.JDA.getSelfUser().getId())) return;
if(!event.getChannel().canTalk()) return;
String[] args = event.getMessage().getContentDisplay().replace(Kiafumi.PREFIX, "").split(" ");
//Removes prefix from args and also splits the arguments into proper args.
boolean prefixed = event.getMessage().getContentDisplay().startsWith(Kiafumi.PREFIX);
legacyCommand(args, event, prefixed);
}
/**
* Overriding our SlashCommandInteractionEvent that way it can execute a cmd.
* @param event
*/
@Override
public void onSlashCommandInteraction(SlashCommandInteractionEvent event) {
newCommand(event.getName(), event);
}
/**
* For the sake of organization. Format the strings like so:
* command - description.
* @return
*/
public abstract List<String> getCommandsAsList();
/**
* Also for the sake of organization. To upsert slash commands.
* Follow as name, description. (for upsertCommand(name, description);
* @return - The name and description for the commands contained within the class.
*/
public abstract HashMap<String, String> getSlashCommandInfo();
}

View file

@ -0,0 +1,74 @@
package moe.oko.Kiafumi.command;
import moe.oko.Kiafumi.Kiafumi;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import java.awt.*;
import java.time.temporal.TemporalField;
import java.util.*;
import java.util.List;
public class PingCommand extends CommandClass{
//Always true, ping cmd is EXISTENTIAL
@Override
public boolean isEnabled() {
return true;
}
@Override
public String getName() {
return "Ping";
}
@Override
public void legacyCommand(String[] args, MessageReceivedEvent e, boolean prefix) {
if(!prefix) { return; }
switch(args[0].toLowerCase()) {
case "ping":
long sentMs = e.getMessage().getTimeCreated().toInstant().toEpochMilli();
long recMs = System.currentTimeMillis();
long ping = sentMs - recMs;
EmbedBuilder eb = new EmbedBuilder().setColor(new Color(0x6271c1))
.setFooter("ping pong :)").setTitle(getComedy()).setDescription("Pinged " + ping + "ms");
e.getChannel().sendMessageEmbeds(eb.build()).queue();
return;
default:
//how
}
}
@Override
public void newCommand(String name, SlashCommandInteractionEvent e) {
e.deferReply().queue();
switch(name.toLowerCase(Locale.ROOT)) {
case "ping":
long sentMs = e.getTimeCreated().toInstant().toEpochMilli();
long recMs = System.currentTimeMillis();
long ping = sentMs - recMs;
EmbedBuilder eb = new EmbedBuilder().setColor(new Color(0x6271c1))
.setFooter("ping pong :)").setTitle(getComedy()).setDescription("Pinged " + ping + "ms");
e.getHook().sendMessageEmbeds(eb.build()).queue();
return;
default:
}
}
private String getComedy() {
Random r = new Random();
return Kiafumi.instance.config.getPingResponses().get(r.nextInt(Kiafumi.instance.config.getPingResponses().size()));
}
@Override
public List<String> getCommandsAsList() {
return null;
}
@Override
public HashMap<String, String> getSlashCommandInfo() {
HashMap<String, String> si = new HashMap<>();
si.put("ping", "Bounces back a funny response :)");
return si;
}
}

View file

@ -13,12 +13,15 @@ public class Server {
private String joinRole;
private boolean serverProtected;
private boolean modified;
public Server(String id) {
this.id = id;
this.welcomeEnabled = false;
this.welcomeChannel = null;
this.joinRole = null;
this.serverProtected = false;
this.modified = false;
}
/**
@ -34,6 +37,13 @@ public class Server {
this.welcomeChannel = welcomeChannel;
this.joinRole = joinRole;
this.serverProtected = serverProtected;
this.modified = false;
}
public String getId() { return id; }
public boolean isModified() {
return modified;
}
@Override

View file

@ -3,7 +3,6 @@ package moe.oko.Kiafumi.model;
import moe.oko.Kiafumi.Kiafumi;
import net.dv8tion.jda.api.entities.Guild;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -19,11 +18,18 @@ public class ServerManager {
error("Failed to load servers properly. Null val on database.");
return;
}
servers = loadedServers;
HashMap<String, Server> serverHashMap = new HashMap<>();
for(Server s : loadedServers) {
info("Loading " + s + " into memory from db.");
serverHashMap.put(s.getId(), s);
}
info("Successfully loaded " + serverHashMap.size() + " servers.");
servers = serverHashMap;
}
public Server getOrCreateServer(String id) {
if(servers.get(id) == null) { createNewDefaultServer(Kiafumi.JDA.getGuildById(id)); }
return servers.get(id);
}
public boolean createNewDefaultServer(Guild guild) {
@ -31,6 +37,7 @@ public class ServerManager {
Server server = new Server(guild.getId());
if(Kiafumi.instance.getDatabase().createServerInformation(guild)) {
info("New defaults persistent for " + server);
servers.put(server.getId(), server);
return true;
} else {
error("Failed to create new defaults for " + guild.getId());

View file

@ -34,6 +34,23 @@ main:
activityMsg: "+help - gaaaaay"
#Status type (ONLINE|IDLE|DO_NOT_DISTURB|INVISIBLE)
statusType: "IDLE"
#Responses for the default ping command
pingResponses:
- "Pinged the pentagon"
- "Pinged oko's basement"
- "Pinged oko.moe"
- "Pinged alpinia.link"
- "Pinged favware.tech"
- "Pinged you're mother"
- "Pinged 142.250.189.238"
- "Pinged civcraft.vg"
- "Pinged cuomo.zone"
- "Pinged my brain (please help)"
- "Pinged <@951782164847001600>"
- "Pinged A33#*$9331@"
- "Pinged a poortea vpn"
- "Pinged 'agoo'"
- "Pinged Ayana"
sql:
host: "localhost"
port: 3306