JayK23
JayK23

Reputation: 263

Discord.py - 'Member' object has no attribute 'channel'

I'm just getting started to discord.py and i'm trying to create a very simple bot that plays an mp3 audio from my local storage. So basically the bot should join a voice channel and play the mp3. Here is my code:

import discord
import ffmpeg
from discord.ext import commands
import datetime

from urllib import parse, request
import re

bot = commands.Bot(command_prefix='$', description="This is a Helper Bot")

@bot.command(name="test")
async def test(ctx):
    # Gets voice channel of message author
    voice_channel = ctx.author.channel
    channel = None
    if voice_channel != None:
        channel = voice_channel.name
        vc = await voice_channel.connect()
        vc.play(discord.FFmpegPCMAudio(executable="C:/ffmpeg/bin/ffmpeg.exe", source=r"PATH"))
        # Sleep while audio is playing.
        while vc.is_playing():
            sleep(.1)
        await vc.disconnect()
    else:
        await ctx.send(str(ctx.author.name) + "is not in a channel.")
    # Delete command after the audio is done playing.
    await ctx.message.delete()

@bot.command()
async def ping(ctx):
    await ctx.send('pong')

@bot.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))



bot.run('MYKEY')

I found the test function here on SO, but i'm having problems using it. When i call it from discord, i get the following error:

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Member' object has no attribute 'channel'

What am i doing wrong here?

Upvotes: 1

Views: 3831

Answers (1)

Tin Nguyen
Tin Nguyen

Reputation: 5330

A discord.Member instance does not have an attribute called channel. It does however have an attribute named voice which is an instance of Optional[VoiceState], see https://discordpy.readthedocs.io/en/latest/api.html?highlight=voice_state#discord.Member.voice

That voice object has an attribute called channel which is an instance of Optional[VoiceChannel], see https://discordpy.readthedocs.io/en/latest/api.html?highlight=voice_state#discord.VoiceState.channel

Upvotes: 1

Related Questions