File size: 2,598 Bytes
4a915d4
 
 
 
 
755da99
4a915d4
 
 
 
 
 
 
 
 
 
 
 
 
5d03556
4a915d4
 
 
 
 
 
 
 
 
 
 
f5cfcf0
4a915d4
 
 
1835f3c
4a915d4
 
 
 
 
 
 
be94477
4a915d4
 
 
 
 
 
 
12a41ce
 
 
 
 
 
4a915d4
 
 
 
5d03556
4a915d4
 
 
 
 
 
 
 
12a41ce
4a915d4
 
 
12a41ce
4a915d4
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const express = require('express')
const { WebhookClient, EmbedBuilder } = require('discord.js')
const axios = require('axios')

const app = express()
const port = process.env.PORT || 7860

app.get('/', (req, res) => {
  res.send('Hello World')
})

app.listen(port, () => {
  console.log(`Server running on port ${port}`)
})

const webhook = new WebhookClient({
  url: 'https://discord.com/api/webhooks/1389959240340213910/eaxo0emjjL4XW6A1M8jAnyftIodvpUVYqmboYnSvqkq8BGaR6IGFFwnfcrhXTyWXiw0z'
})

const endVotenoti = true

let messageRef = null
let messageSent = false
let previousVotes = {}

async function fetchVotes() {
  const res = await axios.get('https://www.rickgdbot.xyz/contest/gauntlets/statsvoting.php?voteStats&apikey=ccprojects')
  return res.data.sort((a, b) => b.votes - a.votes)
}

function formatPlace(index) {
  const emoji = ['πŸ₯‡ 1st', 'πŸ₯ˆ 2nd', 'πŸ₯‰ 3rd', 'πŸ… 4th']
  return index < 4 ? emoji[index] : `${index + 1}th`
}

function createEmbed(data, ended) {
  const fields = data.map((item, index) => ({
    name: formatPlace(index),
    value: `**${item.levelname}**\nCreator: ${item.Creator}\nVotes: ${item.votes}`,
    inline: true
  }))
  const time = new Date().toLocaleString('en-US', { timeZone: 'Asia/Manila' })
  return new EmbedBuilder()
    .setTitle(ended ? 'πŸ“Š Final Vote Result for **Lava Gauntlet**' : 'πŸ“Š Live Vote Status for **Power Gauntlet**\nVote here: https://www.rickgdbot.xyz/contest/gauntlets/vote.php')
    .addFields(fields)
    .setColor(ended ? 0xff5555 : 0x00ffff)
    .setFooter({ text: `Updated the vote at ${time} | Today` })
}

async function updateVotes() {
  const data = await fetchVotes()
  const currentVotes = data.map(item => ({
    ...item,
    previous: previousVotes[item.levelname] || 0,
    diff: item.votes - (previousVotes[item.levelname] || 0)
  }))
  const embed = createEmbed(currentVotes, endVotenoti)
  data.forEach(item => previousVotes[item.levelname] = item.votes)

  if (endVotenoti) {
    if (!messageSent) {
      await webhook.send({ content: 'Vote has been ended view the result here https://www.rickgdbot.xyz/contest/gauntlets/results.php', embeds: [embed] })
      messageSent = true
    }
  } else {
    if (!messageRef) {
      const sent = await webhook.send({ content: 'The vote status', embeds: [embed] })
      messageRef = sent
      setInterval(updateVotes, 120000)
    } else {
      await webhook.editMessage(messageRef.id, { content: 'The vote status', embeds: [embed] })
    }
  }

  console.log(`[${new Date().toLocaleTimeString('en-US', { hour12: false })}] Vote updated.`)
}

updateVotes()