From 1dd09258971d7479fff1e5af6b3e5a6e1cfbffec Mon Sep 17 00:00:00 2001 From: Julius de Jeu Date: Wed, 17 Oct 2018 21:15:09 +0200 Subject: [PATCH] Move over last commands to custom command class, fancify help a bit --- src/main/kotlin/nl/voidcorp/dbot/UnityBot.kt | 69 +++++++++++++------ .../nl/voidcorp/dbot/commands/Commands.kt | 1 - .../kotlin/nl/voidcorp/dbot/commands/Music.kt | 65 +++++++++-------- .../nl/voidcorp/dbot/music/TrackScheduler.kt | 6 +- 4 files changed, 89 insertions(+), 52 deletions(-) diff --git a/src/main/kotlin/nl/voidcorp/dbot/UnityBot.kt b/src/main/kotlin/nl/voidcorp/dbot/UnityBot.kt index d6d9a77..e720945 100644 --- a/src/main/kotlin/nl/voidcorp/dbot/UnityBot.kt +++ b/src/main/kotlin/nl/voidcorp/dbot/UnityBot.kt @@ -1,7 +1,7 @@ package nl.voidcorp.dbot -import com.jagrosh.jdautilities.command.Command 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 @@ -13,11 +13,12 @@ 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.MessageBuilder 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 @@ -33,7 +34,7 @@ val cb = CommandClientBuilder() val log = LoggerFactory.getLogger("UnityBot") -val commands = mutableListOf() +val commands = mutableListOf() @Suppress("JoinDeclarationAndAssignment") @@ -96,34 +97,62 @@ fun main(args: Array) { cb.setPrefix("!") cb.setAlternativePrefix("+") 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!") cb.setHelpConsumer { event -> val greeting = replies.random().replace("%%", event.member.effectiveName) - val eb = EmbedBuilder().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 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") { "!${it.name}" }, true) - eb.addField(f) - } - 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" + val f = MessageEmbed.Field(e.key, e.value.joinToString(separator = "\n", postfix = "\n") { "`!${it.name}`" }, true) + eb.addField(f) + } + eb.setThumbnail(event.selfUser.effectiveAvatarUrl) } - 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()) + } 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() diff --git a/src/main/kotlin/nl/voidcorp/dbot/commands/Commands.kt b/src/main/kotlin/nl/voidcorp/dbot/commands/Commands.kt index 2c97870..d2dd45c 100644 --- a/src/main/kotlin/nl/voidcorp/dbot/commands/Commands.kt +++ b/src/main/kotlin/nl/voidcorp/dbot/commands/Commands.kt @@ -1,6 +1,5 @@ package nl.voidcorp.dbot.commands -import com.jagrosh.jdautilities.command.CommandBuilder import net.dv8tion.jda.core.MessageBuilder import java.time.temporal.ChronoUnit diff --git a/src/main/kotlin/nl/voidcorp/dbot/commands/Music.kt b/src/main/kotlin/nl/voidcorp/dbot/commands/Music.kt index 6094617..eb640e7 100644 --- a/src/main/kotlin/nl/voidcorp/dbot/commands/Music.kt +++ b/src/main/kotlin/nl/voidcorp/dbot/commands/Music.kt @@ -1,6 +1,5 @@ package nl.voidcorp.dbot.commands -import com.jagrosh.jdautilities.command.CommandBuilder import com.jagrosh.jdautilities.command.CommandEvent import com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler import com.sedmelluq.discord.lavaplayer.player.AudioPlayer @@ -23,26 +22,16 @@ fun initMusic() { 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 (!guildMusicMap.containsKey(event.guild.idLong) || (guildMusicMap[event.guild.idLong]!!.player.playingTrack == null)) event.reply("The track list is empty") else 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 { @@ -69,11 +58,40 @@ fun initMusic() { }) } - val ytCommand = CommandBuilder().setName("youtube").addAlias("yt").setHelp("Play a song from YouTube!").build { event -> - queueCommand.run(event) + val ytCommand = UnityMusicCommand("youtube", aliases = *arrayOf("yt"), help = "Search YouTube for a song!") { event, scheduler -> + 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 -> + 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) { @@ -100,7 +118,10 @@ fun initMusic() { } 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 { override fun loadFailed(exception: FriendlyException) { 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() if (attach == null) { event.reply("I can't play an attachment without an attachment...") } 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 { override fun loadFailed(exception: FriendlyException) { diff --git a/src/main/kotlin/nl/voidcorp/dbot/music/TrackScheduler.kt b/src/main/kotlin/nl/voidcorp/dbot/music/TrackScheduler.kt index 0f0d109..6388f73 100644 --- a/src/main/kotlin/nl/voidcorp/dbot/music/TrackScheduler.kt +++ b/src/main/kotlin/nl/voidcorp/dbot/music/TrackScheduler.kt @@ -16,8 +16,6 @@ import nl.voidcorp.dbot.log import java.awt.Color import java.time.LocalDateTime import java.util.* -import kotlin.concurrent.thread -import kotlin.concurrent.timer import kotlin.concurrent.timerTask @@ -146,10 +144,10 @@ class TrackScheduler(val player: AudioPlayer, val guild: Guild, channel: VoiceCh return EmbedBuilder() .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) - .setThumbnail(art) + .setThumbnail(if (art == "null") null else art) .setTimestamp(LocalDateTime.now()).setColor(Color.decode("#ff5500")).build() - }else if (track is HttpAudioTrack){ + } else if (track is HttpAudioTrack) { if (track.userData is Member) return EmbedBuilder() .setFooter("Requested by ${(track.userData as Member).effectiveName}", (track.userData as Member)