我终其一生都找不到或想出一个可行的解决方案。这是实际上很重要的一段代码,以及整个文件,如果您也想看到的话
async def play(self, ctx: commands.Context, url, lp):
channel = ctx.author.voice.channel
if lp == 'loop':
await channel.connect()
async with ctx.typing():
player = await YTDLSource.from_url(url, loop=self.bot.loop)
ctx.voice_client.play(player, after=lambda e: print('Player error: %s' % e) if e else None)
await ctx.send('Now playing: {}'.format(player.title))
while True:
if not ctx.voice_client.is_playing():
async with ctx.typing():
ctx.voice_client.play(player, after=lambda e: print('Player error: %s' % e) if e else None)
time.sleep(0.5)
else:
async with ctx.typing():
await channel.connect()
player = await YTDLSource.from_url(url, loop=self.bot.loop)
ctx.voice_client.play(player, after=lambda e: print('Player error: %s' % e) if e else None)
await ctx.send('Now playing: {}'.format(player.title))
from discord.ext import commands
import ffmpeg
import youtube_dl.YoutubeDL
import asyncio
import time
ytdl_format_options = {
'format': 'bestaudio/best',
'outtmpl': '%(extractor)s-%(id)s-%(title)s.%(ext)s',
'restrictfilenames': True,
'noplaylist': True,
'nocheckcertificate': True,
'ignoreerrors': False,
'logtostderr': False,
'quiet': True,
'no_warnings': True,
'default_search': 'auto',
'source_address': '0.0.0.0' # bind to ipv4 since ipv6 addresses cause issues sometimes
}
ffmpeg_options = {
'options': '-vn'
}
ytdl = youtube_dl.YoutubeDL(ytdl_format_options)
class YTDLSource(discord.PCMVolumeTransformer):
def __init__(self, source, *, data, volume=0.5):
super().__init__(source, volume)
self.data = data
self.title = data.get('title')
self.url = data.get('url')
@classmethod
async def from_url(cls, url, *, loop=None, stream=False):
loop = loop or asyncio.get_event_loop()
data = await loop.run_in_executor(None, lambda: ytdl.extract_info(url, download=not stream))
if 'entries' in data:
# take first item from a playlist
data = data['entries'][0]
filename = data['url'] if stream else ytdl.prepare_filename(data)
return cls(discord.FFmpegPCMAudio(filename, **ffmpeg_options), data=data)
class MyBoi(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot
self.voice_states = {}
@commands.command(name='leave')
async def leave(self, ctx: commands.Context):
await ctx.voice_client.disconnect()
@commands.command(name='play')
async def play(self, ctx: commands.Context, url, lp):
channel = ctx.author.voice.channel
if lp == 'loop':
await channel.connect()
async with ctx.typing():
player = await YTDLSource.from_url(url, loop=self.bot.loop)
ctx.voice_client.play(player, after=lambda e: print('Player error: %s' % e) if e else None)
await ctx.send('Now playing: {}'.format(player.title))
while True:
if not ctx.voice_client.is_playing():
async with ctx.typing():
ctx.voice_client.play(player, after=lambda e: print('Player error: %s' % e) if e else None)
time.sleep(0.5)
else:
async with ctx.typing():
await channel.connect()
player = await YTDLSource.from_url(url, loop=self.bot.loop)
ctx.voice_client.play(player, after=lambda e: print('Player error: %s' % e) if e else None)
await ctx.send('Now playing: {}'.format(player.title))
intents = discord.Intents.all()
clnt = commands.Bot(command_prefix='#', intents=intents)
clnt.add_cog(MyBoi(clnt))
lop = {0: False}
plr = {}
@clnt.event
async def on_ready():
print("ready")
clnt.run("the actual key normally")
代码是否制作不良和/或组织不良?大概。但这是一个个人项目,没想到会与任何人分享。如果您需要澄清任何事情 lmk.
使用此处的代码,我遇到的问题是当我执行循环版本时,机器人会断开一帧并重新连接,然后出现此错误
discord.ext.commands.errors.CommandInvokeError:命令引发异常:ClientException:未连接到语音。
当不使用循环版本时,机器人不会立即断开连接,并且尝试在循环开始时手动重新连接它会给我一个错误,说它已经连接。
另请注意,我没有编写 YTDLSource 类或 ytdl_format_options。