久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

TypeError:無法讀取未定義的“執行"屬性嘗試執

TypeError: Cannot read property of #39;execute#39; of undefined When trying to execute a command file(TypeError:無法讀取未定義的“執行屬性嘗試執行命令文件時)
本文介紹了TypeError:無法讀取未定義的“執行"屬性嘗試執行命令文件時的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在用一個簡單的命令處理程序制作一個不和諧的機器人.我以前從未使用過命令處理程序,所以我對這類函數和類似的東西不太了解.我收到一個錯誤,說執行未定義,我不知道如何解決.代碼:

I am making a discord bot with a simple command handler. I have never used a command handler before so I don't really know a lot about these sorts of functions and things like that. I get the an error saying that execute is undefined, which I do not know how to fix. Code:

module.exports = {Discord : 'Discord.JS'}
module.exports = {client : 'Discord.Client'}

const fs = require('fs');
client.commands = new Discord.Collection();

const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for(const file of commandFiles) {
    const command = require(`./commands/${file}`);

    client.commands.set(command.name, command);
};

client.on('message', msg => {
    let message = '';
    if (msg.channel === msg.author.dmChannel) {
        return
    } else {
            client.commands.get('./commands/allyApplication').execute(message, msg);
    };

    if (msg.content.toLowerCase().startsWith('!accept')) {
        client.commands.get('./commands/acceptAlly').execute(message, msg);
    };

    if (msg.content.toLowerCase().startsWith('!decline')) {
        client.commands.get('./commands/declineAlly').execute(message, msg);
    };

});

這是讀取此內容的腳本的代碼:

This is the code for scripts that read this:

module.exports = {
    name: 'declineAlly',
    description: 'Declines allies.',
    execute(message, msg) {
    REST OF CODE
    }
} 

如果有人知道我該如何解決這個錯誤,那就太好了,因為我是命令處理程序的新手.

If anyone knows how I could possibly fix this error, it would be great since I am new to command handlers.

推薦答案

要回答您的問題,您的代碼不起作用,因為您需要像這樣調用它 client.commands.get('declineAlly').execute(message, msg); 并且你總是運行 client.commands.get('./commands/allyApplication').execute(message, msg); 因為 else,這意味著您的代碼甚至沒有到達您定義命令的地步.此外,您始終將空字符串傳遞給命令.您在這里所做的本身并沒有錯,就是您必須手動將每個命令添加到處理程序.那不是很有效.

To answer your question, your code doesn't work because for one you need to call it like this client.commands.get('declineAlly').execute(message, msg); and you always run client.commands.get('./commands/allyApplication').execute(message, msg); because of the else, meaning that your code doesn't even get to the point where you define your commands. Additionally you always pass an empty string to the command. What you also have done here, which is in itself not wrong, is that you have to manually add each command to the handler. Thats not really efficient.

所以讓我們解決這個問題.

So lets fix that.

