質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Discord

Discordは、ゲーマー向けのボイスチャットアプリです。チャット・通話がブラウザ上で利用可能で、個人専用サーバーも開設できます。通話中でも音楽を流したり、PC画面を共有できるなど多機能な点が特徴です。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

1245閲覧

音楽botの改良について

amenoame

総合スコア1

Discord

Discordは、ゲーマー向けのボイスチャットアプリです。チャット・通話がブラウザ上で利用可能で、個人専用サーバーも開設できます。通話中でも音楽を流したり、PC画面を共有できるなど多機能な点が特徴です。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2021/07/10 10:20

前提・実現したいこと

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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

残念ながら、コード作成依頼は受け付けていません

投稿2021/07/10 10:29

y_waiwai

総合スコア87774

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問