【问题标题】:I want a coin system for seating in a voice channel per 1 minute我想要一个用于每 1 分钟在语音频道中就座的硬币系统
【发布时间】:2020-09-07 14:55:17
【问题描述】:

当用户进入和离开语音频道时,我做了这个机器人修复,我希望每分钟坐在那里一个参与者滴 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');
    }

});


如果您能提供帮助,我将不胜感激,对您来说只需几分钟,而对我来说,这将大有裨益


【问题讨论】:

  • 我认为消息上的 fs.writeFile 会导致缓冲区已满后数据丢失,请考虑一些状态并定期同步。然后您可以添加一个刻度(例如精度 1000 毫秒)并为连接的用户消耗一次硬币nowT - userLastDrainedT > 60s

标签: node.js discord.js


【解决方案1】:

以下是我的解决方法:

  • 每次会员加入频道时,您都会将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
    }
  }
})

【讨论】:

    猜你喜欢
    • 2021-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多