feat: added SeekExtension

Signed-off-by: limited_dev <loginakkisativ@gmail.com>
This commit is contained in:
limited_dev 2023-07-23 23:43:47 +02:00
parent d8f8231028
commit aabab047f7

View file

@ -0,0 +1,152 @@
/*
* Botendo
* Copyright (C) 2023 moonleay
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
package net.moonleay.botendo.extensions.music
import com.kotlindiscord.kord.extensions.extensions.Extension
import com.kotlindiscord.kord.extensions.extensions.publicSlashCommand
import com.kotlindiscord.kord.extensions.types.respond
import dev.kord.common.Color
import dev.kord.rest.builder.message.create.actionRow
import dev.schlaubi.lavakord.audio.Link
import net.moonleay.botendo.extensions.music.components.MusicManager
import net.moonleay.botendo.extensions.music.components.SeekArguments
import net.moonleay.botendo.util.ButtonUtil
import net.moonleay.botendo.util.MessageUtil
import net.moonleay.botendo.util.TimeUtil
import net.moonleay.botendo.util.UrlUtil
class SeekExtension : Extension() {
override val name = "seek"
override suspend fun setup() {
publicSlashCommand(::SeekArguments) {
name = "seek"
description = "Skip to a certain point in a track"
this.action {
val guildId = this.guild!!.id
val link = net.moonleay.botendo.Bot.lava.getLink(guildId.toString())
val player = link.player
val u = this.user
val voiceState = u.asMember(guildId).getVoiceStateOrNull()
if (voiceState == null) {
this.respond {
this.embeds.add(
MessageUtil.getEmbed(
Color(0xE0311A),
"You are not connected to a VC",
"Please connect to a VC",
"${u.asUser().username}#${u.asUser().discriminator}"
)
)
}
return@action
}
val channelId = voiceState.channelId
if (link.state == Link.State.NOT_CONNECTED) {
this.respond {
this.embeds.add(
MessageUtil.getEmbed(
Color(0xE0311A),
"Not connected",
"I'm not in a VC and therefore, I am not playing anything.",
"${u.asUser().username}#${u.asUser().discriminator}"
)
)
}
return@action
} else if (link.state == Link.State.CONNECTED && link.lastChannelId != channelId!!.value) {
this.respond {
this.embeds.add(
MessageUtil.getEmbed(
Color(0xE0311A),
"You are not in my VC",
"We are not in the same VC and therefore, you cannot control the music",
"${u.asUser().username}#${u.asUser().discriminator}"
)
)
}
return@action
}
var track = player.playingTrack
if (track == null) {
this.respond {
this.embeds.add(
MessageUtil.getEmbed(
Color(0xE0311A),
"Not playing",
"I'm not playing anything currently",
"${u.asUser().username}#${u.asUser().discriminator}"
)
)
}
return@action
}
val gts = MusicManager.getGuildTrackScheduler(this.guild!!.asGuild(), player)
val targetPos = TimeUtil.getTimeUnformated(this.arguments.position)
if (player.playingTrack!!.length.inWholeMilliseconds > targetPos && targetPos >= 0) {
player.seekTo(targetPos)
} else {
this.respond {
this.embeds.add(
MessageUtil.getEmbed(
Color(0xE0311A),
"Invalid position",
"The position you specified is invalid",
"${u.asUser().username}#${u.asUser().discriminator}"
)
)
}
return@action
}
this.respond {
this.embeds.add(
MessageUtil.getEmbedWithImage(
Color(0x52E01A),
"Jumped; now playing",
"**${track.title}**\n*Now Playing*\nby ${track.author} ; ${
TimeUtil.getTimeFormatedRaw(
player.position
)
}: ${
TimeUtil.getTimeFormatedRaw(
track.length.inWholeMilliseconds
)
}\n" +
">>>${track.uri}",
user.asUser().username + "#" + user.asUser().discriminator,
"https://img.youtube.com/vi/" + UrlUtil.getYtThumbnailUrl(track.uri!!) + "/maxresdefault.jpg"
)
)
this.actionRow {
this.components.addAll(
ButtonUtil.getMusicControllerButtons(
player.paused,
gts.repeating
).components
)
}
}
}
}
}
}