@pythonMyLife

Почему бот не подключается к гососовому каналу?

Я пишу бота для дискорд на python 3, через апи discord.py. Сейчас я пытаюсь его подключить к голосовому каналу.

import discord
from discord.ext import commands

TOKEN = 'Скрыт'
ver = '0.4.1 VChCT build (Voice Channel Connect Test'

intents = discord.Intents.default ()
intents.members = True
bot = commands.Bot (command_prefix = settings ['prefix'], intents = intents)

async def vch (message, bot):
    channel = message.author.voice.channel
    if channel:
        await message.channel.send(channel.id)
        await bot.connect ()
    else:
        await ctx.send('you arent in a vc')
 
@bot.event 
async def on_message (message): 
    if message.author.bot:
        return

    if message.content.lower () == '!connect':
        await vch (message, bot)
        return

Ошибка:
<br>
Traceback (most recent call last):<br>
  File "D:\Misha\Языки\API\discord\NuclearBot-collected\Nuclearbot-main-3\Nuclearbot-main\main.py", line 119, in <br>
    bot.run (TOKEN)<br>
  File "C:\Users\errma\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 708, in run<br>
    return future.result()<br>
  File "C:\Users\errma\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 687, in runner<br>
    await self.start(*args, **kwargs)<br>
  File "C:\Users\errma\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 651, in start<br>
    await self.connect(reconnect=reconnect)<br>
  File "C:\Users\errma\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 551, in connect<br>
    await self.ws.poll_event()<br>
  File "C:\Users\errma\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\gateway.py", line 560, in poll_event<br>
    msg = await self.socket.receive(timeout=self._max_heartbeat_timeout)<br>
  File "C:\Users\errma\AppData\Local\Programs\Python\Python39\lib\site-packages\aiohttp\client_ws.py", line 212, in receive<br>
    raise RuntimeError(<br>
RuntimeError: Concurrent call to receive() is not allowed<br>
>>> Exception in thread Thread-1:<br>
Traceback (most recent call last):<br>
  File "C:\Users\errma\AppData\Local\Programs\Python\Python39\lib\threading.py", line 950, in _bootstrap_inner<br>
    self.run()<br>
  File "C:\Users\errma\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\gateway.py", line 150, in run<br>
    f = asyncio.run_coroutine_threadsafe(coro, loop=self.ws.loop)<br>
  File "C:\Users\errma\AppData\Local\Programs\Python\Python39\lib\asyncio\tasks.py", line 926, in run_coroutine_threadsafe<br>
    loop.call_soon_threadsafe(callback)<br>
  File "C:\Users\errma\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 791, in call_soon_threadsafe<br>
    self._check_closed()<br>
  File "C:\Users\errma\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 510, in _check_closed<br>
    raise RuntimeError('Event loop is closed')<br>
RuntimeError: Event loop is closed<br>
<br>
Warning (from warnings module):<br>
  File "C:\Users\errma\AppData\Local\Programs\Python\Python39\lib\threading.py", line 952<br>
    self._invoke_excepthook(self)<br>
RuntimeWarning: coroutine 'DiscordWebSocket.send_heartbeat' was never awaited<br>
  • Вопрос задан
  • 143 просмотра
Решения вопроса 1
Функия bot.connect, по идее, не должна вызываться вручную. Она не имеет никакого отношения к подключению к голосовому каналу.
Для подключения к голосовому каналу есть метод у самого голосового канала: VoiceChannel.connect()

Совет: Не изобретайте велосипед. В discord.py уже есть достаточно удобное расширение для команд, используйте его: https://discordpy.readthedocs.io/en/stable/ext/com...

import discord
from discord.ext import commands

TOKEN = 'Скрыт'
VER = '0.4.1 VChCT build (Voice Channel Connect Test'  # см. PEP8

intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix=settings['prefix'], description=VER, intents=intents)

@bot.command()
@commands.guild_only()  # https://discordpy.readthedocs.io/en/stable/ext/commands/api.html#discord.ext.commands.guild_only
async def connect(ctx):
    """Connect to yours voice channel"""
    if (voice := ctx.author.voice) and (voice_channel := voice.channel):
        await voice_channel.connect()
        await ctx.send(channel.id)
    else:
        await ctx.send('you arent in a vc')
Ответ написан
Пригласить эксперта
Ответы на вопрос 1
Amoralny
@Amoralny
Python-разработчик
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы