首页 > 解决方案 > 错误:“TextChannel”对象没有“连接”属性

问题描述

这是代码部分:

import discord 
import random from discord.utils 
import get import time

 class MyClient(discord.Client):
     #Einloggen
     async def on_ready(self):
         print("Eingeloggt")
 
     #Wenn Nachricht gepostet wird
     async def on_message(self, message):
         if message.author == client.user:
             return
        
         if message.content == "$help":
            print("help")
 
         if message.content.startswith("$play"):
             where = message.content.split(" ")[1]
             channel = get(message.guild.channels, name=where)
             voicechannel = await channel.connect()
             voicechannel.play(discord.FFmpegPCMAudio('triggered.mp3'))


client = MyClient()
client.run("")

这是错误:

line 22, in on_message
voicechannel = await channel.connect()
AttributeError: 'TextChannel' object has no attribute 'connect

我已经安装了 discord、opus、ffmpg 和其他一些东西,我该怎么办?请帮忙

标签: pythondiscorddiscord.py

解决方案


正如错误所述,您正在尝试连接到文本频道,您只能连接到语音频道。代替:

channel = get(message.guild.channels, name=where)

使用Guild.voice_channels属性

channel = get(message.guild.voice_channels, name=where)

推荐阅读