讓我們從頂部開始.您將命令設置到集合中的代碼工作正常.因此,讓我們找到問題的根源,即 client.on('message', message 部分.在以下代碼片段中,我總是使用 message 而不是 味精.

Lets start at the top. Your code to set the commands into the collection works fine. So lets get to the root of your issue, the client.on('message', message part. in the following code snippets I always use message instead of msg.

一開始你應該做兩件事.首先檢查頻道是否為DM,如果是則返回.

At the start you should do two things. First check if the channel is a DM and if so return.

if (message.guild === null) {
    return message.reply("Hey there, no reason to DM me anything. I won't answer anyway :wink:"); //example sentence
}

并檢查發送此消息的用戶是否是機器人.這樣其他機器人就不能使用你的了.

And check if the user who sends this message is a bot. That way other bots can't use yours.

if (message.author.bot) return;

接下來您應該設置一個前綴值,在您的情況下為 ! 并檢查消息是否以所述前綴開頭.

What you should do next is set a prefix value, in your case that would be ! and check if a message starts with said prefix.

const prefix = '!';
if (!message.content.startsWith(prefix)) {
    return;
}

現在我們已經檢查了消息是否真的是一個命令,我們可以切掉前綴并將消息的其余部分轉換為我們將調用 args 的數組.

Now that we have checked if the message is actually a command we can slice off the prefix and convert the rest of the message into an array that we will call args.

const args = message.content.slice(prefix.length).trim().split(/ +/g);

接下來,我們需要取出該數組的第一個元素,將其移除并將其存儲為我們的命令值.我們還將其轉換為小寫.

Next we need to take the first element of that array, remove it and store it as our command value. We also convert it to lowercase.

const cmd = args.shift().toLowerCase();

接下來我們需要檢查消息是否真的有一些參數.這很重要,因此如果消息只是一個簡單的 !,則此代碼的其余部分不會被執行.

Next we need to check if the message actually has some arguments. Thats important so the rest of this code doesn't get executed if the message is just a simple !.

if (cmd.length === 0) return;

之后,是時候從我們的命令集合中獲取命令了.

After that it's time to get the command from our command collection.

let command = client.commands.get(cmd);

完成后,我們需要檢查該命令是否確實存在.如果沒有,那么我們返回一個錯誤.

Once that is done we need to check if the command actually exists. If it does not then we return with an error.

if (!command) return message.reply(``${prefix + cmd}` doesn't exist!`);

一旦我們確認該命令存在,就該執行該命令了.

Once we have confirmed that the command exists it's time to execute that command.

command.execute(message, args);

你有它.命令處理程序完成.不過,我們還沒有完成.在您可以使用命令之前,我們需要在此處進行一些更改.

And there you have it. The command handler is finished. We are still not done yet though. Before you can use the commands we need to change something there.

  • 首先,從現在開始,您將使用命令的名稱而不是其他名稱來調用命令,就像您在代碼中使用的那樣.
  • 其次,您需要確保命令的名稱完全小寫.那是因為我們在處理程序中將命令轉換為小寫.

最后我們應該稍微修改一下命令,讓它更容易閱讀.

Lastly we should change the command a little bit so it becomes easier to read.

module.exports = {
    name: 'declineally',
    description: 'Declines allies.',
    execute: (message, msg) => {
        // the rest of your code
    }
} 

在所有這些之后,您的 client.on('message' 事件應該看起來像這樣:

After all of this, your client.on('message' event should look a litte something like this:

client.on('message', message => {
    // check if the message comes through a DM
    if (message.guild === null) {
        return message.reply("Hey there, no reason to DM me anything. I won't answer anyway :wink:");
    }
    // check if the author is a bot
    if (message.author.bot) return;
    
    // set a prefix and check if the message starts with it
    const prefix = "!";
    if (!message.content.startsWith(prefix)) {
        return;
    }

    // slice off the prefix and convert the rest of the message into an array
    const args = message.content.slice(prefix.length).trim().split(/ +/g);
    
    // convert all arguments to lowercase
    const cmd = args.shift().toLowerCase();

    // check if there is a message after the prefix
    if (cmd.length === 0) return;

    // look for the specified command in the collection of commands
    let command = client.commands.get(cmd);

    // if there is no command we return with an error message
    if (!command) return message.reply(``${prefix + cmd}` doesn't exist!`);

    // finally run the command
    command.execute(message, args);

});

這篇關于TypeError:無法讀取未定義的“執行"屬性嘗試執行命令文件時的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

discord.js v12: How do I await for messages in a DM channel?(discord.js v12:我如何等待 DM 頻道中的消息?)
how to make my bot mention the person who gave that bot command(如何讓我的機器人提及發出該機器人命令的人)
How to fix Must use import to load ES Module discord.js(如何修復必須使用導入來加載 ES 模塊 discord.js)
How to list all members from a specific server?(如何列出來自特定服務器的所有成員?)
Discord bot: Fix ‘FFMPEG not found’(Discord bot:修復“找不到 FFMPEG)
Welcome message when joining discord Server using discord.js(使用 discord.js 加入 discord 服務器時的歡迎消息)
主站蜘蛛池模板: 欧美精品在线一区二区三区 | 91精品国产综合久久婷婷香蕉 | 中国一级特黄真人毛片 | 亚洲精品日韩在线 | 国产视频久久 | 一区二区免费在线视频 | 九九热在线观看 | 成人av网站在线观看 | 日韩免费一区二区 | 久久久久久久久久久丰满 | 伊人网综合在线观看 | 久久久久久亚洲精品 | 国产视频三级 | 亚洲精品中文字幕在线观看 | 午夜免费精品视频 | 亚洲精品视频一区二区三区 | 在线播放国产一区二区三区 | 久久出精品 | 日韩精品一区二区三区视频播放 | 欧美成人精品欧美一级 | 99精品视频一区二区三区 | 久久国产精品亚洲 | 天堂免费看片 | 日韩精品 | 午夜天堂精品久久久久 | 欧美日韩电影免费观看 | 成年人精品视频在线观看 | 日韩无 | 嫩草影院网址 | 一区二区三区四区在线 | 欧美影院 | 毛片免费视频 | 伊人网91| 欧美一区二 | 日本欧美在线视频 | 国产精品一区二区视频 | 久久久久成人精品 | 成人免费视频网站 | 国产成人影院 | 国产午夜精品理论片a大结局 | 午夜免费在线电影 |