Files
TuneBot/cogs/music/track_embed.py
T
2022-11-13 18:55:42 +01:00

61 lines
1.7 KiB
Python

import datetime
import typing
import discord
from bot import colors
from cogs.music import helper
if typing.TYPE_CHECKING:
from lavalink.models import AudioTrack
def format_track(track: "AudioTrack", max_length: int = 0):
try:
duration = str(datetime.timedelta(milliseconds=int(track.duration)))
except OverflowError:
duration = "0:00:00"
if max_length == 0:
return f"`{duration}` [{track.title}]({track.uri})"
max_length -= len(duration) + 1
if len(track.title) > max_length:
track_title = track.title[: max_length - 3] + "..."
else:
track_title = track.title
return f"`{duration}` [{track_title}]({track.uri})"
def create_track_embed(
current: "AudioTrack",
queue: typing.Sequence["AudioTrack"],
history: typing.Sequence["AudioTrack"],
) -> discord.Embed:
embed_width = 52
embed = discord.Embed(
title="Now playing...",
colour=colors["embed"],
)
embed.description = format_track(current, max_length=embed_width)
if len(queue) > 0:
upcoming_fmt = "\n".join(
format_track(track, max_length=embed_width) for track in queue
)
else:
upcoming_fmt = "No tracks have been queued yet..."
embed.add_field(name="Coming up", value=upcoming_fmt, inline=False)
if len(history) > 0:
history_fmt = "\n".join(
format_track(track, max_length=embed_width) for track in history
)
else:
history_fmt = "No history yet..."
embed.add_field(name="Previously played", value=history_fmt, inline=False)
embed.set_image(url=f"https://i3.ytimg.com/vi/{current.identifier}/mqdefault.jpg")
embed.set_footer(text=f"Uploaded by: {current.author}")
return embed