简体   繁体   中英

How do I create multiple commands using command_prefix=‘!’?

I am creating a Discord bot and am not experiencing any success creating custom commands with a command_prefix='.'.

Eg I want to create, using the,-sign as the prefix. multiple commands that can even accept other arguments if you know exactly what you want within that command: Something like this, .creeds -or if you know what creed, you could use “!creeds Nicene” or “!creeds Athanasian” or if you want a list just use the base command “!creeds” and it will provide a list.

I have been told to use cogs and have not had any success with those, yet. Though I know I will need a database (using SQLite3) which I am also working on.

@jh316, all I have is this so far:

@client.event #basic event using a !command.
async def on_message(msg):
    if msg.author != client.user:
        if msg.content.lower().startswith("!hi"):
            await msg.channel.send(f"Hi, {msg.author.display_name}!")

Something like this? https://onecompiler.com/python/3yc5xaqce

def purify(string):
    return string.lower().strip()
    
def handleMessage(msg):
    prefix = "!"
    delimiter = ":"
    
    if msg[0:1] != "!":
        print("This is not a command")
        #exit
    
    splitMessage = msg[1:len(msg)].split(":")
    cmd = purify(splitMessage[0])
    param = "" if len(splitMessage) <= 1 else purify(splitMessage[1])
    
    response = executeCommand(cmd, param)
    
    #Now use the response
    print(response)
    
def executeCommand(command, parameter):
    #Actually use command here...
    response = "Sorry, we don't have that one!"
    if command == "creed":
        if parameter == "nicene":
            response = "We believe in one God, the Father almighty, maker of heaven and earth, of all things visible and invisible..."
        elif parameter == "apostles":
            response = "I believe in God, the Father almighty, creator of heaven and earth."

    return response

#Examples
handleMessage("!creed: Nicene")
handleMessage("!creed: Apostles")
handleMessage("!creed: Athanasian")

Obviously you would want to remove the examples and put a single handleMessage reference in your discord client event message handler. Does this help?

Anyway, I love the idea!

Are you using a Client or a Bot? You should be importing from discord.ext.commands import Bot . The Bot class has a decorator to create commands:

from discord import Intents
from discord.ext.commands import Bot

TOKEN = "your secret bot token from the Discord developer page"

bot = Bot(command_prefix="!", intents=Intents.all())

@bot.command()
async def settings(context):
    await context.reply("You executed command `settings`!")

@bot.command()
async def hello(context):
    await context.reply("You executed command `hello`!")

bot.run(TOKEN)

Now your users will be able to type !settings and !hello in Discord to trigger these commands.

Be careful though, because if you define your own on_message like you did in your code, these commands will not automatically be called. You have to invoke them yourself:

@bot.event
async def on_message(message):
    context = await bot.get_context(message)
    await bot.invoke(context)

There is plenty of information online about how these commands work and how you can pass in arguments and cast them to the correct type automatically. This tutorial is one of the first Google Search results when I search "discord.py commands", and it looks like a decent tutorial that you could read.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM