A
A
askalidov2021-10-28 23:04:58
Python
askalidov, 2021-10-28 23:04:58

If there is on_message(), @bot.command does not work, what should I do?

Thought await bot.process_commands(message) would help, but alas
Here is a piece of code

import discord
from discord import utils, client
from discord.ext import commands
from discord.ext.commands import bot, has_permissions
import config

intents = discord.Intents.all()
bot = commands.Bot(command_prefix='!', intents=discord.Intents.all())


class MyClient(discord.Client):
    @bot.event
    async def on_ready(self):
        print('Logged on as {0}!'.format(self.user))

    @bot.event
    async def on_message(self, message):
        author = message.author
        channel = message.channel
        content = message.content

        # игнорировать сообщения бота
        if author == client.user:
            return
        if content.startswith("hi"):
            await channel.send('Hello!')
        elif content.startswith("bye"):
            await channel.send("Goodbye!")
        elif content.startswith("!clear"):
            print('Ok, it will be done')
            await message.channel.purge(limit=5)
            
        await bot.process_commands(message)

    @bot.command()
    @commands.has_permissions(kick_members=True)
    async def kick(self, ctx, user: discord.Member, *, reason=None):
        await user.kick(reason=reason)
        await ctx.send(f"{user} have been kicked successfully!")


# RUN
client = MyClient(intents=intents)
client.run(config.TOKEN)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
shurshur, 2021-10-29
@askalidov

Now there is some mess in the code. Why use methods with decorators from a previously created instance of another Client descendant, commands.Bot, inside the MyClient class, which is derived from Client? It doesn't make any sense. Remove the MyClient class altogether, and move all its methods to a higher level (with the corresponding removal of self everywhere in the arguments and replacing self with bot in the code). Instead of client.run, make bot.run.

S
Shandy, 2021-10-28
@MrShandy

You need to learn Python. You await bot.process_commands(message)are outside the on_message function

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question