1

How to create a Discord.py command?

In fact, it doesn't show anything in the console at all, what should I do?

There's no error, nothing is executing.

import discord
import time

from discord.ext import commands

client = commands.Bot(command_prefix='/')

# This detects when the bot started.

@client.event
async def on_ready():

    print(f'{client.user.name} is ready!')

# This detects when someone sends a message.

@client.event
async def on_message(message):

    if message.author == client.user:

        return

    if 'fuck' in message.content:

        await message.delete()

        async with message.channel.typing():

            time.sleep(1)

        await message.channel.send('You cannot swear in this discord server!')

    if 'shit' in message.content:

        await message.delete()

        async with message.channel.typing():

            time.sleep(1)

        await message.channel.send('You cannot swear in this discord server!')

    if 'bitch' in message.content:

        await message.delete()

        async with message.channel.typing():

            time.sleep(1)

        await message.channel.send('You cannot swear in this discord server!')

    if 'https://' in message.content:

        await message.delete()

        async with message.channel.typing():

            time.sleep(1)

        await message.channel.send('You cannot send links in this discord server!')

# This cteates a command.

@commands.command()
async def test(ctx, arg):

    async with message.channel.typing():

        time.sleep(1)

    await ctx.send(arg)

client.add_command(test)

client.run('TOKEN')

1 Answer 1

0

You need to add client.process_commands at the end of the on_message event.

async def on_message(message):
    # ...

    await client.process_commands(message)

Another thing wrong in your code is that you're not defining message in the test command, you can fix it by using the Context.message attribute

async with ctx.message.typing():
    # ...

# Or
async with ctx.typing():
    # ...

Also you can simply use the client.command decorator instead of commands.command so you don't need to add the line client.add_command

@client.command()
async def test(ctx, arg):
    # ...

Reference:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.