diff options
author | TymanWasTaken <32660892+tymanwastaken@users.noreply.github.com> | 2021-04-27 21:06:22 -0600 |
---|---|---|
committer | TymanWasTaken <32660892+tymanwastaken@users.noreply.github.com> | 2021-04-27 21:06:22 -0600 |
commit | 763fb7d98c3accbb21adf035a7cf0a83cb9533c9 (patch) | |
tree | 9d333fbca2a2a8e19d79904a4e29226174925cfc /src/lib/utils | |
download | tanzanite-763fb7d98c3accbb21adf035a7cf0a83cb9533c9.tar.gz tanzanite-763fb7d98c3accbb21adf035a7cf0a83cb9533c9.tar.bz2 tanzanite-763fb7d98c3accbb21adf035a7cf0a83cb9533c9.zip |
legit just copy utilibot v2 code
Diffstat (limited to 'src/lib/utils')
-rw-r--r-- | src/lib/utils/TopGG.ts | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/src/lib/utils/TopGG.ts b/src/lib/utils/TopGG.ts new file mode 100644 index 0000000..9c06816 --- /dev/null +++ b/src/lib/utils/TopGG.ts @@ -0,0 +1,110 @@ +import { Api } from '@top-gg/sdk'; +import { BotStats, WebhookPayload } from '@top-gg/sdk/dist/typings'; +import { BotClient } from '../extensions/BotClient'; +import { topGGPort, credentials, channels } from '../../config/options'; +import express, { Express } from 'express'; +import { TextChannel, MessageEmbed, WebhookClient } from 'discord.js'; +import { stripIndent } from 'common-tags'; +import { + json as bodyParserJSON, + urlencoded as bodyParserUrlEncoded +} from 'body-parser'; + +export class TopGGHandler { + public api = new Api(credentials.dblToken); + public client: BotClient; + public server: Express = express(); + public constructor(client: BotClient) { + this.client = client; + } + public init(): void { + setInterval(this.postGuilds.bind(this), 60000); + this.server.use(bodyParserJSON()); + this.server.use(bodyParserUrlEncoded({ extended: true })); + this.server.post('/dblwebhook', async (req, res) => { + if (req.headers.authorization !== credentials.dblWebhookAuth) { + res.status(403).send('Unauthorized'); + await this.client.util.warn( + `Unauthorized DBL webhook request 👀 ${await this.client.util.haste( + JSON.stringify( + { + 'Correct Auth': credentials.dblWebhookAuth, + 'Given Auth': req.headers.authorization, + 'Headers': req.headers, + 'Body': req.body + }, + null, + '\t' + ) + )}` + ); + return; + } else { + res.status(200).send('OK'); + } + const data = req.body as WebhookPayload; + await this.postVoteWebhook(data); + }); + this.server.listen(topGGPort, () => { + console.log(`Started express top.gg server at port ${topGGPort}`); + }); + } + public async postGuilds(): Promise<BotStats> { + if (this.client.config.dev) return; + return await this.api.postStats({ + serverCount: this.client.guilds.cache.size, + shardCount: this.client.shard ? this.client.shard.count : 1 + }); + } + public async postVoteWebhook(data: WebhookPayload): Promise<void> { + try { + if (data.type === 'test') { + await this.client.util.info( + `Test vote webhook data recieved, ${await this.client.util.haste( + JSON.stringify(data, null, '\t') + )}` + ); + return; + } else { + const parsedData = { + user: await this.client.users.fetch(data.user), + type: data.type as 'upvote' | 'test', + isWeekend: data.isWeekend + }; + const channel = (await this.client.channels.fetch( + channels.dblVote + )) as TextChannel; + const webhooks = await channel.fetchWebhooks(); + const webhook = + webhooks.size < 1 + ? await channel.createWebhook('Utilibot Voting') + : webhooks.first(); + const webhookClient = new WebhookClient(webhook.id, webhook.token, { + allowedMentions: { parse: [] } + }); + await webhookClient.send(undefined, { + username: 'Utilibot Voting', + avatarURL: this.client.user.avatarURL({ dynamic: true }), + embeds: [ + new MessageEmbed() + .setTitle('Top.GG Vote') + // prettier-ignore + .setDescription( + stripIndent` + User: ${parsedData.user.tag} + Weekend (worth double): ${parsedData.isWeekend ? 'Yes' : 'No'} + ` + ) + .setAuthor( + parsedData.user.tag, + parsedData.user.avatarURL({ dynamic: true }) + ) + .setTimestamp() + ] + }); + } + } catch (e) { + console.error(e); + } + } +} |