尝试使用mongdb在discord.js中查找用户已静音多少次

时间:2018-09-16 11:03:47

标签: mongodb discord.js

我的完整代码:http://hatebin.com/zetpnqyqus

基本上,每当用户被静音时,他们都会以ID静音级别和静音时间进入数据库,然后尝试在有人被静音时在数据库上查找以查看他们在数据库上的次数然后要么将console.log记录下来,要么将其添加到静音消息中。

应使用最少的代码:

const Discord = require("discord.js");
var MongoClient = require('mongodb').MongoClient;
const mongoose = require('mongoose');
const ms = require("ms");
const fs = require("fs");
const mute = require("../models/mutes.js");
 //mongoose.connect("mongodb://localhost/ReportsMutes", { useNewUrlParser: true });
 const moment = require ('moment')






module.exports.run = async (bot, message, args) => {

  MongoClient.connect("mongodb://localhost:27017/ReportsMutes", function(err, db) {
    if(!err) {
      console.log("We are connected");
    }
  });


    let tomute = message.guild.member(message.mentions.users.first() || message.guild.members.get(args[0]));
    if(!tomute) return message.reply("Gimmie a user to mute or i mute u");

    if(message.member.roles.find(r=> r.name === "Mod")){

    // if(tomute.hasPermission("MANAGE_MESSAGES")) return message.reply("U dont have perms innit");
    let muterole = message.guild.roles.find(r => r.name === "muted");

  let time = message.createdAt
         //finding the user in database


    //start of create role

    if(!muterole){
      try{
        muterole = await message.guild.createRole({
          name: "muted",
          color: "#000000",
          permissions:[]
        })
        message.guild.channels.forEach(async (channel, id) => {
          await channel.overwritePermissions(muterole, {
            SEND_MESSAGES: false,
            ADD_REACTIONS: true
          });
        });
      }catch(e){
        console.log(e.stack);
      }
    }
    //end of create role

    let mutetime = args[1];
    if(!mutetime) return message.reply("How long? Hello???");

    await(tomute.addRole(muterole.id));
    message.reply(`<@${tomute.id}> has been muted for ${ms(ms(mutetime))} l8a nerd`);
    let mutechannel = message.guild.channels.find(c => c.name === "gags-and-bans")
    mutechannel.send(`<@${tomute.id}> has been muted for ${ms(ms(mutetime))} `)

    db.mutes.find({"mUsername" : `${tomute.username}`}).count() 


 mutelegnth = ms(ms(mutetime))
 created = message.createdAt

    setTimeout(function(){
      tomute.removeRole(muterole.id);
      message.channel.send(`<@${tomute.id}> has been unmuted!`); 
    }, ms(mutetime));

    }
    else{
        message.channel.send("Only Mods can mute people, get gud fam");

    }

    const Mute = new mute({
      _id: mongoose.Types.ObjectId(),
      mUsername: tomute.user.username,
      legnth: mutelegnth,
      time:  moment(message.createdAt).format('DD YY, h:mm: a')

    });

    Mute.save()
    .then(result => console.log(result))
    .catch(err => console.log(err));




} // closes module.exports
module.exports.help = {
    name: "mute" 
  }


  //they have been muted ${userr} time(s) before!

1 个答案:

答案 0 :(得分:0)

您可以尝试这样的事情:

mute.countDocuments({ mUsername: "username" }, function(err, num){
    console.log(`User muted: ${num} times`)
})

https://mongoosejs.com/docs/api.html#model_Model.countDocuments