【发布时间】:2020-02-19 16:00:51
【问题描述】:
Bot 不会连接到频道并播放音乐,因为它给出了错误。:
AttributeError: 'Bot' object has no attribute 'is_voice_connected'
我查看了一些 discord.py 教程并编写了以下代码:
import asyncio
from discord.ext import commands
client = commands.Bot(command_prefix='!')
songs = asyncio.Queue()
play_next_song = asyncio.Event()
@client.event
async def on_ready():
print('client ready')
async def audio_player_task():
while True:
play_next_song.clear()
current = await songs.get()
current.start()
await play_next_song.wait()
def toggle_next():
client.loop.call_soon_threadsafe(play_next_song.set)
@client.command(pass_context=True)
async def play(ctx, url):
if not client.is_voice_connected(ctx.message.server):
voice = await client.join_voice_channel(ctx.message.author.voice_channel)
else:
voice = client.voice_client_in(ctx.message.server)
player = await voice.create_ytdl_player(url, after=toggle_next)
await songs.put(player)
client.loop.create_task(audio_player_task())
client.run('TOKEN')
【问题讨论】:
标签: python discord python-asyncio