Discord.py error in on_message

Asked

Viewed 443 times

0

I was programming and came across the following error, if I have only @client.command(), codes work, but if you already have a @client.event with on_message, it works only the event.

I already tried to put the await Bot.process_commands(message)/await client.process_commands(message) in the last line of on_message, but it didn’t work.

my code:

import discord as d
from discord.ext import  commands
import asyncio
import random


client = commands.Bot(command_prefix='!', help_command=None)
Bot = commands.Bot(command_prefix='!', help_command=None)
bot = commands.Bot(command_prefix='!', help_command=None)


@client.event


async def on_ready():

    print('-----------------------------------------')
    print('Seja Bem-Vindo Genesis, Mais Info Abaixo:')
    print('Bot Atual:')
    print(client.user.name)
    print('ID do Bot Atual:')
    print(client.user.id)
    print('-----------------------------------------')


@client.event
async def ch_pr():
    await client.wait_until_ready()

    statuses = ["Bot Criado por Genesis", "Genesis Lindo", "Precisa de Ajuda? Digite !ajuda"]

    while not client.is_closed():

        status = random.choice(statuses)

        await client.change_presence(activity=d.Game(name=status))

        await asyncio.sleep(10)

client.loop.create_task(ch_pr())


@client.event
async def on_message(message):
        if message.content.startswith('!ajuda'):
            channel = message.channel
            await channel.send("```Parece que você não sabe me usar, confira abaixo como me usar:\n----------Comandos Úteis----------\n!criarcanalt (Nome do Canal) - Cria um canal de Texto\n!criarcanalv (Nome do Canal) - Cria um canal de Voz\n!ping - Mostra o ping do bot```")
            await channel.send("```----------Comandos Adm----------\n!kick (Nome do Usuário) - Kicka o usuário do servidor\n!ban (Nome do Usuário) - Aplica um Ban no usuário do servidor```")
            await client.process_commands(message)


@client.command()
async def delcanalt(ctx, channel: d.TextChannel):
    mbed = d.Embed(
        title='Sucesso',
        description=f'O canal: **{channel}** foi deletado.',
    )
    if ctx.author.guild_permissions.manage_channels:
        await ctx.send(embed=mbed)
        await channel.delete()

@client.command()
async def delcanalv(ctx, channel: d.VoiceChannel):
    mbed = d.Embed(
        title='Sucesso',
        description=f'O canal: **{channel}** foi deletado.',
    )
    if ctx.author.guild_permissions.manage_channels:
        await ctx.send(embed=mbed)
        await channel.delete()


@client.command()
async def criarcanalt(ctx, channelName):
    guild = ctx.guild

    mbed = d.Embed(
        title = 'Sucesso',
        description = 'O canal **{}** foi criado.'.format(channelName)
    )

    if ctx.author.guild_permissions.manage_channels:
        await guild.create_text_channel(name='{}'.format(channelName))
        await ctx.send(embed=mbed)

@client.command()
async def criarcanalv(ctx, channelName):
    guild = ctx.guild

    mbed = d.Embed(
        title = 'Sucesso',
        description = 'O canal **{}** foi criado.'.format(channelName)
    )

    if ctx.author.guild_permissions.manage_channels:
        await guild.create_voice_channel(name='{}'.format(channelName))
        await ctx.send(embed=mbed)



client.run("TOKEN")
  • welcome to stackoverflow! please be more specific. don’t expect someone to read all your code and understand all the logic you’ve developed to help you. Be objective. What’s the problem? what did you expect to happen? what did you try to do? questions like: "python error" get a rain of slide and are usually erased.

  • well, I was hoping that if I put the client.process_commands, I would process all the commands, but nothing happens, but when I remove this help Event, everything works, but if I add again everything to, there is no error console

1 answer

0

It’s too late, but I still think other people can go through this. I believe the problem was indentation, you put the

await client.process_commands(message)

within the IF condition, ie, would only process the commands if the if condition was met.

The right thing would be:

@client.event
async def on_message(message):
    if message.content.startswith('!ajuda'):
        channel = message.channel
        await channel.send("```Parece que você não sabe me usar, confira abaixo como me usar:\n----------Comandos Úteis----------\n!criarcanalt (Nome do Canal) - Cria um canal de Texto\n!criarcanalv (Nome do Canal) - Cria um canal de Voz\n!ping - Mostra o ping do bot```")
        await channel.send("```----------Comandos Adm----------\n!kick (Nome do Usuário) - Kicka o usuário do servidor\n!ban (Nome do Usuário) - Aplica um Ban no usuário do servidor```")

    # Fora do if
    await client.process_commands(message)

Be sure to put Discord.py and Discord in the category of your question, I believe they will answer you much faster next time. Ah, I also recommend you see Discord.py’s Tasks to replace ch_pr, check here: https://discordpy.readthedocs.io/en/stable/ext/tasks/index.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.