回答編集履歴

1

質問について回答

2020/06/25 09:58

投稿

sanaN
sanaN

スコア38

test CHANGED
@@ -27,3 +27,183 @@
27
27
  自分はBot commands frameworkの書き方に詳しくないので解説はできませんが、
28
28
 
29
29
  このサンプルコードで動かすことはできました。
30
+
31
+
32
+
33
+ 追記(1)
34
+
35
+ 自分がBot commands frameworkを使えないので
36
+
37
+ 全部on_messageで処理します
38
+
39
+ あとこのサンプルソースほぼ内容は上記リンクと一緒で
40
+
41
+ 上記リンクの機能縮小版になります(_p 曲名orURL:joinして再生開始/_dis:ボイスdisconnect)
42
+
43
+ ```python
44
+
45
+ import asyncio
46
+
47
+
48
+
49
+ import discord
50
+
51
+ import youtube_dl
52
+
53
+
54
+
55
+ from discord.ext import commands
56
+
57
+ #この辺は何やってるかわからないけどそのまま引用
58
+
59
+ # Suppress noise about console usage from errors
60
+
61
+ youtube_dl.utils.bug_reports_message = lambda: ''
62
+
63
+
64
+
65
+
66
+
67
+ ytdl_format_options = {
68
+
69
+ 'format': 'bestaudio/best',
70
+
71
+ 'outtmpl': '%(extractor)s-%(id)s-%(title)s.%(ext)s',
72
+
73
+ 'restrictfilenames': True,
74
+
75
+ 'noplaylist': True,
76
+
77
+ 'nocheckcertificate': True,
78
+
79
+ 'ignoreerrors': False,
80
+
81
+ 'logtostderr': False,
82
+
83
+ 'quiet': True,
84
+
85
+ 'no_warnings': True,
86
+
87
+ 'default_search': 'auto',
88
+
89
+ # bind to ipv4 since ipv6 addresses cause issues sometimes
90
+
91
+ 'source_address': '0.0.0.0'
92
+
93
+ }
94
+
95
+
96
+
97
+ ffmpeg_options = {
98
+
99
+ 'options': '-vn'
100
+
101
+ }
102
+
103
+
104
+
105
+ ytdl = youtube_dl.YoutubeDL(ytdl_format_options)
106
+
107
+
108
+
109
+
110
+
111
+ class YTDLSource(discord.PCMVolumeTransformer):
112
+
113
+ def __init__(self, source, *, data, volume=0.5):
114
+
115
+ super().__init__(source, volume)
116
+
117
+
118
+
119
+ self.data = data
120
+
121
+
122
+
123
+ self.title = data.get('title')
124
+
125
+ self.url = data.get('url')
126
+
127
+
128
+
129
+ @classmethod
130
+
131
+ async def from_url(cls, url, *, loop=None, stream=False):
132
+
133
+ loop = loop or asyncio.get_event_loop()
134
+
135
+ data = await loop.run_in_executor(None, lambda: ytdl.extract_info(url, download=not stream))
136
+
137
+
138
+
139
+ if 'entries' in data:
140
+
141
+ # take first item from a playlist
142
+
143
+ data = data['entries'][0]
144
+
145
+
146
+
147
+ filename = data['url'] if stream else ytdl.prepare_filename(data)
148
+
149
+ return cls(discord.FFmpegPCMAudio(filename, **ffmpeg_options), data=data)
150
+
151
+
152
+
153
+
154
+
155
+ bot = commands.Bot(command_prefix=commands.when_mentioned_or("_"),
156
+
157
+ description='Relatively simple music bot example')
158
+
159
+
160
+
161
+
162
+
163
+ @bot.event
164
+
165
+ async def on_ready():
166
+
167
+ print('Logged in as {0} ({0.id})'.format(bot.user))
168
+
169
+ print('------')
170
+
171
+ #この辺は何やってるかわからないけどそのまま引用ここまで
172
+
173
+
174
+
175
+ @bot.event
176
+
177
+ async def on_message(message):
178
+
179
+
180
+
181
+ if message.content.startswith("_p "):
182
+
183
+ await message.author.voice.channel.connect()#vc接続
184
+
185
+ message.content=message.content.replace("_p ","")
186
+
187
+ async with message.channel.typing():
188
+
189
+ player = await YTDLSource.from_url(message.content, loop=asyncio.get_event_loop())#多分再生用の素材を作ってるとこ
190
+
191
+ message.author.guild.voice_client.play(player, after=lambda e: print(
192
+
193
+ 'Player error: %s' % e) if e else None)#再生してるとこ
194
+
195
+ await message.channel.send('Now playing: {}'.format(player.title)) #再生中のタイトルをsend
196
+
197
+
198
+
199
+ if message.content.startswith("_dis"):
200
+
201
+ await message.author.guild.voice_client.disconnect() #VC切断
202
+
203
+
204
+
205
+
206
+
207
+ bot.run("TOKEN")
208
+
209
+ ```