Move over last commands to custom command class, fancify help a bit

This commit is contained in:
Julius de Jeu 2018-10-17 21:15:09 +02:00
parent 65195f7303
commit 1dd0925897
4 changed files with 89 additions and 52 deletions

View file

@ -1,7 +1,7 @@
package nl.voidcorp.dbot package nl.voidcorp.dbot
import com.jagrosh.jdautilities.command.Command
import com.jagrosh.jdautilities.command.CommandClientBuilder import com.jagrosh.jdautilities.command.CommandClientBuilder
import com.jagrosh.jdautilities.command.CommandListener
import com.sedmelluq.discord.lavaplayer.jdaudp.NativeAudioSendFactory import com.sedmelluq.discord.lavaplayer.jdaudp.NativeAudioSendFactory
import com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayerManager import com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayerManager
import com.sedmelluq.discord.lavaplayer.source.soundcloud.SoundCloudAudioSourceManager import com.sedmelluq.discord.lavaplayer.source.soundcloud.SoundCloudAudioSourceManager
@ -13,11 +13,12 @@ import io.javalin.json.ToJsonMapper
import net.dv8tion.jda.core.EmbedBuilder import net.dv8tion.jda.core.EmbedBuilder
import net.dv8tion.jda.core.JDA import net.dv8tion.jda.core.JDA
import net.dv8tion.jda.core.JDABuilder import net.dv8tion.jda.core.JDABuilder
import net.dv8tion.jda.core.MessageBuilder
import net.dv8tion.jda.core.entities.Game import net.dv8tion.jda.core.entities.Game
import net.dv8tion.jda.core.entities.MessageEmbed 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.WebhookClient
import net.dv8tion.jda.webhook.WebhookClientBuilder import net.dv8tion.jda.webhook.WebhookClientBuilder
import nl.voidcorp.dbot.commands.UnityCommand
import nl.voidcorp.dbot.commands.helloCommand import nl.voidcorp.dbot.commands.helloCommand
import nl.voidcorp.dbot.commands.initMusic import nl.voidcorp.dbot.commands.initMusic
import nl.voidcorp.dbot.commands.pingCommand import nl.voidcorp.dbot.commands.pingCommand
@ -33,7 +34,7 @@ val cb = CommandClientBuilder()
val log = LoggerFactory.getLogger("UnityBot") val log = LoggerFactory.getLogger("UnityBot")
val commands = mutableListOf<Command>() val commands = mutableListOf<UnityCommand>()
@Suppress("JoinDeclarationAndAssignment") @Suppress("JoinDeclarationAndAssignment")
@ -96,34 +97,62 @@ fun main(args: Array<String>) {
cb.setPrefix("!") cb.setPrefix("!")
cb.setAlternativePrefix("+") cb.setAlternativePrefix("+")
commands.addAll(helloCommand, pingCommand) commands.addAll(helloCommand, pingCommand)
cb.setGame(Game.watching("fraud and \uD83C\uDFB5")) 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!") val replies = listOf("Hello %%", "Why hello there %%!", "Hello there %%", "General %%. You are a bold one", "A wild %% appeared!")
cb.setHelpConsumer { event -> cb.setHelpConsumer { event ->
val greeting = replies.random().replace("%%", event.member.effectiveName) val greeting = replies.random().replace("%%", event.member.effectiveName)
val eb = EmbedBuilder().setTitle(greeting) val eb = EmbedBuilder()
.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!") when {
.setColor(event.selfMember.color).setFooter("Requested by ${event.member.effectiveName}", event.author.effectiveAvatarUrl).setTimestamp(LocalDateTime.now()) event.args.isEmpty() -> {
val m = commands.catMap() eb.setTitle(greeting)
for (e in m.entries){ .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") { "!${it.name}" }, true) val f = MessageEmbed.Field(e.key, e.value.joinToString(separator = "\n", postfix = "\n") { "`!${it.name}`" }, true)
eb.addField(f) eb.addField(f)
} }
eb.setThumbnail(event.selfUser.effectiveAvatarUrl) eb.setThumbnail(event.selfUser.effectiveAvatarUrl)
val mb = MessageBuilder("$greeting\n").append {
var st = ""
for (c in commands) {
st += "`${c.name}`: ${c.help}${if (c.aliases.isNotEmpty()) " (alias: `${c.aliases.first()}`)" else ""}\n"
} }
st 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()) event.reply(eb.build())
} }
initMusic() 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()) cb.addCommands(*commands.toTypedArray())
val client = cb.build() val client = cb.build()

