pythonpython-3.xdiscord.pysubclassing

Can I make a sub class of discord.client?


I'm trying to program a discord bot with discord.py. I want to make a subclass of the discord.client class so I can add some attributes to the class. Unfortunately I keep getting this error:

Traceback (most recent call last): File "C:/Users/laser/PycharmProjects/DiscordNut/main.py", line 5, in class Bot(client): TypeError: module.init() takes at most 2 arguments (3 given)

Here's my code:

from discord import client
import random as rand


class Bot(client):
    pass


    async def roll(self, *args):
        input = args[0]
        target = input.find('d')
        numDice = int(input[:target])
        diceSize = int(input[target + 1:])
        output = 0
        for i in range(numDice):
            output += rand.randint(1, diceSize)

        await self.send_message(self.get_channel("505154560726269953"), "You rolled a: " + str(output))


def parse(content, delimeter):
    target = content.find(delimeter)
    if content.find("\"") == 0:
        content = content[1:]
        target = content.find("\"")
    if target == 0:
        return parse(content[1:], delimeter)
    elif target < 0:
        return [content]
    else:
        parsed = []
        parsed.append(content[:target])
        return parsed + parse(content[target + 1:], delimeter)


client = Bot()


@client.event
async def on_ready():
    client.users = [i for i in client.get_all_memebers()]
    print(client.users)


@client.event
async def on_message(message):
    parsed = None
    if message.startswith('!'):
        parsed = parse(message.content[1:], " ")
    if parsed[0] in client.commands:
        args = [i for i in parsed if i != parsed[0]]
        client.commands[parsed[0]](args)


client.run("<TOKEN>")

Solution

  • You can do this by inheriting commands.Bot

    from discord.ext import commands
    
    class MyBot(commands.Bot):
        """ 
        you can still access all attributes of commands.Bot
        """
        
    # create the bot instance
    bot = MyBot(
        # still able to access the same attributes
        command_prefix = '',
        intents = intents
    )
    

    What makes commands.Bot much better than using Client is given by the discord.py documentation

    This class is a subclass of discord.Client and as a result anything that you can do with a discord.Client you can do with this bot.