Z
Z
zonf1k2020-02-15 14:27:17
JavaScript
zonf1k, 2020-02-15 14:27:17

Is it possible to improve and / or make something more correct (Bot VK)?

Can something be done better/better here? I would be grateful for your help (I'm new)

const { VK } = require('vk-io');
const vk = new VK({ token: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" });
const acc = require('./acc.json');
const fs = require('fs');

function unixDate(stamp) {
    let date = new Date(stamp),
        year = date.getFullYear(),
        month = (("0" + (date.getMonth() + 1)).slice(-2)),
        day = (("0" + (date.getDate())).slice(-2))
    return `${day}.${month}.${year}`;
};

function unixTime(stamp) {
    let date = new Date(stamp),
        hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(),
        mins = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(),
    return `${hour}:${mins}`;
};

function getDate() {
    return unixDate(Date.now());
};
function getTime() {
    return unixTime(Date.now());
};

vk.updates.use((ctx, next) => {
  if (Number(ctx.senderId) <= 0 || ctx.senderId === undefined) return;
  if (!acc.users.find(x => x.vk === ctx.senderId)) {
    acc.number++;
    acc.users.push({
      id: acc.number,
      vk: ctx.senderId,
      nickname: 'Неизвестно',
      level: 1,
      admin: 0,
      balance: 0,
      winGame: 0,
      lossGame: 0,
      dateReg: getDate(),
      timeReg: getTime(),
    });
  };
  if (ctx.senderId < 0) return;
  return next();
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir, 2020-02-15
@zonf1k

1. Write the full names of the variables - account
2. VK-vk, there shouldn't be such shit code - vkToken
3. Otherwise, in my opinion, it's not bad, but at the very bottom there is not enough indentation between the lines.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question