連接資料庫.js
這是我要從中回傳字串的檔案...
const mongoose = require('mongoose')
require('dotenv').config()
const DB = process.env.DB
function connectDB(client) {
if (!DB) return
mongoose.connect(DB, {
useNewUrlParser: true,
useUnifiedTopology: true
}).then(() => {
console.log(`${client.user.username} is now connected to database. . .`)
}).catch((err) => {
console.log(err)
})
}
function disconnectDB(client) {
if (!DB) return
mongoose.disconnect(DB, {
useNewUrlParser: true,
useUnifiedTopology: true
}).then(() => {
console.log(`${client.user.username} is now Disconnected to database. . .`)
}).catch((err) => {
console.log(err)
})
}
module.exports = {
connectDB,
disconnectDB
}
我想從那時回傳一個字串并在 connectDB() 函式以及 disconnectDB() 中捕獲
就像是:
function connectDB(client) {
if (!DB) return
mongoose.connect(DB, {
useNewUrlParser: true,
useUnifiedTopology: true
}).then(() => {
console.log(`${client.user.username} is now connected to database. . .`)
return '?? Connected' //If succesfully connects
}).catch((err) => {
console.log(err)
return err.toString() //If some error occures
})
}
function disconnectDB(client) {
if (!DB) return
mongoose.disconnect(DB, {
useNewUrlParser: true,
useUnifiedTopology: true
}).then(() => {
console.log(`${client.user.username} is now Disconnected to database. . .`)
return '?? Disconnected' //If succesfully disconnects
}).catch((err) => {
console.log(err)
return err.toString() //If some error occures
})
}
然后我想在另一個檔案中訪問這些回傳的字串:
切換.js
const {
connectDB,
disconnectDB
} = require('../../utils/connectDB')
async execute(interaction, client) {
const connect = interaction.options.get('connect').value
var embRes = ''
var clr = ''
if (connect) {
embRes = connectDB(client)
if (embRes == '?? Connected') {
clr = 'Aqua'
} else {
clr = 'Grey'
}
} else if (!connect) {
embRes = disconnectDB(client)
if (embRes == '?? Disconnected') {
clr = 'Red'
} else {
clr = 'NotQuiteBlack'
}
}
請注意:我處于開發的早期階段意味著我知道的不多,所以請盡可能解釋答案!
感謝您的幫助!
uj5u.com熱心網友回復:
我認為您可以用承諾包裝一層
uj5u.com熱心網友回復:
我不知道你是如何toggle.js構造的,但你可以使用switch功能。
在你的toggle.js:
//if your command starts with ${prefix}admin
//Imagine that your command is *admin
//Then call it inside of your module.exports
const connectDB = require('../../utils/connectDB'); // I'm assuming that this is the extension folder.
module.exports = {
name: "admin",
async execute(client, message, args, prefix) {
//Take note that this is how I call my commands.
if(args[0] == "connect") {
switch(this.name) {
case this.name: connectDB.connectDB(client, message, args, prefix); break;
default: message.channel.send("SOMETHING WENT WRONG");
}
} else if(args[0] == "disconnect") {
switch(this.name) {
case this.name: connectDB.disconnectDB(client, message, args, prefix); break;
default: message.channel.send("SOMETHING WENT WRONG");
}
}
}
}
然后在您connectDB.js呼叫與案例相同的功能
function connectDB(client, message, args, prefix) {
//your codes here
}
function disconnectDB(client, message, args, prefix) {
//your codes here
}
進行深入的解釋。如果您注意到有2 個 connectDB。
第一個connectDB是您的檔案。那么第二個connectDB是你的function
PS:因為您正在使用互動。只需洗掉message, args, prefix并替換為interaction
我不知道這是否適用于互動。
uj5u.com熱心網友回復:
嘗試這個
function connectDB(client) {
if (!DB) return
return new Promise((resolve,_) => {
mongoose.connect(DB, {
useNewUrlParser: true,
useUnifiedTopology: true
}).then(() => {
console.log(`${client.user.username} is now connected to database. . .`)
resolve('?? Connected' ) //If succesfully connects
}).catch((err) => {
console.log(err)
resolve(err.toString()) //If some error occures
})
})
}
async execute(interaction, client) {
const embRes = await connectDB(client)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/504254.html
標籤:javascript 猫鼬 不和谐 不和谐.js 机器人
