前提
pythonでdiscordの音楽botを作成中です。
youtubeのURLをコマンドとして受付け(入力のイベント)、ダウンロード後に再生するというものです。
現在は再生中ですと、何もせずreturnするようになっております。
実現したいこと
再生中にURLが入力された場合にはそのURLをlistか何かで保存して、
再生終了(再生中ではない場合)時にlist[0]をダウンロードし、list[0]を削除し...とキューの実装をしたいです。
ご質問させていただきたいのは、大掛かりなコードの変更をせずに実装が可能か(現在がキューに対応した作りになってるのか。)
と何を使えばいいのかを教えていただきたいです。
発生している問題・エラーメッセージ
メッセージのイベントとしてURLを受け付けて、ダウンロードし、再生という仕組みに現在なっているので
URLを裏に保存後、再生処理にどうやって入ればよいのかわかりません。
URLコマンド受付
↓
キューに追加
↓
再生中なら待機、再生中でなくなったらキューから取り出す
みたいな実装をしたいです。
該当のソースコード
python
1# インストールした discord.py を読み込む 2import asyncio 3import discord 4# 定数を読み込む 5import constant 6import os 7import random 8from urllib.parse import urlparse 9from discord.ext import tasks 10from datetime import datetime 11import youtube_dl 12 13# Suppress noise about console usage from errors 14youtube_dl.utils.bug_reports_message = lambda: '' 15 16ytdl_format_options = { 17 'format': 'bestaudio/best', 18 'outtmpl': '%(extractor)s-%(id)s-%(title)s.%(ext)s', 19 'restrictfilenames': True, 20 'noplaylist': True, 21 'nocheckcertificate': True, 22 'ignoreerrors': False, 23 'logtostderr': False, 24 'quiet': True, 25 'no_warnings': True, 26 'default_search': 'auto', 27 'source_address': '0.0.0.0' # bind to ipv4 since ipv6 addresses cause issues sometimes 28} 29 30ffmpeg_options = { 31 'options': '-vn' 32} 33 34ytdl = youtube_dl.YoutubeDL(ytdl_format_options) 35 36 37class YTDLSource(discord.PCMVolumeTransformer): 38 def __init__(self, source, *, data, volume=0.5): 39 super().__init__(source, volume) 40 41 self.data = data 42 43 self.title = data.get('title') 44 self.url = data.get('url') 45 46 @classmethod 47 async def from_url(cls, url, *, loop=None, stream=False): 48 loop = loop or asyncio.get_event_loop() 49 data = await loop.run_in_executor(None, lambda: ytdl.extract_info(url, download=not stream)) 50 51 if 'entries' in data: 52 # take first item from a playlist 53 data = data['entries'][0] 54 55 filename = data['url'] if stream else ytdl.prepare_filename(data) 56 return cls(discord.FFmpegPCMAudio(filename, **ffmpeg_options), data=data) 57 58 59# 自分のBotのアクセストークンに置き換えてください 60TOKEN = constant.TOKEN 61 62# 接続に必要なオブジェクトを生成 63client = discord.Client() 64# メッセージ受信時に動作する処理 65@client.event 66async def on_message(message): 67 if message.content == "!join": 68 if message.author.voice is None: 69 await message.channel.send("あなたはボイスチャンネルに接続していません。") 70 return 71 # ボイスチャンネルに接続する 72 await message.author.voice.channel.connect() 73 await message.channel.send("接続しました。") 74 elif message.content.startswith("!play "): 75 print("test") 76 if message.guild.voice_client is None: 77 await message.channel.send("接続していません。") 78 return 79 80 # 再生中の場合は再生しない 81 if message.guild.voice_client.is_playing(): 82 await message.channel.send("再生中です。") 83 return 84 85 url = message.content[6:] 86 # youtubeから音楽をダウンロードする 87 # TODO :TypeError: object NoneType can't be used in 'await' expression 88 await message.channel.send("DL now") 89 player = await YTDLSource.from_url(url, loop=client.loop) 90 91 # 再生する 92 await message.guild.voice_client.play(player) 93 94 await message.channel.send('{} を再生します。'.format(player.title))
試したこと
こちらが参考になるかと思いますが、難しくてよくわかりませんでした。
https://sigmoid-poke.hatenablog.com/entry/make_bot_bgm7
asyncio.Queue()でなんとか解決できるのではないかと思い質問させていただきました。
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2022/09/05 12:29