Skip to content

Commit

Permalink
Merge pull request #14 from Layers-of-Railways/main
Browse files Browse the repository at this point in the history
update from main
  • Loading branch information
Szedann authored Dec 28, 2023
2 parents 3817491 + 222b026 commit f98949e
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 25 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ SERVER_ID=
SAY_LOGS_CHANNEL=
LOGS_CHANNEL=
SIMULATED_BAN_SHARE_LOGS_CHANNEL=
BAN_LOGS_CHANNEL=

# These arent needed outside of production
MAVEN_REPO=
Expand Down
26 changes: 11 additions & 15 deletions src/handlers/button.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,30 @@ import {
ButtonBuilder,
ButtonInteraction,
Events,
Interaction,
InteractionButtonComponentData,
} from 'discord.js';
import { Handler } from '..';
import { BaseSchema, Output } from 'valibot';
import { error } from 'console';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const buttonMap = new Map<string, Button<any>>();

export class Button<ArgsType extends BaseSchema> {
export class Button<ArgsType> {
id: string;
args?: ArgsType;
_onPress?: (interaction: ButtonInteraction, args: ArgsType) => unknown;
constructor(
id: string,
args: ArgsType,
onPress: (
interaction: ButtonInteraction,
args: Output<ArgsType>
) => unknown
onPress: (interaction: ButtonInteraction, args: ArgsType) => unknown
) {
this.id = id;
if (buttonMap.has(id)) console.error(`Button ${id} is already defined`);
buttonMap.set(id, this);
this._onPress = onPress;
this.args = args;
}

button(
data: Partial<InteractionButtonComponentData>,
args: Output<ArgsType>
args: ArgsType
): ButtonBuilder {
const button = new ButtonBuilder({
...data,
Expand All @@ -43,17 +36,20 @@ export class Button<ArgsType extends BaseSchema> {
}

export const buttonHandler: Handler = (client) => {
client.on(Events.InteractionCreate, async (interaction) => {
client.on(Events.InteractionCreate, async (interaction: Interaction) => {
if (!interaction.isButton()) return;
const data = JSON.parse(interaction.customId);
const args = data.args;
if (!data.id) return;
if (!buttonMap.has(data.id)) return;
const button = buttonMap.get(data.id);
if (!button) return;
if (!button.args) throw error('No args set in button');
const parsedArgs = button.args.parse(args);

if (!button._onPress) return;
button._onPress(interaction, parsedArgs);
try {
button._onPress(interaction, args);
} catch {
interaction.reply('error while executing');
}
});
};
2 changes: 1 addition & 1 deletion src/logIssueAnalyzers/optifine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const optifineAnalyzer: Analyzer = async (log) => {
if (matchesOptifine?.[1] == 'true') {
return {
name: 'Optifine Warning Disabled',
value: 'You appeared to have disabled the Optifine warning. Many issues you might encounter are caused by Optifine. You will most likely get any support due to this.',
value: 'You appeared to have disabled the Optifine warning. Many issues you might encounter are caused by Optifine. You will not get any support due to this.',
};
} else {
return {
Expand Down
1 change: 1 addition & 0 deletions src/types/environment.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ declare global {
SERVER_ID: string;
SAY_LOGS_CHANNEL: string;
LOGS_CHANNEL: string;
BAN_LOGS_CHANNEL: string;
MAVEN_REPO: string;
GITHUB_STATUS_CHANNEL: string;
GITHUB_SECRET: string;
Expand Down
55 changes: 46 additions & 9 deletions src/webserver/banshare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ import {
TextInputBuilder,
} from 'discord.js';
import { Button } from '../handlers/button.handler';
import { string, object } from 'valibot';

const banButton = new Button(
'ban',
object({ userId: string() }),
async (interaction, data) => {
async (interaction, data: { userId: string }) => {
const reason =
'aero banshare: ' +
'simulated banshare: ' +
(interaction.message.embeds[0].fields[3].value ??
'no reason provided');
const modal = new ModalBuilder()
Expand All @@ -32,20 +30,58 @@ const banButton = new Button(
.setValue(reason)
)
);
interaction.showModal(modal);
await interaction.showModal(modal);
interaction
.awaitModalSubmit({
filter: (interaction) =>
interaction.customId == modal.data.custom_id,
time: 300_000,
})
.then((modalResponse) => {
.then(async (modalResponse) => {
interaction.guild?.bans.create(data.userId, {
reason: modalResponse.components[0].components[0].value,
});
interaction.reply(
`<@${data.userId}> (\`${data.userId}\`) was banned.`
);
await interaction.reply({
content: `<@${data.userId}> (\`${data.userId}\`) was banned.`,
ephemeral: true,
});
await interaction.update({
components: [
new ActionRowBuilder<ButtonBuilder>().addComponents(
new ButtonBuilder()
.setLabel('ban')
.setStyle(ButtonStyle.Danger)
.setDisabled(true)
),
],
});
if (interaction.guild != null) {
const channel = await interaction.guild.channels.fetch(
process.env.BAN_LOGS_CHANNEL
);
if (channel?.isTextBased()) {
channel.send({
embeds: [
new EmbedBuilder()
.setTitle('User Banned via Banshare')
.setDescription(
`<@!${data.userId}> was banned!`
)
.setFields([
{
name: 'Reason',
value: modalResponse.components[0]
.components[0].value,
},
{
name: 'By',
value: interaction.user.username,
},
]),
],
});
}
}
});
}
);
Expand Down Expand Up @@ -87,6 +123,7 @@ const handleBan = async (client: Client, req: Request) => {
const embed = new EmbedBuilder()
.setTitle(`Incoming ban from ${server}`)
.addFields(
{ name: 'User', value: `<@!${user.id}>` },
{ name: 'Username', value: user.username },
{ name: 'User ID', value: user.id },
{ name: 'Present in server', value: `${present}` },
Expand Down

0 comments on commit f98949e

Please sign in to comment.