1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 19:44:20 +00:00

Don't crash if the same song is played twice

This commit is contained in:
Steffo 2019-09-10 13:37:17 +02:00
parent fd89fd6263
commit 019d45456a
2 changed files with 16 additions and 9 deletions

View file

@ -1,5 +1,6 @@
import typing import typing
import pickle import pickle
import datetime
from ..command import Command from ..command import Command
from ..commandinterface import CommandInterface from ..commandinterface import CommandInterface
from ..commandargs import CommandArgs from ..commandargs import CommandArgs
@ -15,11 +16,6 @@ if typing.TYPE_CHECKING:
class PlayNH(NetworkHandler): class PlayNH(NetworkHandler):
message_type = "music_play" message_type = "music_play"
ytdl_args = {
"format": "bestaudio",
"outtmpl": f"./downloads/%(title)s.%(ext)s"
}
@classmethod @classmethod
async def discord(cls, bot: "DiscordBot", data: dict): async def discord(cls, bot: "DiscordBot", data: dict):
"""Handle a play Royalnet request. That is, add audio to a PlayMode.""" """Handle a play Royalnet request. That is, add audio to a PlayMode."""
@ -36,11 +32,16 @@ class PlayNH(NetworkHandler):
if not bot.music_data.get(guild): if not bot.music_data.get(guild):
# TODO: change Exception # TODO: change Exception
raise Exception("No music_data for this guild") raise Exception("No music_data for this guild")
# Create url
ytdl_args = {
"format": "bestaudio",
"outtmpl": f"./downloads/{datetime.datetime.now().timestamp()}_%(title)s.%(ext)s"
}
# Start downloading # Start downloading
if data["url"].startswith("http://") or data["url"].startswith("https://"): if data["url"].startswith("http://") or data["url"].startswith("https://"):
dfiles: typing.List[YtdlDiscord] = await asyncify(YtdlDiscord.create_and_ready_from_url, data["url"], **cls.ytdl_args) dfiles: typing.List[YtdlDiscord] = await asyncify(YtdlDiscord.create_and_ready_from_url, data["url"], **ytdl_args)
else: else:
dfiles = await asyncify(YtdlDiscord.create_and_ready_from_url, f"ytsearch:{data['url']}", **cls.ytdl_args) dfiles = await asyncify(YtdlDiscord.create_and_ready_from_url, f"ytsearch:{data['url']}", **ytdl_args)
await bot.add_to_music_data(dfiles, guild) await bot.add_to_music_data(dfiles, guild)
# Create response dictionary # Create response dictionary
response = { response = {

View file

@ -1,6 +1,7 @@
import typing import typing
import discord import discord
import asyncio import asyncio
import datetime
from ..command import Command from ..command import Command
from ..commandinterface import CommandInterface from ..commandinterface import CommandInterface
from ..commandargs import CommandArgs from ..commandargs import CommandArgs
@ -37,13 +38,18 @@ class ZawarudoNH(NetworkHandler):
if not bot.music_data.get(guild): if not bot.music_data.get(guild):
# TODO: change Exception # TODO: change Exception
raise Exception("No music_data for this guild") raise Exception("No music_data for this guild")
# Create url
ytdl_args = {
"format": "bestaudio",
"outtmpl": f"./downloads/{datetime.datetime.now().timestamp()}_%(title)s.%(ext)s"
}
# Start downloading # Start downloading
zw_start: typing.List[YtdlDiscord] = await asyncify(YtdlDiscord.create_and_ready_from_url, zw_start: typing.List[YtdlDiscord] = await asyncify(YtdlDiscord.create_and_ready_from_url,
"https://scaleway.steffo.eu/jojo/zawarudo_intro.mp3", "https://scaleway.steffo.eu/jojo/zawarudo_intro.mp3",
**cls.ytdl_args) **ytdl_args)
zw_end: typing.List[YtdlDiscord] = await asyncify(YtdlDiscord.create_and_ready_from_url, zw_end: typing.List[YtdlDiscord] = await asyncify(YtdlDiscord.create_and_ready_from_url,
"https://scaleway.steffo.eu/jojo/zawarudo_outro.mp3", "https://scaleway.steffo.eu/jojo/zawarudo_outro.mp3",
**cls.ytdl_args) **ytdl_args)
old_playlist = bot.music_data[guild] old_playlist = bot.music_data[guild]
bot.music_data[guild] = Playlist() bot.music_data[guild] = Playlist()
# Get voice client # Get voice client