Skip to content

Commit

Permalink
Archive submissions
Browse files Browse the repository at this point in the history
  • Loading branch information
Seefaaa committed Oct 29, 2024
1 parent aba1609 commit f14bc57
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 46 deletions.
27 changes: 12 additions & 15 deletions src/events/interactionCreate/button/approveSubmission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const customId = 'submissionApproveButton';
export class ApproveSubmissionButton implements PermanentButtonInteraction {
public customId = customId;
public async execute(interaction: ButtonInteraction) {
if (!interaction.inGuild() || !interaction.channel) return;
if (!interaction.inGuild() || !interaction.channel?.isThread()) return;

const permissions = interaction.member.permissions as PermissionsBitField;

Expand Down Expand Up @@ -50,33 +50,30 @@ export class ApproveSubmissionButton implements PermanentButtonInteraction {
return;
}

await interaction.reply('Başvuru onaylandı.');

try {
await submitter.roles.add(configuration.submission.role);
} catch {
await interaction.reply({
content: 'Başvuru sahibine rol verilemedi.',
ephemeral: true,
});
await interaction.editReply('Başvuru sahibine rol verilemedi.');
return;
}

try {
await interaction.channel.setLocked(true);
await interaction.channel.setArchived(true);
} catch {
await interaction.followUp('Alt başlık arşivlenemedi.');
}

logger.info(
`Approved submission of [${submitter.user.tag}](${submitter.user.id}) by [${interaction.user.tag}](${interaction.user.id})`
);

logger.channel(
'submission',
interaction.client,
`${submitter.user} hesabının başvurusu ${interaction.user} tarafından onaylandı.`
`${submitter.user} hesabının başvurusu ${interaction.user} tarafından onaylandı: ${interaction.channel}`
);

try {
await interaction.channel.delete();
} catch {
await interaction.reply({
content: 'Başvuru onaylandı fakat alt başlık silinemedi.',
ephemeral: true,
});
}
}
}
15 changes: 5 additions & 10 deletions src/events/interactionCreate/button/createSubmission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
type ButtonInteraction,
type ModalActionRowComponentBuilder as ModalActionRow,
ModalBuilder,
type TextChannel,
TextChannel,
TextInputBuilder,
TextInputStyle,
} from 'discord.js';
Expand All @@ -17,13 +17,12 @@ export const customId = 'submissionCreateButton';
export class CreateSubmissionButton implements PermanentButtonInteraction {
public customId = customId;
public async execute(interaction: ButtonInteraction) {
if (!interaction.channel || !interaction.channel.isTextBased()) return;
if (!(interaction.channel instanceof TextChannel)) return;

const channel = interaction.channel as TextChannel;
const channelThreads = await channel.threads.fetch();
const threadManager = await interaction.channel.threads.fetch();

const thread = channelThreads.threads.find((thread) =>
thread.name.endsWith(interaction.user.id)
const thread = threadManager.threads.find(
(thread) => thread.name.endsWith(interaction.user.id) && !thread.locked
);

if (thread) {
Expand Down Expand Up @@ -52,10 +51,6 @@ export class CreateSubmissionButton implements PermanentButtonInteraction {
return;
}

// note: there is a limit of max threads per guild (1000), it'ld better to
// check if it's reached but it's quite impossible to reach that limit
// so i'll ignore it for now.

const modal = new ModalBuilder()
.setCustomId(modalId)
.setTitle('Başvuru Formu');
Expand Down
24 changes: 11 additions & 13 deletions src/events/interactionCreate/button/denySubmission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const customId = 'submissionDenyButton';
export class DenySubmissionButton implements PermanentButtonInteraction {
public customId = customId;
public async execute(interaction: ButtonInteraction) {
if (!interaction.inGuild() || !interaction.channel) return;
if (!interaction.inGuild() || !interaction.channel?.isThread()) return;

const permissions = interaction.member.permissions as PermissionsBitField;

Expand All @@ -26,12 +26,19 @@ export class DenySubmissionButton implements PermanentButtonInteraction {
messageContent.indexOf('>')
);

let submitter: User | null;
let submitter: User | null = null;

try {
submitter = await interaction.client.users.fetch(submitterId);
} catch {}

await interaction.reply('Başvuru reddedildi.');

try {
await interaction.channel.setLocked(true);
await interaction.channel.setArchived(true);
} catch {
submitter = null;
await interaction.followUp('Alt başlık arşivlenemedi.');
}

logger.info(
Expand All @@ -41,16 +48,7 @@ export class DenySubmissionButton implements PermanentButtonInteraction {
logger.channel(
'submission',
interaction.client,
`<@${submitterId}> hesabının başvurusu ${interaction.user} tarafından reddedildi.`
`<@${submitterId}> hesabının başvurusu ${interaction.user} tarafından reddedildi: ${interaction.channel}`
);

try {
await interaction.channel.delete();
} catch {
await interaction.reply({
content: 'Başvuru reddedildi fakat alt başlık silinemedi.',
ephemeral: true,
});
}
}
}
8 changes: 3 additions & 5 deletions src/events/interactionCreate/modal/createSubmission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
ChannelType,
type MessageActionRowComponentBuilder as MessageActionRow,
type ModalSubmitInteraction,
type TextChannel,
TextChannel,
ThreadAutoArchiveDuration,
} from 'discord.js';

Expand All @@ -20,11 +20,9 @@ export const customId = 'createSubmissionModal';
export class CreateSubmissionModal implements ModalInteraction {
public customId = customId;
public async execute(interaction: ModalSubmitInteraction) {
if (!interaction.channel || !interaction.channel.isTextBased()) return;
if (!(interaction.channel instanceof TextChannel)) return;

const channel = interaction.channel as TextChannel;

const thread = await channel.threads.create({
const thread = await interaction.channel.threads.create({
name: `${interaction.user.username} #${interaction.user.id}`,
autoArchiveDuration: ThreadAutoArchiveDuration.OneWeek,
type: ChannelType.PrivateThread,
Expand Down
13 changes: 10 additions & 3 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Client } from 'discord.js';
import type { Client, MessageCreateOptions } from 'discord.js';
import { once } from 'events';
import pino from 'pino';
import pretty from 'pino-pretty';
Expand All @@ -8,7 +8,12 @@ import { name } from '@/package';

type Channel = 'verify' | 'submission';
type ExtendedLogger = pino.Logger & {
channel: (channel: Channel, client: Client, message: string) => Promise<void>;
channel: (
channel: Channel,
client: Client,
message: string,
options?: MessageCreateOptions
) => Promise<void>;
};

const channels: Record<Channel, string> = {
Expand Down Expand Up @@ -36,7 +41,8 @@ async function createLogger() {
logger.channel = async (
channel: Channel,
client: Client,
message: string
message: string,
options?: MessageCreateOptions
) => {
try {
const textChannel = await client.channels.fetch(channels[channel]);
Expand All @@ -52,6 +58,7 @@ async function createLogger() {
await textChannel.send({
content: message,
allowedMentions: { parse: [] },
...options,
});
} catch (error) {
logger.error(
Expand Down
11 changes: 11 additions & 0 deletions src/utils/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,14 @@ export function timestamp(date: Date, format?: Format): string {
format ? ':' + format : ''
}>`;
}

export function ISO8601(date: Date): string {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
const hours = String(date.getHours()).padStart(2, '0');
const minutes = String(date.getMinutes()).padStart(2, '0');
const seconds = String(date.getSeconds()).padStart(2, '0');

return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

0 comments on commit f14bc57

Please sign in to comment.