Ошибка AttributeError при попытке запустить дискорд бота
Этот код выдает ошибку:
import discord
TOKEN = ''
VOICE_CHANNEL_ID = '...'
class Voice_bot(discord.Client):
def __init__(self):
intents = discord.Intents.default()
intents.message_content = True
super().__init__(intents=intents)
async def on_ready(self):
self.voice_channel = self.get_channel(VOICE_CHANNEL_ID)
if self.voice_channel == None:
print('НЕ удалось подключится к голосовому каналу')
return
self.voice_client = await self.voice_channel.connect()
async def on_voice_state_update(self,member, before, after):
if member.id == self.user.id:
pass
if before.channel == None:
print(f'{member.name} вошёл в голосовой канал {after.channel}')
source = discord.FFmpegPCMAudio(source='sounds\\hello\\vbo.mp3',executable="ffmpeg\\bin\\ffmpeg.exe")
play = self.voice_client.play(source)
if after.channel == None:
print(f'{member.name} вышел из голосового канала {before.channel}')
[ERROR] discord.client: Ignoring exception in on_voice_state_update
Traceback (most recent call last):
File "C:\Users\pc\PycharmProjects\bot\.venv\Lib\site-packages\discord\client.py", line 449, in _run_event
await coro(*args, **kwargs)
File "C:\Users\pc\PycharmProjects\PythonProject2\VOICEBOT.py", line 31, in on_voice_state_update
play = self.voice_client.play(source)
^^^^^^^^^^^^^^^^^
AttributeError: 'Voice_bot' object has no attribute 'voice_client'
Ответы (2 шт):
Автор решения: user674434
→ Ссылка
Сама ошибка указывает, что в классе Voice_not
нет атрибута voice_client
.
В самом коде такого атрибута нет, поэтому и выходит такая ошибка.
Автор решения: Александр Ларочкин
→ Ссылка
async def on_voice_state_update(self,member, before, after):
if member.id == self.user.id:
print('Бот вошел в голосовой канал или вышел из него.')
else:
if before.channel == None:
print(f'{member.name} вошёл в голосовой канал {after.channel}')
source = discord.FFmpegPCMAudio(source='sounds\\hello\\vbo.mp3',executable="ffmpeg\\bin\\ffmpeg.exe")
self.voice_client.play(source)
if after.channel == None:
print(f'{member.name} вышел из голосового канала {before.channel}') вот так правильно