Skip to content

Commit

Permalink
feat(voteLogger.ts): added wumpus.store as vote log (#378)
Browse files Browse the repository at this point in the history
* fix: some typing stuff and canvas cache clear

* feat(): Improved database structure and enhanced encryption performance

* feat(webhookHandler.ts): forgot .then()

* feat(dailyMessage.ts): forgot for guilds that use wy only for daily msg

* feat(voteLogger.ts): added wumpus.store as vote log
  • Loading branch information
Sean Sattler authored Dec 23, 2023
1 parent 81b0cf9 commit ee46916
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 71 deletions.
8 changes: 4 additions & 4 deletions src/util/dailyMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ export default class DailyMessage {
db.customMessages.filter((c) => c.type !== "nsfw")[
Math.floor(
Math.random() *
db.customMessages.filter((c) => c.type !== "nsfw").length,
db.customMessages.filter((c) => c.type !== "nsfw").length,
)
].msg,
].msg,
);
} else {
randomDaily = [...General, ...WhatYouDo];
Expand Down Expand Up @@ -119,9 +119,9 @@ export default class DailyMessage {
randomDaily = db.customMessages.filter((c) => c.type !== "nsfw")[
Math.floor(
Math.random() *
db.customMessages.filter((c) => c.type !== "nsfw").length,
db.customMessages.filter((c) => c.type !== "nsfw").length,
)
].msg;
].msg;
}

dailyId = Math.floor(Math.random() * randomDaily.length);
Expand Down
151 changes: 84 additions & 67 deletions src/util/voteLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ import express from "express";
import axios from "axios";
import { white, gray, green } from "chalk-advanced";
import WouldYou from "./wouldYou";

const app = express();
const webhook = new Topgg.Webhook(process.env.TOPGG_WEBHOOK);

export default class VoteLogger {
private c: WouldYou;
private api: Topgg.Api;
public votes: Map<string, Topgg.ShortUser>;

constructor(c: WouldYou) {
this.c = c;
if (!process.env.TOPGG_TOKEN) return;
Expand Down Expand Up @@ -51,7 +53,7 @@ export default class VoteLogger {
* Get all votes from top.gg
* @return {Promise<void>}
*/
async getVotes() {
async getVotes(): Promise<void> {
const votes = await this.api.getVotes();

this.votes = new Map();
Expand All @@ -65,79 +67,94 @@ export default class VoteLogger {
* Start the api for the vote tracker
* @return {void}
*/
startAPI() {
app.post(
"/dblwebhook",
webhook.listener(async (vote) => {
let userdata: any = null;
await axios({
method: "GET",
url: `https://japi.rest/discord/v1/user/${vote.user}`,
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
})
.then((res) => {
userdata = res?.data?.data;
})
.catch((err) => {
captureException(err);
userdata = this.c?.users?.cache?.get(vote.user) ?? null;
});

if (!userdata?.username) return;

let emojis = [
"<a:jammiesyou:1009965703484424282>",
"<a:nyancatyou:1009965705808056350>",
"<a:partyparrotyou:1009965704621080678>",
"<a:shootyou:1009965706978267136>",
"<a:catjamyou:1009965950101110806>",
"<a:patyou:1009964589678612581>",
"<a:patyoufast:1009964759216574586>",
];

const button =
new ActionRowBuilder<MessageActionRowComponentBuilder>().addComponents(
[
new ButtonBuilder()
.setLabel("Vote!")
.setStyle(5)
.setEmoji("💻")
.setURL("https://top.gg/bot/981649513427111957/vote"),
],
);
startAPI(): void {
app.post("/wumpuswebhook", async (req, res) => {
if (req.headers["authorization"] !== process.env.WUMPUS_WEBHOOK) return res.status(401).send({ error: "Unauthorized" });

const emojisRandom = emojis[Math.floor(Math.random() * emojis.length)];
const {
userId,
} = req.body;

const webhookClient = new WebhookClient({
url: process.env.LOG_VOTE as string,
});
await this.sendVoteMessage(userId, "wumpus.store");

console.log(
`${white("Would You?")} ${gray(">")} ${green(
`${userdata.tag} voted for me!`,
)}`,
);
return res.status(200).send({ success: true });
});

webhookClient
.send({
content: `${emojisRandom} Voted for me on ${hideLinkEmbed(
"https://top.gg/bot/981649513427111957/vote",
)}`,
components: [button],
username: `${userdata.tag
.replace("Discord", "")
.replace("discord", "")
.replace("Everyone", "")
.replace("everyone", "")}`,
avatarURL: userdata.avatarURL,
})
.catch((err) => captureException(err));
app.post("/dblwebhook", webhook.listener(async (vote) => {
await this.sendVoteMessage(vote.user, "top.gg");
}),
);

app.listen(5643);
}

async sendVoteMessage(userId: string, website: string): Promise<void> {
let userdata: any = null;

await axios({
method: "GET",
url: `https://japi.rest/discord/v1/user/${userId}`,
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
})
.then((res) => {
userdata = res?.data?.data;
})
.catch((err) => {
captureException(err);
userdata = this.c?.users?.cache?.get(userId) ?? null;
});

if (!userdata?.username) return;

let emojis = [
"<a:jammiesyou:1009965703484424282>",
"<a:nyancatyou:1009965705808056350>",
"<a:partyparrotyou:1009965704621080678>",
"<a:shootyou:1009965706978267136>",
"<a:catjamyou:1009965950101110806>",
"<a:patyou:1009964589678612581>",
"<a:patyoufast:1009964759216574586>",
];

const button =
new ActionRowBuilder<MessageActionRowComponentBuilder>().addComponents(
[
new ButtonBuilder()
.setLabel("Vote!")
.setStyle(5)
.setEmoji("💻")
.setURL(`https://${website}/bot/981649513427111957/vote`), // Change this to an object of websites if you would like to support more than wumpus and top.gg lol
],
);

const emojisRandom = emojis[Math.floor(Math.random() * emojis.length)];

const webhookClient = new WebhookClient({
url: process.env.LOG_VOTE as string,
});

console.log(
`${white("Would You?")} ${gray(">")} ${green(
`${userdata.tag} voted for me!`,
)}`,
);

webhookClient
.send({
content: `${emojisRandom} Voted for me on ${hideLinkEmbed(
`https://${website}/bot/981649513427111957/vote`,
)}`,
components: [button],
username: `${userdata.tag
.replace("Discord", "")
.replace("discord", "")
.replace("Everyone", "")
.replace("everyone", "")}`,
avatarURL: userdata.avatarURL,
})
.catch((err) => captureException(err));
}
}

0 comments on commit ee46916

Please sign in to comment.