亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Discord.js v12,在命令并執行后讀取 args 的潛在拆分錯誤

Discord.js v12,在命令并執行后讀取 args 的潛在拆分錯誤

莫回無 2023-02-24 17:07:01
所以我收到但找不到的錯誤是在空格后接受任何參數作為有效命令。我相信這可能是一個.split()錯誤,因為如果您完全匹配參數,它將產生不同的輸出?,F在,如果您使用未列出的參數,它仍會生成原始命令!qa=!qa mollusk當參數通過但不存在時,它應該返回一個錯誤,但沒有這樣做。這是我的索引和與復制相關的所有內容:const fs = require('fs');const Discord = require('discord.js');const { prefix, token } = require('./config.json');const featureFiles = fs.readdirSync('./commands/features').filter(file => file.endsWith('.js'));for (const file of featureFiles) {    const command = require(`./commands/features/${file}`);    client.commands.set(command.name, command);}    client.on('message', message => {    if (!message.content.startsWith(prefix) || message.author.bot) return;//.trim() is removed, see notes below on why    const args = message.content.slice(prefix.length).split(/ +/g);    const commandName = args.shift().toLowerCase();    const command = client.commands.get(commandName)        || client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));    if (!command) return;    if (command.guildOnly && message.channel.type !== 'text') {        return message.reply('That command cannot be used inside a DM');    }    if (command.args && !args.length) {        let reply = `You didn't provide any arguments!`;        if (command.usage) {            reply += `\nThe proper usage would be: \`${prefix}${command.name} ${command.usage}\``;        }        return message.channel.send(reply);    }    try {        command.execute(message, client, args);    } catch (error) {        console.error(error);        message.channel.send('Error trying to execute command');    }});client.login(token);我刪除了.trim()它,因為它正在讀取前綴和命令名稱之間的空格,這是我不想要的,所以可以在前綴和命令之間使用 100 個空格,它會執行它。這是我正在構建的模塊:我是否認為它是在中找到的.split()?這讓我很困惑,也許我忽略了它,它也對沒有任何參數的常規命令做同樣的事情。這導致相信我這是在index。?qa alksjdkalsjd如果進行了未指定為 arg 的其他輸入(如 ),我希望它簡單地返回。Discord.js = v12
查看完整描述

1 回答

?
POPMUISE

TA貢獻1765條經驗 獲得超5個贊

你想要做的是像這樣重組你的 if :


if(args.length === 0) { // this runs everytime there are no args provided

    return message.channel.send(data, 'To see focus types, type `!qa [arg]`, example `!qa test`', { split: true });

}

if (args[0] === 'test') {

    return message.channel.send(testing)

}

/* ........... */

return message.channel.send(error); // send a errror here if the args were not handled by the above cases



查看完整回答
反對 回復 2023-02-24
  • 1 回答
  • 0 關注
  • 111 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號