Add code to make the stuff work
This commit is contained in:
parent
a18b73fe52
commit
3d9feabe88
15 changed files with 517 additions and 0 deletions
17
src/utils/components/buttons/count_votes.ts
Normal file
17
src/utils/components/buttons/count_votes.ts
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
import { db } from "@/main";
|
||||
|
||||
|
||||
export async function countVotes(data: any): Promise<object> {
|
||||
|
||||
let response = `Quote Votes:`;
|
||||
for (var i in db.bracket.quotes) {
|
||||
response += `\n${db.bracket.votes[i] ?? 0} votes for \`${db.bracket.quotes[i]}\``;
|
||||
};
|
||||
return {
|
||||
type: 4,
|
||||
data: {
|
||||
content: response,
|
||||
flags: 1 << 6,
|
||||
}
|
||||
};
|
||||
}
|
||||
30
src/utils/components/buttons/delete_vote.ts
Normal file
30
src/utils/components/buttons/delete_vote.ts
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
import { db } from "@/main";
|
||||
|
||||
|
||||
export async function deleteVote(data: any): Promise<object> {
|
||||
let userID = data.member.user.id;
|
||||
|
||||
if (!db.bracket.users[userID]) {
|
||||
return {
|
||||
type: 4,
|
||||
data: {
|
||||
content: `You haven't voted in the bracket, so you can't delete your vote.`,
|
||||
flags: 1 << 6,
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
// Subtract user's vote from total
|
||||
let vote = db.bracket.users[userID];
|
||||
--db.bracket.votes[vote];
|
||||
|
||||
delete db.bracket.users[userID];
|
||||
|
||||
return {
|
||||
type: 4,
|
||||
data: {
|
||||
content: `Your vote has been deleted.`,
|
||||
flags: 1 << 6,
|
||||
}
|
||||
};
|
||||
}
|
||||
19
src/utils/components/buttons/my_vote.ts
Normal file
19
src/utils/components/buttons/my_vote.ts
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
import { db } from "@/main";
|
||||
|
||||
export async function showUserVote(data: any): Promise<object> {
|
||||
let vote = db.bracket.users[data.member.user.id];
|
||||
let quote = db.bracket.quotes[vote];
|
||||
|
||||
let response = `You currently haven't voted for a quote!`;
|
||||
if (quote) {
|
||||
response = `Your vote is for:\n> ${quote}`;
|
||||
};
|
||||
|
||||
return {
|
||||
type: 4,
|
||||
data: {
|
||||
content: response,
|
||||
flags: 1 << 6,
|
||||
}
|
||||
};
|
||||
}
|
||||
12
src/utils/components/buttons/view_db.ts
Normal file
12
src/utils/components/buttons/view_db.ts
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
import { db } from "@/main";
|
||||
|
||||
|
||||
export async function viewDB(data: any): Promise<object> {
|
||||
return {
|
||||
type: 4,
|
||||
data: {
|
||||
content: `\`\`\`json\n${JSON.stringify(db.bracket, null, ' ')}\`\`\``,
|
||||
flags: 1 << 6,
|
||||
}
|
||||
};
|
||||
}
|
||||
40
src/utils/components/dropdowns/select_quote.ts
Normal file
40
src/utils/components/dropdowns/select_quote.ts
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
import { db } from "@/main";
|
||||
|
||||
export async function selectQuote(data: any): Promise<object> {
|
||||
let vote = parseInt(data.data.values[0]);
|
||||
let userID = data.member.user.id;
|
||||
let oldVote = db.bracket.users[userID];
|
||||
|
||||
// Set quote to 0 if it hasn't been voted for yet
|
||||
if (!db.bracket.votes[vote]) {
|
||||
db.bracket.votes[vote] = 0;
|
||||
};
|
||||
|
||||
++db.bracket.votes[vote];
|
||||
db.bracket.users[userID] = vote;
|
||||
|
||||
// User changed their vote
|
||||
if (oldVote != null) {
|
||||
|
||||
--db.bracket.votes[oldVote];
|
||||
|
||||
return {
|
||||
type: 4,
|
||||
data: {
|
||||
content: `Your vote has been changed from:\n> ${db.bracket.quotes[oldVote]}\nto:\n> ${db.bracket.quotes[vote]}`,
|
||||
flags: 1 << 6,
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// User voted for the first time
|
||||
else {
|
||||
return {
|
||||
type: 4,
|
||||
data: {
|
||||
content: `Your vote has been recorded for:\n> ${db.bracket.quotes[vote]}`,
|
||||
flags: 1 << 6,
|
||||
}
|
||||
};
|
||||
};
|
||||
};
|
||||
28
src/utils/quotes.ts
Normal file
28
src/utils/quotes.ts
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
import { readFileSync, writeFileSync } from "fs";
|
||||
import { config } from "../main";
|
||||
import axios from "axios";
|
||||
|
||||
export async function getQuote(count = 1) {
|
||||
let r = await axios.get(
|
||||
config.quote.api_base,
|
||||
{ params: { token: config.quote.token } }
|
||||
);
|
||||
let quoteList = r.data.split(`\n`);
|
||||
let history: string[] = JSON.parse(readFileSync(config.server.quote_history, `utf-8`));
|
||||
|
||||
// Populate the quotes list
|
||||
let quotes: string[] = [];
|
||||
do {
|
||||
let quote = quoteList[Math.floor(Math.random() * quoteList.length)];
|
||||
|
||||
if (!quotes.includes(quote) && !history.includes(quote)) {
|
||||
quotes.push(quote);
|
||||
};
|
||||
} while (quotes.length < count);
|
||||
|
||||
history.push(...quotes)
|
||||
|
||||
writeFileSync(config.server.quote_history, JSON.stringify(history));
|
||||
|
||||
return quotes;
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue