尝试使用 mongdb 在 discord.js 中查找用户被静音的次数

Posted

技术标签:

【中文标题】尝试使用 mongdb 在 discord.js 中查找用户被静音的次数【英文标题】:Trying to look up how many times a user has been muted in discord.js using mongdb 【发布时间】:2019-02-20 12:49:32 【问题描述】:

我的完整代码: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!

【问题讨论】:

最好不要链接到您的代码,而是创建一个问题中出现的 MCVE,这可能会对您有所帮助:editing-help 和 MCVE 【参考方案1】:

你可以试试这样的:

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

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

【讨论】:

以上是关于尝试使用 mongdb 在 discord.js 中查找用户被静音的次数的主要内容,如果未能解决你的问题,请参考以下文章

Discord.js 尝试在用户加入语音频道时发送消息

Discord.js - 尝试等待异步进程并允许递归调用包装器

尝试使用 discord.js 将 client.reply 作为嵌入消息发送时出现问题

尝试在 Discord.js 中播放音频文件

尝试使用 Discord.js 安装 nodemon 时出错 [重复]

如何使用 Discord.js 发布到 Discord Webhook(代码 400 错误请求)?