我想要一个每1分钟能坐在一个语音通道中的硬币系统

时间:2020-05-21 03:26:59

标签: node.js discord.js

我做到了,当用户进入并离开语音通道时,该漫游器会修复,我希望坐在那里的每一分钟都让参与者滴200枚硬币,我已经在聊天中做到了。就我的想法而言,我需要“ ms”和“ fs”来捕获一分钟(60000毫秒),我还有一个文件,其中追逐所有硬币,我希望每分钟的数量去那里

const Discord = require('discord.js');
const fs = require("fs");
const ms = require("ms");
const token = "my token here";
const prefix = "!";
const bot = new Discord.Client({disableEveryone: true});

let coins = require("./coins.json");


bot.on("message", message => {

    if (message.author.bot) return;
    if (message.channel.type === "dm") return;

    if(!coins[message.author.id]) {
      coins[message.author.id] = {
        coins: 0
      };
    }


    let coinAmt = Math.floor(Math.random() * 70) + 25;
    let baseAmt = Math.floor(Math.random() * 15) + 12;

    if(coinAmt === baseAmt){
      coins[message.author.id] = {
        coins: coins[message.author.id].coins + coinAmt
      };

      fs.writeFile("./coins.json", JSON.stringify(coins), (err) => {
        if (err) console.log(err)
    });

    let coinEmbed = new Discord.MessageEmbed()

    .setColor("RANDOM")
    .addField("Excellent! ?", `${message.author},  \`${coinAmt}\` coins added to your balance`)

    message.channel.send(coinEmbed)
  }

});



bot.on('voiceStateUpdate', (oldMember, newMember) => {
    console.log('enter');

    if (oldMember.selfMute === true) {
        console.log('muted');
        }

  if (newMember.selfMute === true) {
    console.log('muted-2');
    }

});


如果您能提供帮助,我将非常感激,因为这对您来说只有几分钟,对我来说非常有好处


1 个答案:

答案 0 :(得分:0)

这是我要解决的方法:

  • 每次成员加入频道时,您将Date保存在单独的对象中
  • 当他们离开时,您会看到当前日期和您保存的日期之间有什么区别,并计算硬币以提供给他们

问题主要是关于如何确定用户是否加入或退出语音聊天的:您可以通过查看其先前的或当前的VoiceState是否具有频道来进行检查。
这是一个示例:

let voiceStates = {}

client.on('voiceStateUpdate', (oldState, newState) => {
  let { id } = oldState // This is the user's ID

  if (!oldState.channel) {
    // The user has joined a voice channel
    voiceStates[id] = new Date()
  } else if (!newState.channel) {
    // The user has left the voice chat (and hasn't just switched channel)
    let now = new Date()
    let joined = voiceStates[id] || new Date()

    // This will be the difference in milliseconds
    let dateDiff = now.getTime() - joined.getTime()
    if (dateDiff > 60 * 1000) {
      // The user has spent more than 1 minute in voice channels
      // You can now do your math and assign the coins as you wish
    }
  }
})