feat: middle save

This commit is contained in:
Project_IO 2024-09-03 20:58:09 +09:00
parent 293705da72
commit 81c81b57d9
6 changed files with 41 additions and 23 deletions

View file

@ -10,6 +10,7 @@ plugins {
group = "net.wh64"
version = "1.0-SNAPSHOT"
val ktor_version: String by project
val log4j_version: String by project
val exposed_version: String by project
@ -30,11 +31,14 @@ dependencies {
implementation(kotlin("stdlib"))
implementation(kotlin("reflect"))
implementation("net.dv8tion:JDA:5.1.0")
implementation("io.ktor:ktor-client-cio:$ktor_version")
implementation("io.ktor:ktor-client-core:$ktor_version")
implementation("org.apache.logging.log4j:log4j-api:$log4j_version")
implementation("org.apache.logging.log4j:log4j-core:$log4j_version")
implementation("org.apache.logging.log4j:log4j-slf4j2-impl:$log4j_version")
implementation("org.jetbrains.exposed:exposed-core:$exposed_version")
implementation("org.jetbrains.exposed:exposed-jdbc:$exposed_version")
implementation("org.apache.logging.log4j:log4j-slf4j2-impl:$log4j_version")
implementation("io.ktor:ktor-client-okhttp-jvm:2.3.12")
// testImplementation(platform("org.junit:junit-bom:5.10.0"))
// testImplementation("org.junit.jupiter:junit-jupiter")
}

View file

@ -1,2 +1,3 @@
ktor_version=2.3.12
log4j_version=2.23.1
exposed_version=0.54.0

View file

@ -2,8 +2,10 @@ package net.projecttl.p.x32;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.interactions.commands.Command;
import net.dv8tion.jda.api.interactions.commands.build.Commands;
import net.projecttl.p.x32.command.Ping;
import net.projecttl.p.x32.handler.Command;
import net.projecttl.p.x32.handler.CommandExecutor;
import net.projecttl.p.x32.handler.CommandHandler;
import net.projecttl.p.x32.handler.Ready;
import org.slf4j.Logger;
@ -14,12 +16,15 @@ import java.util.ArrayList;
public class Px32 {
public static final Logger log = LoggerFactory.getLogger(Px32.class);
private JDA jda;
private final ArrayList<Command> commands = new ArrayList<>();
private final ArrayList<CommandExecutor> commands = new ArrayList<>();
private void register() {
commands.forEach(command -> {
jda.updateCommands().addCommands(command.getData()).queue();
log.info("Registered command: {}", command.getData().getName());
jda.upsertCommand(command.getData()).queue();
jda.updateCommands().addCommands(
Commands.context(Command.Type.USER, command.getData().getName())
).queue();
log.info("registered command: {}", command.getData().getName());
});
}

View file

@ -5,7 +5,7 @@ import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.internal.interactions.CommandDataImpl;
import net.projecttl.p.x32.handler.Command;
import net.projecttl.p.x32.handler.CommandExecutor;
import org.jetbrains.annotations.NotNull;
import java.util.Random;
@ -13,7 +13,7 @@ import java.util.concurrent.atomic.AtomicReference;
import static java.lang.String.format;
public class Ping implements Command {
public class Ping implements CommandExecutor {
@NotNull
@Override
public CommandData getData() {

View file

@ -1,16 +1,16 @@
package net.projecttl.p.x32.handler;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.UserContextInteractionEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction;
import net.projecttl.p.x32.Px32;
import java.util.List;
public class CommandHandler extends ListenerAdapter {
private final List<Command> commands;
private final List<CommandExecutor> commands;
public CommandHandler(List<Command> commands) {
public CommandHandler(List<CommandExecutor> commands) {
this.commands = commands;
}
@ -20,15 +20,23 @@ public class CommandHandler extends ListenerAdapter {
return;
}
for (Command command : commands) {
for (CommandExecutor command : commands) {
if (!command.getData().getName().equals(ev.getName())) {
continue;
}
try {
command.execute(ev);
Px32.log.info("user {} execute command: {}", ev.getUser().getId(), ev.getName());
} catch (Exception ex) {
Px32.log.error("user {} execute command {} failed", ev.getUser().getId(), ev.getName(), ex);
}
break;
}
}
@Override
public void onUserContextInteraction(UserContextInteractionEvent ev) {
Px32.log.info("user {} execute context: {}", ev.getUser().getId(), ev.getName());
}
}

View file

@ -3,7 +3,7 @@ package net.projecttl.p.x32.handler
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
import net.dv8tion.jda.api.interactions.commands.build.CommandData
interface Command {
interface CommandExecutor {
val data: CommandData
fun execute(ev: SlashCommandInteractionEvent)
}