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

Bot is finally working!

This commit is contained in:
Steffo 2019-04-17 18:53:53 +02:00
parent 538a850344
commit 3b9843118d
3 changed files with 18 additions and 12 deletions

View file

@ -12,6 +12,7 @@ loop = asyncio.get_event_loop()
log = logging.root log = logging.root
log.addHandler(logging.StreamHandler()) log.addHandler(logging.StreamHandler())
logging.getLogger("royalnet.audio.royalaudiofile").setLevel(logging.DEBUG)
logging.getLogger("royalnet.bots.discord").setLevel(logging.DEBUG) logging.getLogger("royalnet.bots.discord").setLevel(logging.DEBUG)
commands = [PingCommand, ShipCommand, SmecdsCommand, ColorCommand, CiaoruoziCommand, DebugCreateCommand, SyncCommand, commands = [PingCommand, ShipCommand, SmecdsCommand, ColorCommand, CiaoruoziCommand, DebugCreateCommand, SyncCommand,

View file

@ -6,7 +6,6 @@ import typing
import logging as _logging import logging as _logging
from .youtubedl import YtdlFile, YtdlInfo from .youtubedl import YtdlFile, YtdlInfo
log = _logging.getLogger(__name__) log = _logging.getLogger(__name__)
@ -20,16 +19,15 @@ class RoyalAudioFile(YtdlFile):
# Overwrite the new ytdl_args # Overwrite the new ytdl_args
self.ytdl_args = {**self.ytdl_args, **ytdl_args} self.ytdl_args = {**self.ytdl_args, **ytdl_args}
log.info(f"Now downloading {info.webpage_url}") log.info(f"Now downloading {info.webpage_url}")
super().__init__(info, outtmpl="./opusfiles/%(title)s-%(id)s.%(ext)s", **self.ytdl_args) super().__init__(info, outtmpl="./downloads/%(title)s-%(id)s.ytdl", **self.ytdl_args)
# Find the audio_filename with a regex (should be video.opus) # Find the audio_filename with a regex (should be video.opus)
log.info(f"Starting conversion of {self.video_filename}") log.info(f"Preparing {self.video_filename}...")
self.audio_filename = re.sub(rf"\.{self.info.ext}$", ".opus", self.video_filename)
# Convert the video to opus # Convert the video to opus
# Actually not needed, but we do this anyways for compression reasons # Actually not needed, but we do this anyways for compression reasons
converter = ffmpeg.input(self.video_filename) \ ffmpeg.input(self.video_filename) \
.output(self.audio_filename) .output(self.audio_filename, format="s16le", acodec="pcm_s16le", ac=2, ar="48000") \
converter.run(quiet=True) .overwrite_output() \
log.info(f"Converted to {self.audio_filename}") .run(quiet=not __debug__)
# Delete the video file # Delete the video file
log.info(f"Deleting {self.video_filename}") log.info(f"Deleting {self.video_filename}")
self.delete_video_file() self.delete_video_file()
@ -39,9 +37,15 @@ class RoyalAudioFile(YtdlFile):
info_list = YtdlInfo.create_from_url(url) info_list = YtdlInfo.create_from_url(url)
return [RoyalAudioFile(info) for info in info_list] return [RoyalAudioFile(info) for info in info_list]
@property
def audio_filename(self):
return f"./downloads/{self.info.title}-{self.info.id}.pcm"
def as_audio_source(self):
# TODO: find a way to close this
file = open(self.audio_filename, "rb")
return discord.PCMAudio(file)
def delete_audio_file(self): def delete_audio_file(self):
# TODO: _might_ be unsafe, test this # TODO: _might_ be unsafe, test this
os.remove(self.audio_filename) os.remove(self.audio_filename)
def as_audio_source(self):
return discord.FFmpegPCMAudio(f"./{self.audio_filename}")

View file

@ -213,6 +213,7 @@ class DiscordBot:
async def network_handler(self, message: Message) -> Message: async def network_handler(self, message: Message) -> Message:
"""Handle a Royalnet request.""" """Handle a Royalnet request."""
log.debug(f"Received a {message.__class__.__name__}")
if isinstance(message, SummonMessage): if isinstance(message, SummonMessage):
return await self.nh_summon(message) return await self.nh_summon(message)
elif isinstance(message, PlayMessage): elif isinstance(message, PlayMessage):
@ -246,7 +247,7 @@ class DiscordBot:
def advance(error=None): def advance(error=None):
loop.create_task(self.advance_music_data(guild)) loop.create_task(self.advance_music_data(guild))
log.info(f"Starting to play {next_file.audio_filename}") log.info(f"Starting playback of {next_file.info.title}")
voice_client.play(next_file.as_audio_source(), after=advance) voice_client.play(next_file.as_audio_source(), after=advance)
async def nh_play(self, message: PlayMessage): async def nh_play(self, message: PlayMessage):