我現在正在用java 腳本撰寫一個Discord 機器人。我使用MongoDB為我的服務器中的每個用戶存盤一個“熱”值。(每當用戶做錯事并在一段時間后再次取消時,“加熱系統”會增加一定的百分比,更具體地說:在我的示例中每分鐘 -5%)。
為此,我想Model.updateMany()回圈使用。正如您在下面看到的,我使用 filter 引數來查找與我的服務器相關的每個檔案。現在的問題是我如何才能從存盤的價值中扣除這 5%,因為它不是靜態的,而是動態的。
const { Client } = require('discord.js');
const mongoose = require('mongoose');
//using modules and event handlers
module.exports = {
name: 'ready',
/**
* @param {Client} client
*/
async execute(client) {
const heatdb = require('/app/models/heatDB.js');
//how often the loop should pe repeated
const interval = 10 * 60 * 1000;
console.log('Client ready.')
//connnecting my data
mongoose.connect(
'I put the mongoose link here', {
useNewUrlParser: true,
useUnifiedTopology: true
}).then(() => {
console.log('Data connected.')
}).catch((err) => {
console.log(err)
});
//loop begins here
setInterval(function(){
//filtered so that it's only searching for documents having the guild ID in it
heatdb.updateMany({ GuildID: "000000000000000000"}, {Heat: parseInt(/*needed value*/) - 5}})
}, interval);
},
};
還可以看看我的模型是如何在下面構建的:
const { Schema, model } = require('mongoose');
module.exports = model("heatDB", new Schema({
GuildID: String,
UserID: String,
Heat: String,
}))
如果您需要任何其他幫助我,請告訴我。
先感謝您,
九頭蛇
uj5u.com熱心網友回復:
如果您的熱值是架構中的數字而不是字串,那么您可以試試這個 -
heatdb.updateMany({ GuildID: "000000000000000000"},{$mul:{"Heat":0.95}})
說明:-您想每次減少 5% 的熱量,然后您可以使用mul
操作員并將您的熱量值設定為當前值的 95%。每次都會給你5%的減免。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/406693.html
標籤:
