Home > other >  Why is my command with handler not working?
Why is my command with handler not working?

Time:11-23

I am using discord.js v13, I am just starting the code. When trying to run the "beep" command, it does not respond in the chat, there are no errors in the console, nor does it crash the bot, just nothing happens.

This is my index.js:

const config = require("./config.json")
const fs = require("fs")
const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });

//////////////////////////////////////////////////////////////////////////////////////////
client.on("ready", () => {
    console.log(`[カノンの準備ができました!]`)
    client.user.setActivity({ type: "LISTENING", name: `Love Spiral Tower` })
})
//////////////////////////////////////////////////////////////////////////////////////////

client.config = require("./config.json")

client.commands = new Discord.Collection()
client.aliases = new Discord.Collection()



fs.readdir("./コマンド/", (err, files) => {
    if (err) return console.log("このコマンドの処理中にエラーが発生しました")
    const jsFiles = files.filter(f => f.split(".").pop() === "js")
    if (jsFiles.length <= 0) return console.log("このコマンドの処理中にエラーが発生しました")
    jsFiles.forEach(file => {
        const cmd = require(`./コマンド/${file}`)
        console.log(`ファイルが見つかりました!: ${file}`)
        client.commands.set(cmd.name, cmd)
        if (cmd.aliases) cmd.aliases.forEach(alias => client.aliases.set(alias, cmd.name))
    })
})

client.on("message", async message => {
    const prefix = config.prefix
    if (!message.content.startsWith(prefix)) return
    const args = message.content.slice(prefix.length).trim().split(/  /g)
    const command = args.shift().toLowerCase()
    const cmd = client.commands.get(command) || client.commands.get(client.aliases.get(command))
    if (!cmd) return
    try {
        cmd.run(client, message, args)
    } catch (e) {
        console.error(e)
    }
})

client.login(config.token);

And my command "beep":

const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });

module.exports = {
    name: "beep",
    alias: ["b"],

    execute (client, message, args){

        message.channel.send("Boop!")
    }
} 

Help pls.

CodePudding user response:

Index.js

The client.on("message", async message => is used only in discord.js v12 and and maybe the client won't answer.

You should to use messageCreate instead message if you're using v13.

Example:

client.on("messageCreate", async message => {
const prefix = config.prefix
if (!message.content.startsWith(prefix)) return
const args = message.content.slice(prefix.length).trim().split(/  /g)
const command = args.shift().toLowerCase()
const cmd = client.commands.get(command) || client.commands.get(client.aliases.get(command))
if (!cmd) return
try {
    cmd.run(client, message, args)
} catch (e) {
    console.error(e)
}
})

The command

Have you try to use async run instead execute?

if not then use this code at the bottom of my post or use async run(client, message, args)

const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });

module.exports = {
    name: "beep",
    alias: ["b"],

    async run(client, message, args){

        message.channel.send("Boop!")
    }
}

CodePudding user response:

Your bot does not detect messages. You need GUILD_MESSAGES intents

const client = new Client({
  intents: [
    Intents.FLAGS.GUILDS,
    Intents.FLAGS.GUILD_MESSAGES
  ]
})

Another thing is that you should not create a new client for each command. This just creates a lot of unnecessary clients, when you can just use the one receiving the event.

  • Related