AttributeError:"NoneType"对象没有属性"channels" [英] AttributeError: 'NoneType' object has no attribute 'channels'

查看:53
本文介绍了AttributeError:"NoneType"对象没有属性"channels"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Discord机器人模块出现问题.我收到 AttributeError:'NoneType'对象没有属性'channels'我不确定它抛出此错误的方式:

Hi I'm having an issue with a module for my Discord bot. I'm getting AttributeError: 'NoneType' object has no attribute 'channels' I'm not sure way it's throwing out this error:

这就是我正在使用的东西:

Here is what I'm working with:

from discord.ext import commands
from discord.utils import get
import logging as log
from datetime import datetime,timedelta
import discord
import os
from .utils import checks
from run import UKGBot


import asyncio


class Pinner():
    """Pins messages to a specific channel."""

    def __init__(self, bot: UKGBot):
        self.bot = bot



    async def on_message(self, message):
        """Listen for message then pin it"""
        try:
            guild = message.guild
            channel = get(message.guild.channels, name="gtky")
            pins = await message.channel.pins()
            if message.channel == channel and message.type != discord.MessageType.pins_add:
                if len(pins) == 20:
                    await message.unpin(pins[-1])
                await asyncio.sleep(3) 
                await message.pin()

        except discord.Forbidden:
            print("No permissions to do that!")


    def setup(bot):
        """Setup function"""
        to_add = Pinner(bot)
        bot.add_listener(to_add.on_message, 'on_message')
        bot.add_cog(to_add)

推荐答案

您正在尝试访问某些对象的 channels 属性,但是该对象为 None ==其他语言为空.

You are trying to access the channels property of some object, but that object is None == Null in other languages.

在您的代码中,您唯一引用渠道的地方是 message.guild.channels ,在 channel = get(message.guild.channels,name ="gtky")行,因此消息对象的行会属性为 None

From your code the only place you reference channels is message.guild.channels, in the channel = get(message.guild.channels, name="gtky") line, so the guild property of the message object is None

这篇关于AttributeError:"NoneType"对象没有属性"channels"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