问题
I've never had a problem with the say command until I start using modules and cleaned up my code but since I have few my commands have bugged. I'm trying to figure out why it is saying the command.
const commandFiles = fs
.readdirSync("./commands/")
.filter(file => file.endsWith(".js"));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
bot.commands.set(command.name, command);
}
bot.on("message", async message => {
if (!message.content.startsWith(PREFIX)) return;
let args = message.content.substring(PREFIX.length).split(" ");
switch (args[0]) {
case "say":
bot.commands.get("say").execute(message, args);
break;
const Discord = require("discord.js");
module.exports = {
name: "say",
description: "Show's avatar",
async execute(message, args){
const sayMessage = args.join(" ");
message.delete().catch(O_o => {});
message.channel.send(`${sayMessage} - ${message.author}`);
}
}
Thanks in advance!
回答1:
The code is doing exactly what you programmed it to, be it not intended.
let args = message.content.substring(PREFIX.length).split(" "); // args = ['say', 'Hello', 'World']
bot.commands.get("say").execute(message, args); // Passing in the entire args array
const sayMessage = args.join(" "); // sayMessage = 'say Hello World'
One solution of many:
let args = message.content.substring(PREFIX.length).split(" ");
const command = args.splice(0, 1); // args now only contains the arguments
switch (command) {
...
}
回答2:
Here you go this is using the anidiots guidebot boiler plate i highly recommend using it to help you better understand incorporating a command handler into your discord bot.
your going to want to download the repo from here https://github.com/AnIdiotsGuide/guidebot/
- extract it
- run npm install
- in the commands folder add say.js using the code below this is how to properly execute what your trying todo
- by default the prefix is ~ to change it in discord chat run ~conf edit prefix Example: ~conf edit prefix !
- run !say hello world
exports.run = async (client, message, args) => {
const sayMessage = args.join(" ");
message.delete();
message.channel.send(sayMessage + ' - ' + message.author)
};
exports.conf = {
enabled: true,
guildOnly: false,
aliases: [],
permLevel: "User"
};
exports.help = {
name: "say",
category: "Miscelaneous",
description: "say command",
usage: "say"
};```
来源:https://stackoverflow.com/questions/59816652/discord-js-say-command-actually-says-say