前提・実現したいこと
pythonの音楽Botテンプレを改良してもらいたいです
該当のソースコード
python
1import asyncio 2 3import discord 4import youtube_dl 5 6from discord.ext import commands 7 8# Suppress noise about console usage from errors 9youtube_dl.utils.bug_reports_message = lambda: '' 10 11 12ytdl_format_options = { 13 'format': 'bestaudio/best', 14 'outtmpl': '%(extractor)s-%(id)s-%(title)s.%(ext)s', 15 'restrictfilenames': True, 16 'noplaylist': True, 17 'nocheckcertificate': True, 18 'ignoreerrors': False, 19 'logtostderr': False, 20 'quiet': True, 21 'no_warnings': True, 22 'default_search': 'auto', 23 'source_address': '0.0.0.0' # bind to ipv4 since ipv6 addresses cause issues sometimes 24} 25 26ffmpeg_options = { 27 'options': '-vn' 28} 29 30ytdl = youtube_dl.YoutubeDL(ytdl_format_options) 31 32 33class YTDLSource(discord.PCMVolumeTransformer): 34 def __init__(self, source, *, data, volume=0.5): 35 super().__init__(source, volume) 36 37 self.data = data 38 39 self.title = data.get('title') 40 self.url = data.get('url') 41 42 @classmethod 43 async def from_url(cls, url, *, loop=None, stream=False): 44 loop = loop or asyncio.get_event_loop() 45 data = await loop.run_in_executor(None, lambda: ytdl.extract_info(url, download=not stream)) 46 47 if 'entries' in data: 48 # take first item from a playlist 49 data = data['entries'][0] 50 51 filename = data['url'] if stream else ytdl.prepare_filename(data) 52 return cls(discord.FFmpegPCMAudio(filename, **ffmpeg_options), data=data) 53 54 55class Music(commands.Cog): 56 def __init__(self, bot): 57 self.bot = bot 58 59 @commands.command() 60 async def join(self, ctx, *, channel: discord.VoiceChannel): 61 """Joins a voice channel""" 62 63 if ctx.voice_client is not None: 64 return await ctx.voice_client.move_to(channel) 65 66 await channel.connect() 67 68 @commands.command() 69 async def play(self, ctx, *, query): 70 """Plays a file from the local filesystem""" 71 72 source = discord.PCMVolumeTransformer(discord.FFmpegPCMAudio(query)) 73 ctx.voice_client.play(source, after=lambda e: print(f'Player error: {e}') if e else None) 74 75 await ctx.send(f'Now playing: {query}') 76 77 @commands.command() 78 async def yt(self, ctx, *, url): 79 """Plays from a url (almost anything youtube_dl supports)""" 80 81 async with ctx.typing(): 82 player = await YTDLSource.from_url(url, loop=self.bot.loop) 83 ctx.voice_client.play(player, after=lambda e: print(f'Player error: {e}') if e else None) 84 85 await ctx.send(f'Now playing: {player.title}') 86 87 @commands.command() 88 async def stream(self, ctx, *, url): 89 """Streams from a url (same as yt, but doesn't predownload)""" 90 91 async with ctx.typing(): 92 player = await YTDLSource.from_url(url, loop=self.bot.loop, stream=True) 93 ctx.voice_client.play(player, after=lambda e: print(f'Player error: {e}') if e else None) 94 95 await ctx.send(f'Now playing: {player.title}') 96 97 @commands.command() 98 async def volume(self, ctx, volume: int): 99 """Changes the player's volume""" 100 101 if ctx.voice_client is None: 102 return await ctx.send("Not connected to a voice channel.") 103 104 ctx.voice_client.source.volume = volume / 100 105 await ctx.send(f"Changed volume to {volume}%") 106 107 @commands.command() 108 async def stop(self, ctx): 109 """Stops and disconnects the bot from voice""" 110 111 await ctx.voice_client.disconnect() 112 113 @play.before_invoke 114 @yt.before_invoke 115 @stream.before_invoke 116 async def ensure_voice(self, ctx): 117 if ctx.voice_client is None: 118 if ctx.author.voice: 119 await ctx.author.voice.channel.connect() 120 else: 121 await ctx.send("You are not connected to a voice channel.") 122 raise commands.CommandError("Author not connected to a voice channel.") 123 elif ctx.voice_client.is_playing(): 124 ctx.voice_client.stop() 125 126bot = commands.Bot(command_prefix=commands.when_mentioned_or("!"), 127 description='Relatively simple music bot example') 128 129@bot.event 130async def on_ready(): 131 print(f'Logged in as {bot.user} (ID: {bot.user.id})') 132 print('------') 133 134bot.add_cog(Music(bot))
試したこと
手が付けられない
補足情報(FW/ツールのバージョンなど)
python 3.8.5
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。