Skip to content

Commit

Permalink
banshare (#11)
Browse files Browse the repository at this point in the history
Co-authored-by: Szedann <[email protected]>
  • Loading branch information
IThundxr and Szedann authored Dec 27, 2023
1 parent 8da2a5b commit 1d29ece
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 4 deletions.
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# These are needed no matter what
DISCORD_TOKEN=
SERVER_ID=
SAY_LOGS_CHANNEL=
LOGS_CHANNEL=

Expand All @@ -11,4 +12,5 @@ LOADING_EMOJI=
SUCCESS_EMOJI=
FAIL_EMOJI=
WEBSERVER_PORT=
DATABASE_URL=
DATABASE_URL=
SIMULATED_BAN_SHARE_KEY=
2 changes: 2 additions & 0 deletions src/types/environment.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ declare global {
namespace NodeJS {
interface ProcessEnv {
DISCORD_TOKEN: string;
SERVER_ID: string;
SAY_LOGS_CHANNEL: string;
LOGS_CHANNEL: string;
MAVEN_REPO: string;
Expand All @@ -14,6 +15,7 @@ declare global {
FAIL_EMOJI: string;
WEBSERVER_PORT: string;
DATABASE_URL: string;
SIMULATED_BAN_SHARE_KEY: string;
NODE_ENV: 'development' | 'dev-prod' | 'production';
}
}
Expand Down
9 changes: 7 additions & 2 deletions src/webserver.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import express from 'express';
import { handleWebhook } from './webserver/github';
import { handleGithubWebhook } from './webserver/github';
import { client } from './index';
import { handleBanShare } from './webserver/banshare';

const app = express();

Expand All @@ -11,7 +12,11 @@ app.get('/', function (req, res) {
});

app.post('/github-webhook', function (req, res) {
handleWebhook(client, req, res);
handleGithubWebhook(client, req, res);
});

app.post('/banshare', function (req, res) {
handleBanShare(client, req, res);
});

const port = process.env.WEBSERVER_PORT || 3000;
Expand Down
82 changes: 82 additions & 0 deletions src/webserver/banshare.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { Request, Response } from 'express';
import { ButtonStyle, Client, Colors, EmbedBuilder } from 'discord.js';
import { Button } from '../handlers/button.handler';
import { string, object } from 'valibot';
import { ActionRowBuilder, ButtonBuilder } from '@discordjs/builders';

const banButton = new Button(
'Ban',
object({ userId: string() }),
(interaction, data) => {
const reason =
'aero banshare: ' +
(interaction.message.embeds[0].fields[3].value ??
'no reason provided');
interaction.guild?.bans.create(data.userId, { reason: reason });
interaction.reply(`<@${data.userId}> (\`${data.userId}\`) was banned.`);
}
);

export const handleBanShare = (client: Client, req: Request, res: Response) => {
if (!verify_signature(req)) {
res.status(401).send('Unauthorized');
return;
}

if (
req.body === undefined ||
req.body.server === undefined ||
req.body.userid === undefined ||
req.body.reason === undefined
) {
return res.status(400).send('Invalid request body');
}

handleBan(client, req);

res.status(200).send('Webhook received successfully');
};

const handleBan = async (client: Client, req: Request) => {
const channel = client.channels.cache.get(process.env.LOGS_CHANNEL);
const server = req.body.server;
const userId = req.body.userid;
const reason = req.body.reason;

const user = await client.users.fetch(userId);

const guild = client.guilds.cache.get(process.env.SERVER_ID);
const guildMember = await guild?.members.fetch(userId);
const present = guildMember !== undefined;

const embed = new EmbedBuilder()
.setTitle(`Incoming ban from ${server}`)
.addFields(
{ name: 'Username', value: user.username },
{ name: 'User ID', value: user.id },
{ name: 'Present in server', value: `${present}` },
{ name: 'Reason', value: reason },
{ name: 'Banned From', value: server }
)
.setColor(Colors.Red)
.setTimestamp();
channel?.isTextBased() &&
(await channel.send({
embeds: [embed],
components: [
new ActionRowBuilder<ButtonBuilder>().addComponents(
banButton.button(
{
label: 'Ban',
style: ButtonStyle.Danger,
},
{ userId }
)
),
],
}));
};

const verify_signature = (req: Request) => {
return process.env.SIMULATED_BAN_SHARE_KEY === req.headers['x-api-key'];
};
6 changes: 5 additions & 1 deletion src/webserver/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ const WEBHOOK_SECRET: string = process.env.GITHUB_SECRET;
const commitMap = new Map<string, Commit>();
const githubMap = new Map<number, string>();

export const handleWebhook = (client: Client, req: Request, res: Response) => {
export const handleGithubWebhook = (
client: Client,
req: Request,
res: Response
) => {
if (!verify_signature(req)) {
res.status(401).send('Unauthorized');
return;
Expand Down

0 comments on commit 1d29ece

Please sign in to comment.