Discord音乐Bot执行play命令无音频且报TypeError错误求助
解决Discord音乐Bot的播放错误与无声问题
问题现象
- 执行
play命令后,Discord显示Bot正在输出音频,但听不到任何声音 - PyCharm控制台抛出如下错误:
Traceback (most recent call last): File "/Volumes/Mahmoud-Disk/MyProfile/Library/Python/3.8/lib/python/site-packages/discord/ext/commands/core.py", line 229, in wrapped ret = await coro(*args, **kwargs) File "/Volumes/Mahmoud-Disk/MyProfile/Desktop/Discord Bot/Discord-Bot/main.py", line 72, in play await ctx.send(embed = discord.Embed( TypeError: __init__() got an unexpected keyword argument 'author' The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/Volumes/Mahmoud-Disk/MyProfile/Library/Python/3.8/lib/python/site-packages/discord/ext/commands/bot.py", line 1349, in invoke await ctx.command.invoke(ctx) File "/Volumes/Mahmoud-Disk/MyProfile/Library/Python/3.8/lib/python/site-packages/discord/ext/commands/core.py", line 1023, in invoke await injected(*ctx.args, **ctx.kwargs) # type: ignore File "/Volumes/Mahmoud-Disk/MyProfile/Library/Python/3.8/lib/python/site-packages/discord/ext/commands/core.py", line 238, in wrapped raise CommandInvokeError(exc) from exc discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: __init__() got an unexpected keyword argument 'author'
用户代码
import discord from discord.ext import commands import wavelink client = commands.Bot(command_prefix = ".", intents = discord.Intents.all()) class CustomPlayer (wavelink.Player): def __init__(self): super().__init__() self.queue = wavelink.Queue() @client.event async def on_ready(): client.loop.create_task(connect_nodes()) #HTTPS and Websocket operations async def connect_nodes(): #Helper function await client.wait_until_ready() await wavelink.NodePool.create_node( bot = client , host = "127.0.0.1" , port = 2333 , password = "yk I can't " ) @client.event async def on_wavelink_node_ready(node = wavelink.Node): print(f"Node: <{node.identifier}> is ready!") @client.command() async def connect(ctx): vc = ctx.voice_client try: channel = ctx.author.voice.channel except AttributeError: return await ctx.send("Please join a channel to connect.") if not vc: await ctx.author.voice.channel.connect(cls = CustomPlayer()) else: await ctx.send("The bot is already connected to a voice channel.") @client.command() async def disconnect(ctx): vc = ctx.voice_client if vc: await vc.disconnect() else: await ctx.send("Bot is not connected to a voice channel.") @client.command() async def play(ctx, *, search: wavelink.YouTubeTrack): vc = ctx.voice_client #represents a discord voice connection if not vc: custom_player = CustomPlayer() vc: CustomPlayer = await ctx.author.voice.channel.connect(cls = custom_player) if vc.is_playing(): vc.queue.put(item = search) await ctx.send(embed = discord.Embed( title = search.title, url = search.uri, author = ctx.author, description = f"Queued {search.title} in {vc.channel}" )) else: await vc.play(search) await ctx.send(embed = discord.Embed( title = search.title, url = search.uri, author = ctx.author, description = f"Playing {vc.source.title} in {vc.channel}"))
解决方案
1. 修复Embed初始化错误
discord.Embed的构造函数不支持author参数,需要通过set_author()方法单独设置作者信息。修改play命令中的两处Embed创建代码:
队列提示部分修改:
# 创建Embed基础内容 embed = discord.Embed( title=search.title, url=search.uri, description=f"Queued {search.title} in {vc.channel}" ) # 设置作者信息 embed.set_author(name=ctx.author.display_name, icon_url=ctx.author.avatar.url) await ctx.send(embed=embed)
播放提示部分修改:
embed = discord.Embed( title=search.title, url=search.uri, description=f"Playing {vc.source.title} in {vc.channel}" ) embed.set_author(name=ctx.author.display_name, icon_url=ctx.author.avatar.url) await ctx.send(embed=embed)
2. 排查无声问题
修复上述错误后,如果仍听不到声音,按以下步骤排查:
- 确认Lavalink节点已启动:本地需运行Lavalink服务,且代码中
connect_nodes()方法的host、port、password与Lavalink配置文件一致 - 检查Bot权限:确保Bot在服务器中拥有
语音连接和语音发言权限 - 版本兼容性:确认
discord.py与wavelink版本匹配(Python3.6需使用对应兼容的旧版本,比如discord.py<=1.7.3,wavelink<=1.7.0) - 本地音频设置:检查Discord中Bot的音量是否调至最大,以及自身音频输出设备是否正常
内容的提问来源于stack exchange,提问作者P-erson
相关产品推荐
相关产品推荐

