mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-21 10:17:59 -05:00
Merge V3/feature/audio into V3/develop (a.k.a. audio refactor) (#3459)
This commit is contained in:
@@ -1,336 +1,10 @@
|
||||
import asyncio
|
||||
import contextlib
|
||||
import functools
|
||||
import re
|
||||
import tarfile
|
||||
import time
|
||||
import zipfile
|
||||
from enum import Enum, unique
|
||||
from io import BytesIO
|
||||
from typing import MutableMapping, Optional, TYPE_CHECKING
|
||||
from urllib.parse import urlparse
|
||||
from typing import MutableMapping
|
||||
|
||||
import discord
|
||||
import lavalink
|
||||
|
||||
from redbot.core import Config, commands
|
||||
from redbot.core.bot import Red
|
||||
from redbot.core.i18n import Translator
|
||||
from redbot.core.utils.chat_formatting import bold, box
|
||||
from discord.utils import escape_markdown as escape
|
||||
|
||||
from .audio_dataclasses import Query
|
||||
|
||||
__all__ = [
|
||||
"_pass_config_to_utils",
|
||||
"track_limit",
|
||||
"queue_duration",
|
||||
"draw_time",
|
||||
"dynamic_time",
|
||||
"match_url",
|
||||
"clear_react",
|
||||
"match_yt_playlist",
|
||||
"remove_react",
|
||||
"get_track_description",
|
||||
"track_creator",
|
||||
"time_convert",
|
||||
"url_check",
|
||||
"userlimit",
|
||||
"is_allowed",
|
||||
"track_to_json",
|
||||
"rgetattr",
|
||||
"humanize_scope",
|
||||
"CacheLevel",
|
||||
"format_playlist_picker_data",
|
||||
"get_track_description_unformatted",
|
||||
"track_remaining_duration",
|
||||
"Notifier",
|
||||
"PlaylistScope",
|
||||
]
|
||||
_RE_TIME_CONVERTER = re.compile(r"(?:(\d+):)?([0-5]?[0-9]):([0-5][0-9])")
|
||||
_RE_YT_LIST_PLAYLIST = re.compile(
|
||||
r"^(https?://)?(www\.)?(youtube\.com|youtu\.?be)(/playlist\?).*(list=)(.*)(&|$)"
|
||||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
_config: Config
|
||||
_bot: Red
|
||||
else:
|
||||
_config = None
|
||||
_bot = None
|
||||
|
||||
_ = Translator("Audio", __file__)
|
||||
|
||||
|
||||
def _pass_config_to_utils(config: Config, bot: Red) -> None:
|
||||
global _config, _bot
|
||||
if _config is None:
|
||||
_config = config
|
||||
if _bot is None:
|
||||
_bot = bot
|
||||
|
||||
|
||||
def track_limit(track, maxlength) -> bool:
|
||||
try:
|
||||
length = round(track.length / 1000)
|
||||
except AttributeError:
|
||||
length = round(track / 1000)
|
||||
|
||||
if maxlength < length <= 900000000000000: # livestreams return 9223372036854775807ms
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
async def is_allowed(guild: discord.Guild, query: str, query_obj: Query = None) -> bool:
|
||||
|
||||
query = query.lower().strip()
|
||||
if query_obj is not None:
|
||||
query = query_obj.lavalink_query.replace("ytsearch:", "youtubesearch").replace(
|
||||
"scsearch:", "soundcloudsearch"
|
||||
)
|
||||
global_whitelist = set(await _config.url_keyword_whitelist())
|
||||
global_whitelist = [i.lower() for i in global_whitelist]
|
||||
if global_whitelist:
|
||||
return any(i in query for i in global_whitelist)
|
||||
global_blacklist = set(await _config.url_keyword_blacklist())
|
||||
global_blacklist = [i.lower() for i in global_blacklist]
|
||||
if any(i in query for i in global_blacklist):
|
||||
return False
|
||||
if guild is not None:
|
||||
whitelist = set(await _config.guild(guild).url_keyword_whitelist())
|
||||
whitelist = [i.lower() for i in whitelist]
|
||||
if whitelist:
|
||||
return any(i in query for i in whitelist)
|
||||
blacklist = set(await _config.guild(guild).url_keyword_blacklist())
|
||||
blacklist = [i.lower() for i in blacklist]
|
||||
return not any(i in query for i in blacklist)
|
||||
return True
|
||||
|
||||
|
||||
async def queue_duration(ctx) -> int:
|
||||
player = lavalink.get_player(ctx.guild.id)
|
||||
duration = []
|
||||
for i in range(len(player.queue)):
|
||||
if not player.queue[i].is_stream:
|
||||
duration.append(player.queue[i].length)
|
||||
queue_dur = sum(duration)
|
||||
if not player.queue:
|
||||
queue_dur = 0
|
||||
try:
|
||||
if not player.current.is_stream:
|
||||
remain = player.current.length - player.position
|
||||
else:
|
||||
remain = 0
|
||||
except AttributeError:
|
||||
remain = 0
|
||||
queue_total_duration = remain + queue_dur
|
||||
return queue_total_duration
|
||||
|
||||
|
||||
async def track_remaining_duration(ctx) -> int:
|
||||
player = lavalink.get_player(ctx.guild.id)
|
||||
if not player.current:
|
||||
return 0
|
||||
try:
|
||||
if not player.current.is_stream:
|
||||
remain = player.current.length - player.position
|
||||
else:
|
||||
remain = 0
|
||||
except AttributeError:
|
||||
remain = 0
|
||||
return remain
|
||||
|
||||
|
||||
async def draw_time(ctx) -> str:
|
||||
player = lavalink.get_player(ctx.guild.id)
|
||||
paused = player.paused
|
||||
pos = player.position
|
||||
dur = player.current.length
|
||||
sections = 12
|
||||
loc_time = round((pos / dur) * sections)
|
||||
bar = "\N{BOX DRAWINGS HEAVY HORIZONTAL}"
|
||||
seek = "\N{RADIO BUTTON}"
|
||||
if paused:
|
||||
msg = "\N{DOUBLE VERTICAL BAR}"
|
||||
else:
|
||||
msg = "\N{BLACK RIGHT-POINTING TRIANGLE}"
|
||||
for i in range(sections):
|
||||
if i == loc_time:
|
||||
msg += seek
|
||||
else:
|
||||
msg += bar
|
||||
return msg
|
||||
|
||||
|
||||
def dynamic_time(seconds) -> str:
|
||||
m, s = divmod(seconds, 60)
|
||||
h, m = divmod(m, 60)
|
||||
d, h = divmod(h, 24)
|
||||
|
||||
if d > 0:
|
||||
msg = "{0}d {1}h"
|
||||
elif d == 0 and h > 0:
|
||||
msg = "{1}h {2}m"
|
||||
elif d == 0 and h == 0 and m > 0:
|
||||
msg = "{2}m {3}s"
|
||||
elif d == 0 and h == 0 and m == 0 and s > 0:
|
||||
msg = "{3}s"
|
||||
else:
|
||||
msg = ""
|
||||
return msg.format(d, h, m, s)
|
||||
|
||||
|
||||
def format_playlist_picker_data(pid, pname, ptracks, pauthor, scope) -> str:
|
||||
author = _bot.get_user(pauthor) or pauthor or _("Unknown")
|
||||
line = _(
|
||||
" - Name: <{pname}>\n"
|
||||
" - Scope: < {scope} >\n"
|
||||
" - ID: < {pid} >\n"
|
||||
" - Tracks: < {ptracks} >\n"
|
||||
" - Author: < {author} >\n\n"
|
||||
).format(pname=pname, scope=humanize_scope(scope), pid=pid, ptracks=ptracks, author=author)
|
||||
return box(line, lang="md")
|
||||
|
||||
|
||||
def match_url(url) -> bool:
|
||||
try:
|
||||
query_url = urlparse(url)
|
||||
return all([query_url.scheme, query_url.netloc, query_url.path])
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
def match_yt_playlist(url) -> bool:
|
||||
if _RE_YT_LIST_PLAYLIST.match(url):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
async def remove_react(message, react_emoji, react_user) -> None:
|
||||
with contextlib.suppress(discord.HTTPException):
|
||||
await message.remove_reaction(react_emoji, react_user)
|
||||
|
||||
|
||||
async def clear_react(bot: Red, message: discord.Message, emoji: MutableMapping = None) -> None:
|
||||
try:
|
||||
await message.clear_reactions()
|
||||
except discord.Forbidden:
|
||||
if not emoji:
|
||||
return
|
||||
with contextlib.suppress(discord.HTTPException):
|
||||
for key in emoji.values():
|
||||
await asyncio.sleep(0.2)
|
||||
await message.remove_reaction(key, bot.user)
|
||||
except discord.HTTPException:
|
||||
return
|
||||
|
||||
|
||||
def get_track_description(track) -> Optional[str]:
|
||||
if track and getattr(track, "uri", None):
|
||||
query = Query.process_input(track.uri)
|
||||
if query.is_local or "localtracks/" in track.uri:
|
||||
if track.title != "Unknown title":
|
||||
return f'**{escape(f"{track.author} - {track.title}")}**' + escape(
|
||||
f"\n{query.to_string_user()} "
|
||||
)
|
||||
else:
|
||||
return escape(query.to_string_user())
|
||||
else:
|
||||
return f'**{escape(f"[{track.title}]({track.uri}) ")}**'
|
||||
elif hasattr(track, "to_string_user") and track.is_local:
|
||||
return escape(track.to_string_user() + " ")
|
||||
|
||||
|
||||
def get_track_description_unformatted(track) -> Optional[str]:
|
||||
if track and hasattr(track, "uri"):
|
||||
query = Query.process_input(track.uri)
|
||||
if query.is_local or "localtracks/" in track.uri:
|
||||
if track.title != "Unknown title":
|
||||
return escape(f"{track.author} - {track.title}")
|
||||
else:
|
||||
return escape(query.to_string_user())
|
||||
else:
|
||||
return escape(f"{track.title}")
|
||||
elif hasattr(track, "to_string_user") and track.is_local:
|
||||
return escape(track.to_string_user() + " ")
|
||||
|
||||
|
||||
def track_creator(player, position=None, other_track=None) -> MutableMapping:
|
||||
if position == "np":
|
||||
queued_track = player.current
|
||||
elif position is None:
|
||||
queued_track = other_track
|
||||
else:
|
||||
queued_track = player.queue[position]
|
||||
return track_to_json(queued_track)
|
||||
|
||||
|
||||
def track_to_json(track: lavalink.Track) -> MutableMapping:
|
||||
track_keys = track._info.keys()
|
||||
track_values = track._info.values()
|
||||
track_id = track.track_identifier
|
||||
track_info = {}
|
||||
for k, v in zip(track_keys, track_values):
|
||||
track_info[k] = v
|
||||
keys = ["track", "info"]
|
||||
values = [track_id, track_info]
|
||||
track_obj = {}
|
||||
for key, value in zip(keys, values):
|
||||
track_obj[key] = value
|
||||
return track_obj
|
||||
|
||||
|
||||
def time_convert(length) -> int:
|
||||
match = _RE_TIME_CONVERTER.match(length)
|
||||
if match is not None:
|
||||
hr = int(match.group(1)) if match.group(1) else 0
|
||||
mn = int(match.group(2)) if match.group(2) else 0
|
||||
sec = int(match.group(3)) if match.group(3) else 0
|
||||
pos = sec + (mn * 60) + (hr * 3600)
|
||||
return pos
|
||||
else:
|
||||
try:
|
||||
return int(length)
|
||||
except ValueError:
|
||||
return 0
|
||||
|
||||
|
||||
def url_check(url) -> bool:
|
||||
valid_tld = [
|
||||
"youtube.com",
|
||||
"youtu.be",
|
||||
"soundcloud.com",
|
||||
"bandcamp.com",
|
||||
"vimeo.com",
|
||||
"beam.pro",
|
||||
"mixer.com",
|
||||
"twitch.tv",
|
||||
"spotify.com",
|
||||
"localtracks",
|
||||
]
|
||||
query_url = urlparse(url)
|
||||
url_domain = ".".join(query_url.netloc.split(".")[-2:])
|
||||
if not query_url.netloc:
|
||||
url_domain = ".".join(query_url.path.split("/")[0].split(".")[-2:])
|
||||
return True if url_domain in valid_tld else False
|
||||
|
||||
|
||||
def userlimit(channel) -> bool:
|
||||
if channel.user_limit == 0 or channel.user_limit > len(channel.members) + 1:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def rsetattr(obj, attr, val):
|
||||
pre, _, post = attr.rpartition(".")
|
||||
return setattr(rgetattr(obj, pre) if pre else obj, post, val)
|
||||
|
||||
|
||||
def rgetattr(obj, attr, *args):
|
||||
def _getattr(obj2, attr2):
|
||||
return getattr(obj2, attr2, *args)
|
||||
|
||||
return functools.reduce(_getattr, [obj] + attr.split("."))
|
||||
from redbot.core import commands
|
||||
|
||||
|
||||
class CacheLevel:
|
||||
@@ -494,13 +168,13 @@ class Notifier:
|
||||
self.color = await self.context.embed_colour()
|
||||
embed2 = discord.Embed(
|
||||
colour=self.color,
|
||||
title=self.updates.get(key).format(num=current, total=total, seconds=seconds),
|
||||
title=self.updates.get(key, "").format(num=current, total=total, seconds=seconds),
|
||||
)
|
||||
if seconds and seconds_key:
|
||||
embed2.set_footer(text=self.updates.get(seconds_key).format(seconds=seconds))
|
||||
embed2.set_footer(text=self.updates.get(seconds_key, "").format(seconds=seconds))
|
||||
try:
|
||||
await self.message.edit(embed=embed2)
|
||||
self.last_msg_time = time.time()
|
||||
self.last_msg_time = int(time.time())
|
||||
except discord.errors.NotFound:
|
||||
pass
|
||||
|
||||
@@ -514,7 +188,7 @@ class Notifier:
|
||||
async def update_embed(self, embed: discord.Embed):
|
||||
try:
|
||||
await self.message.edit(embed=embed)
|
||||
self.last_msg_time = time.time()
|
||||
self.last_msg_time = int(time.time())
|
||||
except discord.errors.NotFound:
|
||||
pass
|
||||
|
||||
@@ -531,13 +205,3 @@ class PlaylistScope(Enum):
|
||||
@staticmethod
|
||||
def list():
|
||||
return list(map(lambda c: c.value, PlaylistScope))
|
||||
|
||||
|
||||
def humanize_scope(scope, ctx=None, the=None):
|
||||
|
||||
if scope == PlaylistScope.GLOBAL.value:
|
||||
return (_("the ") if the else "") + _("Global")
|
||||
elif scope == PlaylistScope.GUILD.value:
|
||||
return ctx.name if ctx else (_("the ") if the else "") + _("Server")
|
||||
elif scope == PlaylistScope.USER.value:
|
||||
return str(ctx) if ctx else (_("the ") if the else "") + _("User")
|
||||
|
||||
Reference in New Issue
Block a user