简体   繁体   中英

Discord.py: First command works, second doesn't

so my first command works but the second doesnt, how can i fix this?

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('bb!help') and isinstance(message.channel, discord.DMChannel):
        await message.channel.send('XXXXXXX')



@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('bb!support') and isinstance(message.channel, discord.DMChannel):
        await message.channel.send('XXXXXXX')

I don't think you can have the same listener multiple times, here's the fixed code

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if isinstance(message.channel, discord.DMChannel):
        if message.content.startswith('bb!help'):
            # here the code

        elif message.content.startswith('bb!support'):
            # here the code

    # if you're using `commands.Bot`
    await client.process_message(message)

Also a better alternative is using commands.Bot as you have an built-in command system, here's a simple example:

import discord
from discord.ext import commands

intents = discord.Intents.default()

bot = command.Bot(command_prefix='bb!', intents=intents)

@bot.command()
@commands.dm_only() # So we can use this command only in DM's
async def help(ctx):
    await ctx.send('getting help')

# To invoke it
# `bb!help` in dm's

bot.run('TOKEN')

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