aboutsummaryrefslogtreecommitdiff
path: root/ambient.py
blob: b0b3d9a3c043bf107714d2410bc2a29c9abf5b34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import discord
from discord.ext import commands
import yt_dlp
import asyncio

TOKEN = ""                  # bot token
PLAYLIST_URL = "https://www.youtube.com/playlist?list=PLuI-iSzcTZFUfVlc9OoZL5LbubQ_T_st9"

intents = discord.Intents.default()
intents.message_content = True
intents.voice_states = True

bot = commands.Bot(command_prefix="!", intents=intents)

ytdl_opts = {
    "format": "bestaudio/best",
    "quiet": True,
    "extract_flat": True,
}

ffmpeg_opts = {
    "options": "-vn"
}

playlist_urls = []

def load_playlist():
    global playlist_urls
    with yt_dlp.YoutubeDL(ytdl_opts) as ydl:
        info = ydl.extract_info(PLAYLIST_URL, download=False)
        playlist_urls = [entry["url"] for entry in info["entries"]]

async def play_loop(vc):
    global playlist_urls
    while True:
        for url in playlist_urls:
            with yt_dlp.YoutubeDL({"format": "bestaudio"}) as ydl:
                info = ydl.extract_info(url, download=False)
                audio_url = info["url"]

            source = discord.FFmpegPCMAudio(audio_url, **ffmpeg_opts)
            vc.play(source)

            while vc.is_playing():
                await asyncio.sleep(1)

@bot.event
async def on_ready():
    print(f"logged in as {bot.user}")

@bot.command()
async def start(ctx):
    if not ctx.author.voice:
        await ctx.send("join a voice channel first")
        return

    channel = ctx.author.voice.channel
    vc = await channel.connect()

    load_playlist()
    await play_loop(vc)

bot.run(TOKEN)