-1

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)
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
Shan Jameson
  • 75
  • 1
  • 4
  • 13

2 Answers2

1

This is happening because message.guild is None. guild is None because private messages, direct messages between two users, do not go through a guild.

If your bot sends or receives any private messages, those messages will have None as their message.guild attributes.

Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
1

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

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