View file

@ -1,6 +1,5 @@
package nl.voidcorp.dbot.commands package nl.voidcorp.dbot.commands
import com.jagrosh.jdautilities.command.CommandBuilder
import net.dv8tion.jda.core.MessageBuilder import net.dv8tion.jda.core.MessageBuilder
import java.time.temporal.ChronoUnit import java.time.temporal.ChronoUnit

View file

@ -1,6 +1,5 @@
package nl.voidcorp.dbot.commands package nl.voidcorp.dbot.commands
import com.jagrosh.jdautilities.command.CommandBuilder
import com.jagrosh.jdautilities.command.CommandEvent import com.jagrosh.jdautilities.command.CommandEvent
import com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler import com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler
import com.sedmelluq.discord.lavaplayer.player.AudioPlayer import com.sedmelluq.discord.lavaplayer.player.AudioPlayer
@ -23,26 +22,16 @@ fun initMusic() {
AudioSourceManagers.registerRemoteSources(playerManager) AudioSourceManagers.registerRemoteSources(playerManager)
val queueCommand = CommandBuilder().setName("queue").addAlias("q").setHelp("Queue's a song (use without arguments to get the queue)").build { event -> val queueCommand = UnityMusicCommand("queue", aliases = *arrayOf("q"), help = "Use this command to queue a song, if you don't add a link it will search on youtube with the specified arguments\n\nExecute with no arguments to view the current queue!") { event, scheduler ->
if (event.args.isEmpty()) { if (event.args.isEmpty()) {
if (!guildMusicMap.containsKey(event.guild.idLong) || (guildMusicMap[event.guild.idLong]!!.player.playingTrack == null)) if (!guildMusicMap.containsKey(event.guild.idLong) || (guildMusicMap[event.guild.idLong]!!.player.playingTrack == null))
event.reply("The track list is empty") else event.reply("The track list is empty") else
event.reply(guildMusicMap[event.guild.idLong]!!.getTrackList(event.member)) event.reply(guildMusicMap[event.guild.idLong]!!.getTrackList(event.member))
return@build return@UnityMusicCommand
} }
val scheduler = if (guildMusicMap.containsKey(event.guild.idLong)) guildMusicMap[event.guild.idLong]!! else {
val channel = event.guild.voiceChannels.firstOrNull { it.members.contains(event.member) }
if (channel == null) {
event.reply("Join a voice Channel please!")
return@build
}
val s = TrackScheduler(playerManager.createPlayer(), event.guild, channel)
guildMusicMap[event.guild.idLong] = s
s
}
playerManager.loadItem(event.args, object : AudioLoadResultHandler { playerManager.loadItem(event.args, object : AudioLoadResultHandler {
@ -69,11 +58,40 @@ fun initMusic() {
}) })
} }
val ytCommand = CommandBuilder().setName("youtube").addAlias("yt").setHelp("Play a song from YouTube!").build { event -> val ytCommand = UnityMusicCommand("youtube", aliases = *arrayOf("yt"), help = "Search YouTube for a song!") { event, scheduler ->
queueCommand.run(event) if (event.args.isEmpty()) {
event.reply("Please supply a song name or URL!")
return@UnityMusicCommand
}
playerManager.loadItem(event.args, object : AudioLoadResultHandler {
override fun loadFailed(exception: FriendlyException) {
event.reply("Shit's fucked!")
}
override fun trackLoaded(track: AudioTrack) {
scheduler.queue(track, event.member)
}
override fun noMatches() {
getLinkFromSearch(event, scheduler, false)
}
override fun playlistLoaded(playlist: AudioPlaylist) {
for (t in playlist.tracks) {
scheduler.queue(t, event.member)
}
}
})
} }
val soundcloudCommand = UnityMusicCommand("soundcloud", help = "Play a song via SoundCloud!", aliases = *arrayOf("sc")) { event, scheduler -> val soundcloudCommand = UnityMusicCommand("soundcloud", help = "Play a song via SoundCloud!", aliases = *arrayOf("sc")) { event, scheduler ->
if (event.args.isEmpty()) {
event.reply("Please supply a song name or URL!")
return@UnityMusicCommand
}
playerManager.loadItem(event.args, object : AudioLoadResultHandler { playerManager.loadItem(event.args, object : AudioLoadResultHandler {
override fun loadFailed(exception: FriendlyException) { override fun loadFailed(exception: FriendlyException) {
@ -100,7 +118,10 @@ fun initMusic() {
} }
val playCommand = UnityMusicCommand("play", "Force this song to be the next!", aliases = *arrayOf("p")) { event, scheduler -> val playCommand = UnityMusicCommand("play", "Force this song to be the next!", aliases = *arrayOf("p")) { event, scheduler ->
if (event.args.isEmpty()) {
event.reply("Please supply a song name or URL!")
return@UnityMusicCommand
}
playerManager.loadItem(event.args, object : AudioLoadResultHandler { playerManager.loadItem(event.args, object : AudioLoadResultHandler {
override fun loadFailed(exception: FriendlyException) { override fun loadFailed(exception: FriendlyException) {
event.reply("Shit's fucked!") event.reply("Shit's fucked!")
@ -144,21 +165,11 @@ fun initMusic() {
} }
val attachmentPlay = CommandBuilder().setName("attachment").addAlias("attach").setHelp("Plays the attached file (`use the optional comment to activate the command`)").build { event -> val attachmentPlay = UnityMusicCommand("attachment", help = "Play any attached song, if it is in a normal format that is...\n\n`just drag and drop the song on your Discord window and in the optional comment add !attach(ment)`", aliases = *arrayOf("attach")) { event, scheduler ->
val attach = event.message.attachments.firstOrNull() val attach = event.message.attachments.firstOrNull()
if (attach == null) { if (attach == null) {
event.reply("I can't play an attachment without an attachment...") event.reply("I can't play an attachment without an attachment...")
} else { } else {
val scheduler = if (guildMusicMap.containsKey(event.guild.idLong)) guildMusicMap[event.guild.idLong]!! else {
val channel = event.guild.voiceChannels.firstOrNull { it.members.contains(event.member) }
if (channel == null) {
event.reply("Join a voice Channel please!")
return@build
}
val s = TrackScheduler(playerManager.createPlayer(), event.guild, channel)
guildMusicMap[event.guild.idLong] = s
s
}
playerManager.loadItem(attach.url, object : AudioLoadResultHandler { playerManager.loadItem(attach.url, object : AudioLoadResultHandler {
override fun loadFailed(exception: FriendlyException) { override fun loadFailed(exception: FriendlyException) {

View file

@ -16,8 +16,6 @@ import nl.voidcorp.dbot.log
import java.awt.Color import java.awt.Color
import java.time.LocalDateTime import java.time.LocalDateTime
import java.util.* import java.util.*
import kotlin.concurrent.thread
import kotlin.concurrent.timer
import kotlin.concurrent.timerTask import kotlin.concurrent.timerTask
@ -146,10 +144,10 @@ class TrackScheduler(val player: AudioPlayer, val guild: Guild, channel: VoiceCh
return EmbedBuilder() return EmbedBuilder()
.setFooter("Requested by ${(track.userData as Member).effectiveName}", (track.userData as Member) .setFooter("Requested by ${(track.userData as Member).effectiveName}", (track.userData as Member)
.user.effectiveAvatarUrl).setAuthor(track.info.author).setTitle(track.info.title, track.info.uri) .user.effectiveAvatarUrl).setAuthor(track.info.author).setTitle(track.info.title, track.info.uri)
.setThumbnail(art) .setThumbnail(if (art == "null") null else art)
.setTimestamp(LocalDateTime.now()).setColor(Color.decode("#ff5500")).build() .setTimestamp(LocalDateTime.now()).setColor(Color.decode("#ff5500")).build()
}else if (track is HttpAudioTrack){ } else if (track is HttpAudioTrack) {
if (track.userData is Member) if (track.userData is Member)
return EmbedBuilder() return EmbedBuilder()
.setFooter("Requested by ${(track.userData as Member).effectiveName}", (track.userData as Member) .setFooter("Requested by ${(track.userData as Member).effectiveName}", (track.userData as Member)