164 lines
6.9 KiB
Kotlin
164 lines
6.9 KiB
Kotlin
package nl.voidcorp.dbot
|
|
|
|
import com.jagrosh.jdautilities.command.CommandClientBuilder
|
|
import com.jagrosh.jdautilities.command.CommandListener
|
|
import com.sedmelluq.discord.lavaplayer.jdaudp.NativeAudioSendFactory
|
|
import com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayerManager
|
|
import com.sedmelluq.discord.lavaplayer.source.soundcloud.SoundCloudAudioSourceManager
|
|
import com.sedmelluq.discord.lavaplayer.source.youtube.YoutubeAudioSourceManager
|
|
import io.javalin.Javalin
|
|
import io.javalin.json.FromJsonMapper
|
|
import io.javalin.json.JavalinJson
|
|
import io.javalin.json.ToJsonMapper
|
|
import net.dv8tion.jda.core.EmbedBuilder
|
|
import net.dv8tion.jda.core.JDA
|
|
import net.dv8tion.jda.core.JDABuilder
|
|
import net.dv8tion.jda.core.entities.Game
|
|
import net.dv8tion.jda.core.entities.MessageEmbed
|
|
import net.dv8tion.jda.core.events.message.MessageReceivedEvent
|
|
import net.dv8tion.jda.webhook.WebhookClient
|
|
import net.dv8tion.jda.webhook.WebhookClientBuilder
|
|
import nl.voidcorp.dbot.commands.UnityCommand
|
|
import nl.voidcorp.dbot.commands.helloCommand
|
|
import nl.voidcorp.dbot.commands.initMusic
|
|
import nl.voidcorp.dbot.commands.pingCommand
|
|
import nl.voidcorp.dbot.storage.GitlabWebhook
|
|
import org.slf4j.LoggerFactory
|
|
import java.io.File
|
|
import java.io.FileReader
|
|
import java.time.LocalDateTime
|
|
|
|
val playerManager = DefaultAudioPlayerManager()
|
|
|
|
val cb = CommandClientBuilder()
|
|
|
|
val log = LoggerFactory.getLogger("UnityBot")
|
|
|
|
val commands = mutableListOf<UnityCommand>()
|
|
|
|
|
|
@Suppress("JoinDeclarationAndAssignment")
|
|
fun main(args: Array<String>) {
|
|
|
|
playerManager.registerSourceManager(YoutubeAudioSourceManager(true))
|
|
playerManager.registerSourceManager(SoundCloudAudioSourceManager(true))
|
|
|
|
JavalinJson.apply {
|
|
toJsonMapper = object : ToJsonMapper {
|
|
override fun map(obj: Any): String = gson.toJson(obj)
|
|
}
|
|
fromJsonMapper = object : FromJsonMapper {
|
|
override fun <T> map(json: String, targetClass: Class<T>): T = gson.fromJson(json, targetClass)
|
|
}
|
|
}
|
|
|
|
if (!File(".hooks").exists()) {
|
|
log.error("The .hooks file does not exist!")
|
|
log.error("Exiting")
|
|
System.exit(1)
|
|
}
|
|
|
|
val hooks = mutableListOf<WebhookClient>()
|
|
FileReader(".hooks").forEachLine {
|
|
val wh = WebhookClientBuilder(it).build()
|
|
hooks += wh
|
|
}
|
|
|
|
log.info(hooks.toString())
|
|
|
|
val app = Javalin.create().requestLogger { ctx, timeMs ->
|
|
if (ctx.res.status >= 400) {
|
|
log.error("${ctx.method()} ${ctx.path()} -> ${ctx.status()} (${timeMs}ms)")
|
|
} else {
|
|
log.info("${ctx.method()} ${ctx.path()} -> ${ctx.status()} (${timeMs}ms)")
|
|
}
|
|
}.disableStartupBanner().start(4466)
|
|
|
|
app.post("/gitlab.json") { ctx ->
|
|
val bod = ctx.validatedBody<GitlabWebhook>()
|
|
if (bod.value != null) {
|
|
bod.value!!.apply {
|
|
val embed = EmbedBuilder()
|
|
embed.setTitle(project.name, project.web_url)
|
|
val builder = embed.descriptionBuilder
|
|
var url = ""
|
|
commits.forEach {
|
|
builder.append("${it.message} (${it.timestamp})")
|
|
url = it.url
|
|
}
|
|
embed.setAuthor("$user_name ($user_username)", url, user_avatar)
|
|
hooks.forEach { it.send(embed.build()) }
|
|
}
|
|
}
|
|
ctx.status(200).result("OK")
|
|
}
|
|
|
|
cb.setOwnerId("168743656738521088")
|
|
cb.setPrefix("!")
|
|
cb.setAlternativePrefix("+")
|
|
commands.addAll(helloCommand, pingCommand)
|
|
cb.setGame(Game.watching("fraud and \uD83C\uDFB5 (v1.2)"))
|
|
|
|
val replies = listOf("Hello %%", "Why hello there %%!", "Hello there %%", "General %%. You are a bold one", "A wild %% appeared!")
|
|
cb.setHelpConsumer { event ->
|
|
val greeting = replies.random().replace("%%", event.member.effectiveName)
|
|
val eb = EmbedBuilder()
|
|
when {
|
|
event.args.isEmpty() -> {
|
|
eb.setTitle(greeting)
|
|
.appendDescription("My name is ${event.selfMember.effectiveName}, and I am definitely the best Discord bot around!\n\nUse `!help command` for a chance that I have more info about a command!")
|
|
.setColor(event.selfMember.color).setFooter("Requested by ${event.member.effectiveName}", event.author.effectiveAvatarUrl).setTimestamp(LocalDateTime.now())
|
|
val m = commands.catMap()
|
|
for (e in m.entries) {
|
|
|
|
val f = MessageEmbed.Field(e.key, e.value.joinToString(separator = "\n", postfix = "\n") { "`!${it.name}`" }, true)
|
|
eb.addField(f)
|
|
}
|
|
eb.setThumbnail(event.selfUser.effectiveAvatarUrl)
|
|
}
|
|
commands.any { it.name == event.args } -> {
|
|
val cmd = commands.first { it.name == event.args }
|
|
eb.setTitle("**!${cmd.name}**")
|
|
.appendDescription(cmd.help)
|
|
.setColor(event.selfMember.color).setFooter("Requested by ${event.member.effectiveName}", event.author.effectiveAvatarUrl).setTimestamp(LocalDateTime.now())
|
|
if (cmd.aliases.isNotEmpty()) eb.addField("Aliases", cmd.aliases.joinToString { "`$it`" }, false)
|
|
|
|
}
|
|
event.args == "help" -> {
|
|
eb.setTitle(greeting)
|
|
.appendDescription("Haha, very funny ${event.member.effectiveName}")
|
|
.setColor(event.selfMember.color).setFooter("Requested by ${event.member.effectiveName}", event.author.effectiveAvatarUrl).setTimestamp(LocalDateTime.now())
|
|
}
|
|
else -> {
|
|
eb.setTitle(greeting)
|
|
.appendDescription("I honestly have no idea what the command `${event.args}` does...")
|
|
.setColor(event.selfMember.color).setFooter("Requested by ${event.member.effectiveName}", event.author.effectiveAvatarUrl).setTimestamp(LocalDateTime.now())
|
|
}
|
|
|
|
}
|
|
event.reply(eb.build())
|
|
|
|
}
|
|
|
|
initMusic()
|
|
cb.setListener(object : CommandListener {
|
|
override fun onNonCommandMessage(event: MessageReceivedEvent) {
|
|
val msg = event.message.contentRaw
|
|
if (msg.startsWith('!') or msg.startsWith('+')) {
|
|
val eb = EmbedBuilder().setTitle("Something went wrong...")
|
|
.addField("Unknown command: `${msg.substring(1)}`", "I don't know this command...", false)
|
|
.setColor(event.guild.selfMember.color).setFooter("Requested by ${event.member.effectiveName}", event.author.effectiveAvatarUrl).setTimestamp(LocalDateTime.now())
|
|
event.textChannel.sendMessage(eb.build()).queue()
|
|
}
|
|
}
|
|
})
|
|
|
|
|
|
cb.addCommands(*commands.toTypedArray())
|
|
val client = cb.build()
|
|
bot = JDABuilder(args[0]).addEventListener(client).addEventListener(nl.voidcorp.dbot.Events).setAudioSendFactory(NativeAudioSendFactory()).build()
|
|
}
|
|
|
|
lateinit var bot: JDA
|
|
|