我正在嘗試使用一個單獨的類來裝滿我的函式,這樣 index.js 就不會變得混亂。我遇到的問題是我的新 lib.js 檔案無法與 discord.js 一起使用。我計劃添加多個更復雜的功能,因此替換lib.start()為msg.channel.send('Game Started')并不能解決我的問題。有沒有辦法讓 discord.js 命令在 lib.js 中作業,以便我可以將它們呼叫到 index.js 中?
索引.js
const Discord = require('discord.js')
const client = new Discord.Client();
const lib = require("./classes/lib");
const { token } = require('./Data/config.json');
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
})
client.on('message', async msg => {
if(msg.content.startsWith("m!")) {
const command = msg.content.substring(2)
switch(command) {
//Calling 'start()'
case "start game" : lib.start(); break;
default: msg.channel.send('Unknown Command');
}
}
})
client.login(token)
庫.js
function start() {
msg.channel.send('Game Started'); //Trying to get this to work
}
module.exports = {start};
uj5u.com熱心網友回復:
在函式中傳入對訊息的參考
client.on('message', async msg => {
if(msg.content.startsWith("m!")) {
const command = msg.content.substring(2)
switch(command) {
//Calling 'start()'
case "start game" : lib.start(msg); break;
default: msg.channel.send('Unknown Command');
}
}
})
function start(msg) {
msg.channel.send('Game Started');
}
module.exports = {start};
您可能還想要 makestart和進一步的功能async,只需await在呼叫它們時記住它們。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/387361.html
標籤:javascript 节点.js 功能 不和谐 不和谐.js
