Skip to content

Commit

Permalink
Add commands
Browse files Browse the repository at this point in the history
  • Loading branch information
duncte123 committed Mar 7, 2024
1 parent 4d86fa8 commit 3cfb83b
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 5 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/publish-bot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
- name: Image digest
run: echo ${{ steps.docker_build.outputs.digest }}

- name: Trigger dev webhook
uses: distributhor/workflow-webhook@v3
env:
webhook_url: ${{ secrets.WEBHOOK_URL }}
# - name: Trigger dev webhook
# uses: distributhor/workflow-webhook@v3
# env:
# webhook_url: ${{ secrets.WEBHOOK_URL }}
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@

## How to host yourself?
- use docker!


## Planned features
- an easy way for non programmers to add commands through yaml files
31 changes: 31 additions & 0 deletions src/main/kotlin/me/duncte123/io/ICommand.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package me.duncte123.io

import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
import net.dv8tion.jda.api.interactions.commands.build.*

interface ICommand {
val name: String
val description: String

val options: List<OptionData>
get() = listOf()
val subCommands: List<SubcommandData>
get() = listOf()

fun execute(event: SlashCommandInteractionEvent)

fun toCommandData(): CommandData {
val data = Commands.slash(name, description)
.setGuildOnly(true)

if (options.isNotEmpty()) {
data.addOptions(options)
}

if (subCommands.isNotEmpty()) {
data.addSubcommands(subCommands)
}

return data
}
}
23 changes: 22 additions & 1 deletion src/main/kotlin/me/duncte123/io/Main.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
package me.duncte123.io

import me.duncte123.io.commands.JjCommand
import net.dv8tion.jda.api.JDABuilder
import net.dv8tion.jda.api.events.GenericEvent
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
import net.dv8tion.jda.api.events.session.ReadyEvent
import net.dv8tion.jda.api.hooks.EventListener

val commands = mapOf(
"jj" to JjCommand()
)

fun main() {
JDABuilder.createLight(System.getenv("BOT_TOKEN")).build()
JDABuilder.createLight(System.getenv("BOT_TOKEN"))
.addEventListeners(object : EventListener {
override fun onEvent(event: GenericEvent) {
if (event is ReadyEvent) {
event.jda.updateCommands()
.addCommands(commands.values.map { it.toCommandData() })
.queue()
} else if (event is SlashCommandInteractionEvent) {
commands[event.name]?.execute(event)
}
}
})
.build()
}
62 changes: 62 additions & 0 deletions src/main/kotlin/me/duncte123/io/commands/JjCommand.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package me.duncte123.io.commands

import me.duncte123.io.ICommand
import net.dv8tion.jda.api.entities.channel.ChannelType
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
import net.dv8tion.jda.api.interactions.commands.OptionType
import net.dv8tion.jda.api.interactions.commands.build.OptionData
import net.dv8tion.jda.api.utils.FileUpload

const val JJ_ID = 272258147169599489L

class JjCommand : ICommand {
override val name = "jj"
override val description = "Allows JJ to upload pictures via the bot."

override val options = listOf(
OptionData(
OptionType.CHANNEL,
"channel",
"Where to send the file?",
true
),
OptionData(
OptionType.ATTACHMENT,
"file",
"the file to send to the channel",
true
)
)

override fun execute(event: SlashCommandInteractionEvent) {
if (event.user.idLong != JJ_ID) {
event.reply("you are not jj").setEphemeral(true).queue()
return
}

val channel = event.getOption("channel")!!.asChannel

if (!channel.type.isMessage) {
event.reply("Must be a text channel fuckwhat").setEphemeral(true).queue()
return
}

val msgChan = channel.asGuildMessageChannel()

if (!msgChan.canTalk()) {
event.reply("Can't talk there :)").setEphemeral(true).queue()
return
}

val file = event.getOption("file")!!.asAttachment

event.reply("Sending the file to ${channel.asMention}").queue()

file.proxy.download().thenAccept { stream ->
msgChan.sendFiles(FileUpload.fromData(stream, file.fileName))
.queue()
}


}
}

0 comments on commit 3cfb83b

Please sign in to comment.