musicube/src/bot.py

100 lines
2.7 KiB
Python
Raw Normal View History

2021-09-24 21:05:12 +02:00
import os
2021-09-24 21:36:53 +02:00
import sys
2021-09-25 00:42:36 +02:00
import asyncio
2021-09-25 00:42:36 +02:00
import discord
from discord.ext import commands, tasks
2021-09-24 21:05:12 +02:00
from dotenv import load_dotenv
2021-09-25 00:42:36 +02:00
import youtube_dl
2021-09-24 21:05:12 +02:00
2021-09-24 22:26:49 +02:00
from error import ErrorHandler
2021-09-24 21:17:56 +02:00
load_dotenv()
2021-09-25 00:42:36 +02:00
# Suppress noise about console usage from errors
youtube_dl.utils.bug_reports_message = lambda: ''
class Music(commands.Cog):
def __init__(self, bot):
2021-09-25 02:45:44 +02:00
self._bot = bot
self._queue = asyncio.Queue()
self._queue_lock = asyncio.Lock()
2021-09-25 00:42:36 +02:00
self._ytdl_format_options = {
'format': 'bestaudio/best',
'outtmpl': '%(extractor)s-%(id)s-%(title)s.%(ext)s',
'restrictfilenames': True,
'noplaylist': True,
'nocheckcertificate': True,
'ignoreerrors': False,
'logtostderr': False,
'quiet': True,
'no_warnings': True,
'default_search': 'auto',
'source_address': '0.0.0.0' # bind to ipv4 since ipv6 addresses cause issues sometimes
}
self._ffmpeg_options = {
'options': '-vn'
}
2021-09-25 02:45:44 +02:00
self._ytdl = youtube_dl.YoutubeDL(self._ytdl_format_options)
2021-09-25 00:42:36 +02:00
# pylint: disable=no-member
2021-09-25 02:45:44 +02:00
self._handle_playback.start()
2021-09-25 00:42:36 +02:00
@tasks.loop()
2021-09-25 02:45:44 +02:00
async def _handle_playback(self):
2021-09-25 00:42:36 +02:00
while True:
2021-09-25 02:45:44 +02:00
await self._queue_lock.acquire()
ctx, url, title = await self._queue.get()
2021-09-25 00:42:36 +02:00
audio = discord.FFmpegPCMAudio(url, **self._ffmpeg_options)
def after(err):
if err:
print(f"Player error: {err}")
2021-09-25 02:45:44 +02:00
self._queue.task_done()
self._queue_lock.release()
2021-09-25 00:42:36 +02:00
ctx.voice_client.play(audio, after=after)
await ctx.send(f"Now playing: {title}")
@commands.command()
2021-09-24 23:48:12 +02:00
async def play(self, ctx, *, url):
async with ctx.typing():
2021-09-25 02:45:44 +02:00
data = self._ytdl.extract_info(url, download=False)
2021-09-25 00:42:36 +02:00
if 'entries' in data:
data = data['entries'][0]
title = data.get('title')
url = data.get('url')
2021-09-25 02:45:44 +02:00
await self._queue.put((ctx, url, title))
if ctx.voice_client.is_playing():
await ctx.send(f"Queued: {title}")
@commands.command()
async def stop(self, ctx):
await ctx.voice_client.disconnect()
2021-09-24 23:48:12 +02:00
@play.before_invoke
async def ensure_voice(self, ctx):
if ctx.voice_client is None:
if ctx.author.voice:
await ctx.author.voice.channel.connect()
else:
raise commands.CommandError("Author not connected to a voice channel.")
2021-09-24 22:03:03 +02:00
if __name__ == "__main__":
bot = commands.Bot(command_prefix=commands.when_mentioned_or("!"), description='Relatively simple music bot example')
2021-09-24 22:03:03 +02:00
@bot.event
async def on_ready():
print(f"Logged in as {bot.user} ({bot.user.id})")
print('------')
2021-09-24 22:03:03 +02:00
bot.add_cog(Music(bot))
2021-09-24 22:26:49 +02:00
bot.add_cog(ErrorHandler(bot))
2021-09-24 22:03:03 +02:00
token = os.environ.get("BOT_TOKEN", None)
if not token:
2021-09-24 22:03:28 +02:00
print("No token found in BOT_TOKEN")
2021-09-24 22:03:03 +02:00
sys.exit(1)
2021-09-24 21:05:12 +02:00
2021-09-24 22:03:03 +02:00
bot.run(token)