ccprojects commited on
Commit
4a915d4
Β·
verified Β·
1 Parent(s): fbde297

Create index.js

Browse files
Files changed (1) hide show
  1. index.js +96 -0
index.js ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const express = require('express')
2
+ const { WebhookClient, EmbedBuilder } = require('discord.js')
3
+ const axios = require('axios')
4
+ const { GoogleGenerativeAI } = require('@google/generative-ai')
5
+
6
+ const app = express()
7
+ const port = process.env.PORT || 3000
8
+
9
+ app.get('/', (req, res) => {
10
+ res.send('Hello World')
11
+ })
12
+
13
+ app.listen(port, () => {
14
+ console.log(`Server running on port ${port}`)
15
+ })
16
+
17
+ const webhook = new WebhookClient({
18
+ url: 'https://discord.com/api/webhooks/1389959240340213910/eaxo0emjjL4XW6A1M8jAnyftIodvpUVYqmboYnSvqkq8BGaR6IGFFwnfcrhXTyWXiw0z'
19
+ })
20
+
21
+ const genAI = new GoogleGenerativeAI("AIzaSyACBopnf2eRynlRpaxHKBZ9x6XtD2U9tBo")
22
+ const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" })
23
+
24
+ const endVotenoti = false
25
+
26
+ let messageRef = null
27
+ let messageSent = false
28
+ let previousVotes = {}
29
+
30
+ async function fetchVotes() {
31
+ const res = await axios.get('https://www.rickgdbot.xyz/contest/gauntlets/statsvoting.php?voteStats&apikey=ccprojects')
32
+ return res.data.sort((a, b) => b.votes - a.votes)
33
+ }
34
+
35
+ function formatPlace(index) {
36
+ const emoji = ['πŸ₯‡ 1st', 'πŸ₯ˆ 2nd', 'πŸ₯‰ 3rd', 'πŸ… 4th']
37
+ return index < 4 ? emoji[index] : `${index + 1}th`
38
+ }
39
+
40
+ function createEmbed(data, analysis, ended) {
41
+ const fields = data.map((item, index) => ({
42
+ name: formatPlace(index),
43
+ value: `**${item.levelname}**\nCreator: ${item.Creator}\nVotes: ${item.votes}`,
44
+ inline: true
45
+ }))
46
+ const time = new Date().toLocaleString('en-US', { timeZone: 'Asia/Manila' })
47
+ return new EmbedBuilder()
48
+ .setTitle(ended ? 'πŸ“Š Final Vote Result' : 'πŸ“Š Live Vote Status for **Power Gauntlet**')
49
+ .addFields(fields)
50
+ .addFields({ name: 'πŸ“ AI Vote Report', value: analysis })
51
+ .setColor(ended ? 0xff5555 : 0x00ffff)
52
+ .setFooter({ text: `Updated the vote at ${time} | Today` })
53
+ }
54
+
55
+ async function analyzeVotes(currentVotes) {
56
+ const changes = currentVotes.map(item => {
57
+ const prev = previousVotes[item.levelname] || 0
58
+ const diff = item.votes - prev
59
+ return `${item.levelname} by ${item.Creator}: ${item.votes} votes (${diff >= 0 ? '+' : ''}${diff})`
60
+ }).join('\n')
61
+ const prompt = `You are a voting analyst. Here's the vote update:\n\n${changes}\n\nSummarize what happened in this update.`
62
+ const result = await model.generateContent({ contents: [{ role: 'user', parts: [{ text: prompt }] }] })
63
+ const response = await result.response
64
+ return response.text().trim()
65
+ }
66
+
67
+ async function updateVotes() {
68
+ const data = await fetchVotes()
69
+ const currentVotes = data.map(item => ({
70
+ ...item,
71
+ previous: previousVotes[item.levelname] || 0,
72
+ diff: item.votes - (previousVotes[item.levelname] || 0)
73
+ }))
74
+ const analysis = await analyzeVotes(currentVotes)
75
+ const embed = createEmbed(currentVotes, analysis, endVotenoti)
76
+ data.forEach(item => previousVotes[item.levelname] = item.votes)
77
+
78
+ if (endVotenoti) {
79
+ if (!messageSent) {
80
+ await webhook.send({ content: 'Vote has been ended', embeds: [embed] })
81
+ messageSent = true
82
+ }
83
+ } else {
84
+ if (!messageRef) {
85
+ const sent = await webhook.send({ content: 'The vote status', embeds: [embed] })
86
+ messageRef = sent
87
+ setInterval(updateVotes, 120000)
88
+ } else {
89
+ await webhook.editMessage(messageRef.id, { content: 'The vote status', embeds: [embed] })
90
+ }
91
+ }
92
+
93
+ console.log(`[${new Date().toLocaleTimeString('en-US', { hour12: false })}] Updated votes and AI analysis sent.`)
94
+ }
95
+
96
+ updateVotes()