Home > Back-end >  Errors says change_presence is not an attribute to NoneType when using Discord.py
Errors says change_presence is not an attribute to NoneType when using Discord.py

Time:10-11

I am trying to change the status of my bot to be in playing a game. I used client.change_presence but it seems that it is not an attribute of NoneType.

Here the code:

class Bot(commands.Bot):
    def __init__(self):
        super().__init__(command_prefix=_prefix_callable,
                         help_attrs=dict(hidden=True))
        self.token = credentials['token']
        self.client = discord.Client()

        for extension in INITIAL_EXTENSIONS:
            try:
                self.load_extension(extension)
            except Exception as e:
                print('Failed to load extension {}\n{}: {}'.format(extension, type(e).__name__, e))

    async def on_ready(self):
        print('Logged in as:')
        print('Username: '   self.user.name)
        print('ID: '   str(self.user.id))
        print('------')
        await self.client.change_presence(status=discord.Status.idle, activity=discord.Game(name="can help"))

    async def close(self):
        await super().close()

    def run(self):
        super().run(self.token, reconnect=True)


if __name__ == '__main__':
    bot = Bot()
    bot.run()

And here is the error I keep getting:

Traceback (most recent call last):
  File "C:\Apps\Python\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "D:\Projects\Bot\main.py", line 84, in on_ready
    await self.client.change_presence(status=discord.Status.idle, activity=discord.Game(name="can help"))
  File "C:\Apps\Python\lib\site-packages\discord\client.py", line 1062, in change_presence
    await self.ws.change_presence(activity=activity, status=status, afk=afk)
AttributeError: 'NoneType' object has no attribute 'change_presence'

Thanks.

CodePudding user response:

Bot is the Client subclass. you needn't it. try it:

class Bot(commands.Bot):
    def __init__(self):
        super().__init__(command_prefix=_prefix_callable,
                         help_attrs=dict(hidden=True))
        self.token = credentials['token']
        # self.client = discord.Client() <removed>

        for extension in INITIAL_EXTENSIONS:
            try:
                self.load_extension(extension)
            except Exception as e:
                print('Failed to load extension {}\n{}: {}'.format(extension, type(e).__name__, e))

    async def on_ready(self):
        print('Logged in as:')
        print('Username: '   self.user.name)
        print('ID: '   str(self.user.id))
        print('------')
        # await self.client.change_presence(status=discord.Status.idle, 
        #                                   activity=discord.Game(name="can help")) <old>
        await self.change_presence(status=discord.Status.idle, 
                                   activity=discord.Game(name="can help"))

    # async def close(self):
    #     await super().close() <what different hear?>

    def run(self):
        super().run(self.token, reconnect=True)


if __name__ == '__main__':
    bot = Bot()
    bot.run()
  • Related