Python:Discord 机器人语音聊天不再有效

问题描述 投票:0回答:2

我的 Discord 语音聊天机器人已经工作了 2 年。截至本周早些时候,该机器人可以正常连接到 discord,但它不播放任何音频。

机器人的目的是在 Discord 文本聊天频道中寻找“$Hello”消息,然后连接到语音聊天并通过语音聊天播放 Hello.mp3 文件。

我在两台不同的服务器上运行这个机器人,但都同时停止工作。 我使用了一些打印命令,我可以看到它挂在这行代码上没有任何错误,但它也没有打印“test Hello 2”

vc = await voice_channel.connect()

完整代码:

import os
import discord
from discord.ext.commands import Bot
from discord.ext import commands
from datetime import datetime as dt
from time import strftime
from discord.ext import tasks
import asyncio

TOKEN = 'xxxxxxxxxxxxxx' ### This is the discord unique token for the bot to work ###
client = discord.Client()
voiceChannelId = 57575757575 ###This is the voice channel ID the bot connects to in order to play MP3s ###
#bot = commands.Bot('.')
#DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")


###### When the code executes, the bot is in the on_ready state and goes to playSounds() #####
##### This code is not used in this part of the code but left it in just in case ####
@client.event
async def on_ready():
        await asyncio.sleep(1)
        print('We have logged in as {0.user}'.format(client))
        playSounds.start()
        
##### Voice chat connection ####
@client.event
async def join(ctx):
    channel = ctx.author.voice.channel
    await channel.connect()

###### looks for Messages anywhere in discord that contain "$Hello" #######
@client.event
async def on_message(message):
    if (message.content == "$Hello"):
                print('starting Hello()')
                voice_channel = client.get_channel(voiceChannelId)
                try:
                        print('test Hello 1')
                        global vc
                        vc = await voice_channel.connect()
                        print('test Hello 2') ##### This is not printing, suspect line above is hanging ###
                        vc.play(discord.FFmpegPCMAudio(source='/root/Desktop/Discord/Code/mp3/Hello.mp3'))
                        vc.source = discord.PCMVolumeTransformer(vc.source)
                        vc.source.volume = 1
                        while vc.is_playing():
                           await asyncio.sleep(1)
                        print('test Hello 3')
                
                except:
                        print('test Hello 4')
                        if (vc.is_connected() == True):
                                vc.play(discord.FFmpegPCMAudio(source='/root/Desktop/Discord/Code/mp3/Hello.mp3'))
                                vc.source = discord.PCMVolumeTransformer(vc.source)
                                vc.source.volume = 1
                                while vc.is_playing():
                                    await asyncio.sleep(1)
                                print('Test Hello 5')
                await message.channel.send("testing audio.")

尝试更新:

python3 -m pip 安装 -U discord.py.

python3 -m pip install -U discord.py[语音]

以及更新 python。

编辑:我什至尝试让 ChatGPT 让它与我的代码的 10 种不同变体一起工作。没有帮助。 AI 表示 Discord API 或其他内容可能最近发生了变化,但不能再告诉我了。

我的服务器和机器人有正确的权限,那里没有任何改变,但我仍然三重检查..

python discord voice
2个回答
1
投票

确保你的图书馆是最新的,不和谐最近做了改变。它对我有用。

注意我更新的是

discord
,不是
discord.py

pip install -U discord


0
投票

正如 dinones 所回答的那样;

pip 安装 -U discord

© www.soinside.com 2019 - 2024. All rights reserved.