【发布时间】:2021-03-06 02:43:42
【问题描述】:
我刚刚开始使用 discord.py,我正在尝试创建一个非常简单的机器人,可以播放本地存储中的 mp3 音频。所以基本上机器人应该加入一个语音频道并播放 mp3。这是我的代码:
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')
我在 SO 上找到了 test 函数,但我在使用它时遇到了问题。当我从不和谐中调用它时,我收到以下错误:
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Member' object has no attribute 'channel'
我在这里做错了什么?
【问题讨论】:
-
如果异常正确,
ctx.author.channel似乎没有名为 channel 的属性,要验证这一点,请查看此处打印属性:stackoverflow.com/a/193539/7968203
标签: python discord discord.py