3

I want to make the bot join the voice channel that I'm in when prompted. Here's what I have:

@client.event
async def on_message(message):
  if message.content.startswith('.join'):
    channel = 775902254951301125
    await channel.connect()

It doesn't seem to work, any tips?

6
  • 2
    Hello 👋! Can you be more specific about what you mean by "doesn't work"? What do you expect to happen? What happens instead? Answering these questions will help us understand your issue.
    – idontknow
    Commented Jan 15, 2021 at 1:01
  • 1
    When I trigger the command I expect the bot to join the voice channel, but it gives me this instead- AttributeError: 'int' object has no attribute 'connect'. Commented Jan 15, 2021 at 1:11
  • 2
    "channel" in the code above is just a plain number. You seem to want to call a method (.connect()) on it, as if it was some kind of object related to the discord.py library. You probably forgot to call some function from the library with this number. First thing I recommend would be to check the documentation for an example.
    – Pac0
    Commented Jan 15, 2021 at 1:16
  • 1
    Oh. That number was the ID of the voice channel, but I guess it doesn't work like that. I tweaked the code: @client.event async def on_message(message): if message.content.startswith('.join'): channel = author.voice.channel await channel.connect() Now it tells me that 'author' isn't defined. How would I go about defining it? (Sorry I'm new to programming) Commented Jan 15, 2021 at 1:24
  • 2
    Since you are trying to get the author of the message, try message.author.voice.channel instead 🤗
    – idontknow
    Commented Jan 15, 2021 at 4:41

2 Answers 2

1

I think this is what you're looking for!

@client.command(pass_context=True)
async def join(ctx):
    channel = ctx.message.author.voice.voice_channel
    await client.join_voice_channel(channel)

That was pulled straight from this video which was just a google search away.

2
  • 2
    I tried this out, and it gives me an error: AttributeError: 'VoiceState' object has no attribute 'voice_channel' Commented Jan 15, 2021 at 17:43
  • I don't think the op is using the bot commands framework.
    – idontknow
    Commented Jan 15, 2021 at 19:17
0
import discord
from discord.ext import commands
client = commands.Bot(command_prefix = '>')

@client.event
async def on_ready():
  print ("Log : "+str(client.user))
  ch = await client.fetch_channel("enter id voice channel")
  await ch.connect()


client.run('token')
1
  • 2
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Nov 7, 2022 at 7:13

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.