Discord Bot 使用斜杠命令

Rei*_*ing 6 bots node.js discord.js

我想让一个机器人使用另一个机器人的斜杠命令。如果我发送类似 /help 的消息,它将不会使用斜杠功能,而是只会发送“/help”(不使用斜杠命令功能)。有谁知道这是否可能?

Kas*_*spr 0

假设我的评论就是您正在寻找的内容,这应该可以解决您的疑问。

// Bot 1
const {
    Client
} = require("discord.js")

const client = new Client({
    intents: '...'
})
const prefix = '/'

client.on('messageCreate', async message => {
    if (message.content.startsWith(prefix) && !message.author.bot) {
        const command = `/${message.content}`
        const response = message.reply(`${message.author}, relaying command`)

        await response.then(message.channel.send(command))
    }
})
Run Code Online (Sandbox Code Playgroud)
// Bot 2
const {
    Client
} = require("discord.js")

const client = new Client({
    intents: '...'
})
const bot_1_id = '4654564984981981894654'
const prefix = '/'

client.on('messageCreate', async message => {
    if (message.author.bot && message.author.id === bot_1_id && message.content.startsWith(prefix)) {
        const args = message.content.slice(prefix.length).trim().split(/ +/)
        const cmd = args.shift().toLowerCase()
        const command = client.commands.get(cmd)

        // if you don't have command files

        if (cmd === 'help') {
            message.channel.send(`${message.author} I recognized your help command`)
            return
        }
        // if you have a command files see below script for file

        if (!command) return
        
        try {
            command.execute(message, args)
        } catch (error) {
            console.error(error)
            message.reply(`Couldn't run command`)
        }
    } else {
        return
    }
})
Run Code Online (Sandbox Code Playgroud)

如果您想/确实使用命令文件,则可选

// Command File
module.exports = {
    name: 'help',
    description: 'help command',
    async execute(message, args) {
        message.channel.send(`${message.author} I recognized your help command`)
        return
    }
}
Run Code Online (Sandbox Code Playgroud)