【发布时间】:2021-10-06 07:59:50
【问题描述】:
我最近决定学习 discord.py 库,我正在尝试制作一个音乐机器人。当我使用“播放”命令时,它不会引发任何错误,但也不会播放任何音频。同样在使用“播放”后,如果使用“暂停”、“停止”和“恢复”命令,我会收到错误:
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: object NoneType can't be used in 'await' expression
代码:
import discord
from discord.ext import commands
import youtube_dl
class music(commands.Cog):
def __init__(self, client):
self.client = client
self.vc = None
@commands.command(name="join",help="Müzük açmadan önce kanala katılırım.")
async def join(self, ctx):
if ctx.author.voice is None:
await ctx.send(f"{ctx.message.author.mention}Şu anda bir ses kanalında değilsin.")
voice_channel = ctx.author.voice.channel
if ctx.voice_client is None:
await voice_channel.connect()
else:
await ctx.send(f"{ctx.message.author.mention}Şu anda başka bir ses kanalındayım.")
print(ctx.voice_client.is_playing())
@commands.command(name="disconnect",help="Kanaldan çıkıp giderim.")
async def disconnect(self, ctx):
await ctx.voice_client.disconnect()
@commands.command(name="play",aliases =['p'],help="YouTube linkini alıp oynatırım.")
async def play(self, ctx, url):
ctx.voice_client.stop()
FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5','options':'vnd'}
YDL_OPTIONS = {'format':"bestaudio"}
vc = ctx.voice_client
with youtube_dl.YoutubeDL(YDL_OPTIONS) as ydl:
info = ydl.extract_info(url,download=False)
url2 = info['formats'][0]['url']
source = await discord.FFmpegOpusAudio.from_probe(url2, **FFMPEG_OPTIONS)
vc.play(source)
await ctx.send("Şu anda şarkını çalıyorum.")
@commands.command(name = "pause",help="Müzüğü durdururum.")
async def pause(self,ctx):
await ctx.voice_client.pause()
await ctx.send("Müzik durduruldu")
@commands.command(name="stop",help="Müzük kapanır")
async def stop(self, ctx):
await ctx.voice_client.stop()
await ctx.send("Müzüğü kapattım.")
@commands.command(name = "resume",help="Müzüğü devam ettiririm.")
async def resume(self,ctx):
await ctx.voice_client.resume()
await ctx.send("Müzik devam ediyor")
def setup(client):
client.add_cog(music(client))
【问题讨论】:
标签: python discord discord.py