I want a coin system for seating in a voice channel per 1 minute

守給你的承諾、 提交于 2020-06-17 09:44:07

问题


I did that the bot fixes when the user enters and leaves the voice channel, i want for each minute of sitting there a participant dripping 200 coins, i already did so with the chat. For my idea, I need "ms" and "fs" to capture a minute (60000 milliseconds) I also have a file where all the coins are chased, I want the amount per minute to go there

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');
    }

});


I will be very grateful if you help, for you it is a few minutes and for me it is of great benefit



回答1:


Here's how I would solve it:

  • Every time a member joins a channel, you save the Date in a separate object
  • When they leave, you see what's the difference between the current Date and the one you saved and calculate the coins to give them

The questions are mostly about how to determine if a user has joined or left the voice chat: you can check that by seeing if either their previous or current VoiceState has a channel.
Here's an example:

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
    }
  }
})


来源:https://stackoverflow.com/questions/61926688/i-want-a-coin-system-for-seating-in-a-voice-channel-per-1-minute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!