Spaces:
Runtime error
Runtime error
| 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() |