feat: middle save

This commit is contained in:
Project_IO 2024-09-08 01:13:13 +09:00
parent 81c81b57d9
commit fab98e826d
20 changed files with 211 additions and 233 deletions

View file

@ -1,70 +1,51 @@
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("java")
kotlin("jvm") version "2.0.20"
id("com.gradleup.shadow") version "8.3.0"
}
group = "net.wh64"
version = "1.0-SNAPSHOT"
group = property("group")!!
version = property("version")!!
val ktor_version: String by project
val log4j_version: String by project
val exposed_version: String by project
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(21))
}
allprojects {
apply(plugin = "java")
apply(plugin = "org.jetbrains.kotlin.jvm")
sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21
}
repositories {
mavenCentral()
}
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.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")
}
tasks {
test {
useJUnitPlatform()
}
withType<JavaCompile> {
options.encoding = "UTF-8"
}
withType<KotlinCompile> {
compilerOptions {
jvmTarget.set(JvmTarget.JVM_21)
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(21))
}
sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21
}
shadowJar {
archiveBaseName.set(project.name)
archiveClassifier.set("")
archiveVersion.set("")
manifest {
attributes["Main-Class"] = "net.projecttl.p.x32.Px32"
}
repositories {
mavenCentral()
}
}
subprojects {
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.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")
}
}
tasks.test {
useJUnitPlatform()
}

View file

@ -1,3 +1,9 @@
kotlin.code.style=official
org.gradle.jvmargs=-Xmx2048M -Dfile.encoding=UTF-8
group=net.projecttl
version=0.1.0-SNAPSHOT
ktor_version=2.3.12
log4j_version=2.23.1
exposed_version=0.54.0

View file

@ -0,0 +1,15 @@
plugins {
id("java")
}
group = rootProject.group
version = rootProject.version
dependencies {
testImplementation(platform("org.junit:junit-bom:5.10.0"))
testImplementation("org.junit.jupiter:junit-jupiter")
}
tasks.test {
useJUnitPlatform()
}

View file

@ -0,0 +1,14 @@
plugins {
id("java")
}
group = rootProject.group
version = rootProject.version
repositories {
mavenCentral()
}
tasks.test {
useJUnitPlatform()
}

View file

@ -0,0 +1,18 @@
package net.wh64.p.x32;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
public class KernelCore {
private final JDABuilder builder;
public KernelCore(String token) {
this.builder = JDABuilder.createDefault(token);
}
public JDA build() {
JDA jda = builder.build();
return jda;
}
}

View file

@ -0,0 +1,5 @@
package net.wh64.p.x32
class GlobalCommandHandler {
}

View file

@ -0,0 +1,39 @@
package net.wh64.p.x32
import net.dv8tion.jda.api.events.interaction.command.MessageContextInteractionEvent
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
import net.dv8tion.jda.api.events.interaction.command.UserContextInteractionEvent
import net.dv8tion.jda.api.interactions.commands.build.CommandData
interface SlashCommandExecutor {
val data: CommandData
fun execute(ev: SlashCommandInteractionEvent)
}
interface UserContextExecutor {
val data: CommandData
fun execute(ev: UserContextInteractionEvent)
}
interface MessageContextExecutor {
val data: CommandData
fun execute(ev: MessageContextInteractionEvent)
}
abstract class CommandAdapter : SlashCommandExecutor, UserContextExecutor, MessageContextExecutor {
abstract override val data: CommandData
override fun execute(ev: SlashCommandInteractionEvent) {
executor(ev)
}
override fun execute(ev: UserContextInteractionEvent) {
executor(ev)
}
override fun execute(ev: MessageContextInteractionEvent) {
executor(ev)
}
abstract fun executor(ev: Any)
}

View file

@ -0,0 +1,31 @@
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("com.gradleup.shadow") version "8.3.0"
}
group = rootProject.group
version = rootProject.version
tasks {
test {
useJUnitPlatform()
}
processResources {
filesMatching("default.properties") {
expand(project.properties)
}
}
withType<JavaCompile> {
options.encoding = "UTF-8"
}
withType<KotlinCompile> {
compilerOptions {
jvmTarget.set(JvmTarget.JVM_21)
}
}
}

View file

@ -0,0 +1,20 @@
package net.projecttl.p.x32.exec;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.projecttl.p.x32.exec.config.DefaultConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Px32 {
public static final Logger log = LoggerFactory.getLogger(Px32.class);
private static JDA jda;
public static void main(String[] args) {
JDABuilder builder = JDABuilder.createDefault(System.getenv("BOT_TOKEN"));
log.info("Px32 {}", DefaultConfig.INSTANCE.getVersion());
builder.setAutoReconnect(true);
jda = builder.build();
}
}

