【问题标题】:What's wrong with my discordpy get() method?我的不和谐 get() 方法有什么问题?
【发布时间】:2021-11-29 06:23:45
【问题描述】:

所以我想写一个有趣的小不和谐机器人,它以俄罗斯为主题。无论请求什么歌曲,我都想让它播放苏联国歌,在discord命令中“&channel”之后写的频道中。

这是我得到的控制台提要,当我输入“vadim play something &channel Just Chatting blyat:

女贞 只是聊天 忽略 on_menter 代码中的异常 回溯(最近一次通话最后): _run_event 中的文件“C:\Users\maria\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py”,第 343 行 等待 coro(*args, **kwargs) 文件“C:\Users\maria\Desktop\Coding\Projekte\Python\SovjetBot\Code\bot.py”,第 33 行,on_message voicechannel = channel.connect() AttributeError: 'NoneType' 对象没有属性 'connect'

这是我的代码:

import discord
from discord.utils import get
import time


class MyClient(discord.Client):

    #Triggered on login
    async def on_ready(self):
        print("Privet")
    #Triggered on messages
    async def on_message(self, message):
        if message.author == client.user:
            return

        if message.content.lower().startswith("vadim") and message.content.lower().endswith("blyat"):
            messagearray = message.content.split(" ")
            messagearray.pop()
            messagearray.remove("vadim")

            if messagearray[0].lower() == "play" and ((len(messagearray)) != 1 or messagearray.contains('&channel')):

                where = ""
                for i in range(messagearray.index("&channel") + 1, len(messagearray)):
                    where = where + ' ' + messagearray[i]
                    where = where[0:]
                    time.sleep(0.25)

                print(where)

                channel = get(message.guild.channels, name=where)
                time.sleep(0.25)
                voicechannel = channel.connect()
                time.sleep(0.25)
                voicechannel.play(discord.FFmpegPCMAudio('National Anthem of USSR.mp3'))








client = MyClient()
client.run(The bots client id)

【问题讨论】:

    标签: python python-3.x discord discord.py


    【解决方案1】:

    TLDR

    改变

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

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

    说明

    有错误表明您在voicechannel = channel.connect() 行有问题,因为channelNoneType。您在上面的行中使用channel = get(message.guild.channels, name=where) 定义channel。但是,根据documentation,您使用的get() 函数是协程,因此您需要await

    旁注

    创建channel 后,在尝试将其与voicechannel = channel.connect() 一起使用之前,应检查它是否不是None

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-30
      • 2014-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-09
      • 1970-01-01
      相关资源
      最近更新 更多