問題描述
我正在用一個簡單的命令處理程序制作一個不和諧的機器人.我以前從未使用過命令處理程序,所以我對這類函數和類似的東西不太了解.我收到一個錯誤,說執行未定義,我不知道如何解決.代碼:
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模板網!