P
P
pythonMyLife2020-12-08 09:45:51
Python
pythonMyLife, 2020-12-08 09:45:51

Why is the bot not connecting to the state channel?

I'm writing a discord bot in python 3 using the discord.py api. Now I'm trying to connect it to the voice channel.

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

Mistake:
<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>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Nevzorov, 2020-12-08
@pythonMyLife

The bot.connect function , in theory, should not be called manually. It has nothing to do with connecting to a voice channel.
To connect to a voice channel, there is a method on the voice channel itself: VoiceChannel.connect()
Tip: Don't reinvent the wheel. Discord.py already has a pretty handy extension for commands, use it: 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')

A
Amoralny, 2020-12-08
Cat @Amoralny

Read the documentation!!!
https://discordpy.readthedocs.io/en/latest/api.htm...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question