D
D
DefaultCR2021-02-16 18:33:43
Node.js
DefaultCR, 2021-02-16 18:33:43

How to translate code from python to node js?

I have a python code and I would like to translate it to node js, but my knowledge of python is very weak, the code itself is:

import time

@bot.event
async def on_ready():
    print('Bot Connected')
    global tdict
    tdict = {}
    await bot.change_presence(activity = discord.Game('r!help'))

@bot.event
async def on_voice_state_update(member, before, after):
    author = member.id
    if before.channel is None and after.channel is not None:
        print('1')
        t1 = time.time()
        tdict[author] = t1
    elif before.channel is not None and after.channel is None and author in tdict:
        t2 = time.time() 
        print('0')
        print(t2-tdict[author])

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
ettychel, 2021-02-16
@DefaultCR


Below is your python code in JS using the discord-js library

The code

const Discord = require("discord.js");
const bot = new Discord.Client();

bot.on("ready", async () => {
  console.log("Bot Connected");
  global.tdict = {};
  await bot.user.setActivity("r!help", { type: "PLAYING" });
});

bot.on("voiceStateUpdate", async (before, after) => {
  const author = before.member.id || after.member.id;
  if (!before.channel && after.channel) {
    console.log("1");
    const t1 = Math.floor(Date.now() / 1000);
    global.tdict[author] = t1;
  } else if (before.channel && !after.channel && global.tdict.hasOwnProperty(author)) {
    const t2 = Math.floor(Date.now() / 1000);
    console.log("0");
    console.log(t2 - global.tdict[author]);
  }
});

A
alex4answ, 2021-02-16
@alex4answ

Google what each operation does, transfer it to node.
There are only 2-3 functions that you need to google, what's up, are you serious?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question