teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

質問について回答

2020/06/25 09:58

投稿

sanaN
sanaN

スコア38

answer CHANGED
@@ -12,4 +12,94 @@
12
12
  [github Rapptz/RoboDanny/discord.py/examples/basic_voice.py](https://github.com/Rapptz/discord.py/blob/master/examples/basic_voice.py)
13
13
 
14
14
  自分はBot commands frameworkの書き方に詳しくないので解説はできませんが、
15
- このサンプルコードで動かすことはできました。
15
+ このサンプルコードで動かすことはできました。
16
+
17
+ 追記(1)
18
+ 自分がBot commands frameworkを使えないので
19
+ 全部on_messageで処理します
20
+ あとこのサンプルソースほぼ内容は上記リンクと一緒で
21
+ 上記リンクの機能縮小版になります(_p 曲名orURL:joinして再生開始/_dis:ボイスdisconnect)
22
+ ```python
23
+ import asyncio
24
+
25
+ import discord
26
+ import youtube_dl
27
+
28
+ from discord.ext import commands
29
+ #この辺は何やってるかわからないけどそのまま引用
30
+ # Suppress noise about console usage from errors
31
+ youtube_dl.utils.bug_reports_message = lambda: ''
32
+
33
+
34
+ ytdl_format_options = {
35
+ 'format': 'bestaudio/best',
36
+ 'outtmpl': '%(extractor)s-%(id)s-%(title)s.%(ext)s',
37
+ 'restrictfilenames': True,
38
+ 'noplaylist': True,
39
+ 'nocheckcertificate': True,
40
+ 'ignoreerrors': False,
41
+ 'logtostderr': False,
42
+ 'quiet': True,
43
+ 'no_warnings': True,
44
+ 'default_search': 'auto',
45
+ # bind to ipv4 since ipv6 addresses cause issues sometimes
46
+ 'source_address': '0.0.0.0'
47
+ }
48
+
49
+ ffmpeg_options = {
50
+ 'options': '-vn'
51
+ }
52
+
53
+ ytdl = youtube_dl.YoutubeDL(ytdl_format_options)
54
+
55
+
56
+ class YTDLSource(discord.PCMVolumeTransformer):
57
+ def __init__(self, source, *, data, volume=0.5):
58
+ super().__init__(source, volume)
59
+
60
+ self.data = data
61
+
62
+ self.title = data.get('title')
63
+ self.url = data.get('url')
64
+
65
+ @classmethod
66
+ async def from_url(cls, url, *, loop=None, stream=False):
67
+ loop = loop or asyncio.get_event_loop()
68
+ data = await loop.run_in_executor(None, lambda: ytdl.extract_info(url, download=not stream))
69
+
70
+ if 'entries' in data:
71
+ # take first item from a playlist
72
+ data = data['entries'][0]
73
+
74
+ filename = data['url'] if stream else ytdl.prepare_filename(data)
75
+ return cls(discord.FFmpegPCMAudio(filename, **ffmpeg_options), data=data)
76
+
77
+
78
+ bot = commands.Bot(command_prefix=commands.when_mentioned_or("_"),
79
+ description='Relatively simple music bot example')
80
+
81
+
82
+ @bot.event
83
+ async def on_ready():
84
+ print('Logged in as {0} ({0.id})'.format(bot.user))
85
+ print('------')
86
+ #この辺は何やってるかわからないけどそのまま引用ここまで
87
+
88
+ @bot.event
89
+ async def on_message(message):
90
+
91
+ if message.content.startswith("_p "):
92
+ await message.author.voice.channel.connect()#vc接続
93
+ message.content=message.content.replace("_p ","")
94
+ async with message.channel.typing():
95
+ player = await YTDLSource.from_url(message.content, loop=asyncio.get_event_loop())#多分再生用の素材を作ってるとこ
96
+ message.author.guild.voice_client.play(player, after=lambda e: print(
97
+ 'Player error: %s' % e) if e else None)#再生してるとこ
98
+ await message.channel.send('Now playing: {}'.format(player.title)) #再生中のタイトルをsend
99
+
100
+ if message.content.startswith("_dis"):
101
+ await message.author.guild.voice_client.disconnect() #VC切断
102
+
103
+
104
+ bot.run("TOKEN")
105
+ ```