Description
Summary
Calling ctx.author.voice.channel.connect() gets the bot to join the voice channel, but then it doesn't respond to the slash command and then exits the voice channel 25 seconds later
Reproduction Steps
Follow the tutorial here, but simplify the code to only call the voice.channel.connect()
method. Run the slash command record
.
Minimal Reproducible Code
import discord
import os
from dotenv import load_dotenv
load_dotenv() # load all the variables from the env file
connections = {}
intents = discord.Intents.default()
intents.voice_states = True
bot = discord.Bot(intents=intents)
@bot.event
async def on_ready():
print(f"{bot.user} is ready and online!")
@bot.command()
async def record(ctx): # If you're using commands.Bot, this will also work.
voice = ctx.author.voice
if not voice:
await ctx.respond("You aren't in a voice channel!")
vc = await voice.channel.connect() # Connect to the voice channel the author is in.
connections.update({ctx.guild.id: vc}) # Updating the cache with the guild and channel.
await ctx.respond("Started recording!")
bot.run(os.getenv('DISCORD_TOKEN')) # run the bot with the token
Expected Results
When the user who sent the slash command is in the voice channel, the bot should connect and then respond with "Started recording!". The bot should then stay in the voice channel.
Actual Results
The bot does join the voice channel, but disconnects after 25 seconds (even when there is speaking activity from the user). Once the bot disconnects, the voice.channel.connect()
method returns and then we get a 404 Not Found Unknown interaction
error. Additionally, while the bot is hanging on the connect method, after 3 seconds it sends a The application did not respond
red error message in the Discord UI.
bot is ready and online!
Ignoring exception in command record:
Traceback (most recent call last):
File "test_discord_bot/venv/lib/python3.10/site-packages/discord/commands/core.py", line 138, in wrapped
ret = await coro(arg)
File "test_discord_bot/venv/lib/python3.10/site-packages/discord/commands/core.py", line 1082, in _invoke
await self.callback(ctx, **kwargs)
File "test_discord_bot/main.py", line 28, in record
await ctx.respond("Started recording!")
File "test_discord_bot/venv/lib/python3.10/site-packages/discord/interactions.py", line 620, in respond
return await self.response.send_message(*args, **kwargs)
File "test_discord_bot/venv/lib/python3.10/site-packages/discord/interactions.py", line 961, in send_message
await self._locked_response(
File "test_discord_bot/venv/lib/python3.10/site-packages/discord/interactions.py", line 1292, in _locked_response
await coro
File "test_discord_bot/venv/lib/python3.10/site-packages/discord/webhook/async_.py", line 222, in request
raise NotFound(response, data)
discord.errors.NotFound: 404 Not Found (error code: 10062): Unknown interaction
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "test_discord_bot/venv/lib/python3.10/site-packages/discord/bot.py", line 1137, in invoke_application_command
await ctx.command.invoke(ctx)
File "test_discord_bot/venv/lib/python3.10/site-packages/discord/commands/core.py", line 435, in invoke
await injected(ctx)
File "test_discord_bot/venv/lib/python3.10/site-packages/discord/commands/core.py", line 146, in wrapped
raise ApplicationCommandInvokeError(exc) from exc
discord.errors.ApplicationCommandInvokeError: Application Command raised an exception: NotFound: 404 Not Found (error code: 10062): Unknown interaction
Intents
discord.Intents.default() with intents.voice_states = True
System Information
- Python v3.10.12-final
- py-cord v2.6.1-final
- aiohttp v3.12.14
- system info: Linux 6.8.0-60-generic Added
on_raw_typing
event #63~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Tue Apr 22 19:00:15 UTC 2
Checklist
- I have searched the open issues for duplicates.
- I have shown the entire traceback, if possible.
- I have removed my token from display, if visible.
Additional Context
I've tested this with both of the following installation methods and the results are the same:
pip install "py-cord[voice]"
python3 -m pip install "git+https://github.com/Pycord-Development/pycord#egg=py-cord[voice]"
Issue #2310 seemed similar to this, but I'm not sure if it's the same cause. Is it necessary to have the voice
scope when generating the OAuth2 URL? If so, then this might be the issue, because the discord documentation says:
voice scope: allows your app to connect to voice on user's behalf and see all the voice members - requires Discord approval
However I thought that was unrelated to the bot...