From 94ab3c716b7ecef35faad617504185ff92962694 Mon Sep 17 00:00:00 2001 From: ionburger Date: Tue, 24 Jan 2023 21:07:47 -0700 Subject: [PATCH] working on voice --- py/bot.py | 5 ++-- py/cogs/quotequeue.py | 15 ++++++---- py/cogs/voice.py | 70 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 8 deletions(-) create mode 100644 py/cogs/voice.py diff --git a/py/bot.py b/py/bot.py index 74e2f25..9b6fd4e 100644 --- a/py/bot.py +++ b/py/bot.py @@ -10,7 +10,7 @@ log = logging.getLogger(__name__) #setup intents = discord.Intents.all() -bot = bridge.AutoShardedBot(shard_count=4,shard_ids=[2,3],intents=intents,command_prefix=".") +bot = bridge.Bot(intents=intents,command_prefix=".") config = configparser.ConfigParser() config.read("config/config.conf") bot.db = MongoClient(config["mongodb"]["host"],int(config["mongodb"]["port"]),username=config["mongodb"]["username"],password=config["mongodb"]["password"])['data'] @@ -20,7 +20,8 @@ bot.load_extension("cogs.counting") bot.load_extension("cogs.misc") bot.load_extension("cogs.triggers") bot.load_extension("cogs.dad") -bot.load_extension("cogs.quotequeue") +bot.load_extension("cogs.voice") +#bot.load_extension("cogs.quotequeue") #logging diff --git a/py/cogs/quotequeue.py b/py/cogs/quotequeue.py index 95e8ea8..a2455c8 100644 --- a/py/cogs/quotequeue.py +++ b/py/cogs/quotequeue.py @@ -2,25 +2,28 @@ import discord from discord.ext import commands, tasks import datetime +from zoneinfo import ZoneInfo from bin.storage import Config - +times = [datetime.time(12,0,tzinfo=ZoneInfo("America/Denver"))] class Quotequeue(commands.Cog): def __init__(self, bot): self.bot = bot self.db = bot.db - self.quotequeue.start() + self.quotetime.start() + print(self.quotetime.next_iteration) - @tasks.loop(seconds=5) - async def quotequeue(self): + @tasks.loop(time=times) + async def quotetime(self): channel = self.bot.get_channel(1004178748205187086) await channel.send("test") - @quotequeue.before_loop - async def before_quotequeue(self): + @quotetime.before_loop + async def before_quotetime(self): await self.bot.wait_until_ready() + print(self.quotetime.next_iteration) def setup(bot): bot.add_cog(Quotequeue(bot)) diff --git a/py/cogs/voice.py b/py/cogs/voice.py new file mode 100644 index 0000000..b7e190d --- /dev/null +++ b/py/cogs/voice.py @@ -0,0 +1,70 @@ +import discord +from discord.ext import commands +from yt_dlp import YoutubeDL +from bin.storage import Config + +class Voice(commands.Cog): + def __init__(self, bot): + self.bot = bot + self.queue = [] + + def player(self, ctx): + print(self.queue) + if len(self.queue) == 0: + return + self.queue.pop(0) + ffmpeg_options = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'} + ctx.voice_client.play(discord.FFmpegPCMAudio(self.queue[0], **ffmpeg_options), after=Voice.player(self, ctx)) + + + @commands.command() + async def join(self, ctx): + if ctx.author.voice is None: + await ctx.respond("You are not in a voice channel") + else: + channel = ctx.author.voice.channel + await channel.connect() + + @commands.command() + async def leave(self, ctx): + if ctx.voice_client is None: + await ctx.respond("I am not in a voice channel") + else: + await ctx.voice_client.disconnect() + + @commands.command() + async def play(self, ctx, link): + if ctx.author.voice is None: + await ctx.respond("You are not in a voice channel") + if ctx.voice_client is None: + channel = ctx.author.voice.channel + await channel.connect() + ffmpeg_options = { + 'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', + 'options': '-vn' + } + 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', + } + with YoutubeDL(ytdl_format_options) as ydl: + info_dict = ydl.extract_info(f"ytsearch:{link}", download=False)["entries"][0] + video_url = info_dict.get("url", None) + video_id = info_dict.get("id", None) + video_title = info_dict.get('title', None) + self.queue.append(video_url) + if not ctx.voice_client.is_playing(): + ctx.voice_client.play(discord.FFmpegPCMAudio(self.queue[0], **ffmpeg_options), after=Voice.player(self, ctx)) + + +def setup(bot): + bot.add_cog(Voice(bot))