View file

@ -0,0 +1,24 @@
package net.projecttl.p.x32.exec.config
import java.util.*
import kotlin.reflect.KProperty
object DefaultConfig {
private fun useConfig(): DefaultConfigDelegate {
return DefaultConfigDelegate()
}
val version: String by useConfig()
}
private class DefaultConfigDelegate {
private val props = Properties()
init {
props.load(this.javaClass.getResourceAsStream("/default.properties"))
}
operator fun getValue(thisRef: Any?, property: KProperty<*>): String {
return props.getProperty(property.name).toString()
}
}

View file

@ -0,0 +1 @@
version=${version}

View file

@ -1,2 +1,4 @@
rootProject.name = "px32-bot"
include("px32-bot-runner")
include("px32-bot-core")
include("px32-bot-api")

View file

@ -1,47 +0,0 @@
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.CommandExecutor;
import net.projecttl.p.x32.handler.CommandHandler;
import net.projecttl.p.x32.handler.Ready;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
public class Px32 {
public static final Logger log = LoggerFactory.getLogger(Px32.class);
private JDA jda;
private final ArrayList<CommandExecutor> commands = new ArrayList<>();
private void register() {
commands.forEach(command -> {
jda.upsertCommand(command.getData()).queue();
jda.updateCommands().addCommands(
Commands.context(Command.Type.USER, command.getData().getName())
).queue();
log.info("registered command: {}", command.getData().getName());
});
}
public static void main(String[] args) {
Px32 core = new Px32();
JDABuilder builder = JDABuilder.createDefault(System.getenv("BOT_TOKEN"));
core.commands.add(new Ping());
CommandHandler handler = new CommandHandler(core.commands);
builder.addEventListeners(
handler,
new Ready()
);
builder.setAutoReconnect(true);
core.jda = builder.build();
core.register();
}
}

View file

@ -1,45 +0,0 @@
package net.projecttl.p.x32.command;
import net.dv8tion.jda.api.EmbedBuilder;
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.CommandExecutor;
import org.jetbrains.annotations.NotNull;
import java.util.Random;
import java.util.concurrent.atomic.AtomicReference;
import static java.lang.String.format;
public class Ping implements CommandExecutor {
@NotNull
@Override
public CommandData getData() {
return CommandData.fromData(new CommandDataImpl(
"ping",
"Discord API 레이턴시를 확인 합니다."
).toData());
}
@Override
public void execute(SlashCommandInteractionEvent ev) {
long current = System.currentTimeMillis();
AtomicReference<MessageEmbed> embed = new AtomicReference<>(new EmbedBuilder()
.setDescription(":hourglass: Just wait a seconds...")
.build());
ev.replyEmbeds(embed.get()).queue(hook -> {
Random r = new Random();
embed.set(new EmbedBuilder()
.setTitle(":ping_pong: Pong!")
.addField("\uD83E\uDD16", format("**%d**ms", System.currentTimeMillis() - current), true)
.addField("\uD83D\uDD0C", format("**%d**ms", ev.getJDA().getGatewayPing()), true)
.setColor(r.nextInt(0x000001, 0xffffff))
.build());
hook.editOriginalEmbeds(embed.get()).queue();
});
}
}

View file

@ -1,42 +0,0 @@
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.projecttl.p.x32.Px32;
import java.util.List;
public class CommandHandler extends ListenerAdapter {
private final List<CommandExecutor> commands;
public CommandHandler(List<CommandExecutor> commands) {
this.commands = commands;
}
@Override
public void onSlashCommandInteraction(SlashCommandInteractionEvent ev) {
if (ev.getUser().isBot()) {
return;
}
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

@ -1,15 +0,0 @@
package net.projecttl.p.x32.handler;
import net.dv8tion.jda.api.events.session.ReadyEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Ready extends ListenerAdapter {
private static final Logger log = LoggerFactory.getLogger(Ready.class);
@Override
public void onReady(ReadyEvent ev) {
log.info("Logged in as {}", ev.getJDA().getSelfUser().getAsTag());
}
}

View file

@ -1,4 +0,0 @@
package net.projecttl.p.x32
class Database {
}

View file

@ -1,9 +0,0 @@
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 CommandExecutor {
val data: CommandData
fun execute(ev: SlashCommandInteractionEvent)
}

View file

@ -1,16 +0,0 @@
package net.projecttl.p.x32.service
import org.jetbrains.exposed.sql.transactions.transaction
interface ServiceProvider<P, T> {
fun <T> dbQuery(block: () -> T): T =
transaction { block() }
fun create(data: T)
fun read(id: P): T?
fun update(id: P, data: T) {}
fun delete(id: P) {}
}