Skip to content
This repository has been archived by the owner on Sep 1, 2023. It is now read-only.

Commit

Permalink
add case creation date (#399)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nyameliaaaa authored Mar 18, 2023
1 parent 72ec1a4 commit 135b528
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 30 deletions.
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"files.insertFinalNewline": true,
"typescript.tsdk": "node_modules\\typescript\\lib"
"typescript.tsdk": "node_modules\\typescript\\lib",
"[prisma]": {
"editor.defaultFormatter": "Prisma.prisma"
}
}
16 changes: 8 additions & 8 deletions i18n/en-US/commands.json
Original file line number Diff line number Diff line change
Expand Up @@ -375,31 +375,31 @@
"DESCRIPTION": "Timeout a user from the server",
"NAME": "timeout",
"OPTIONS": {
"USER": {
"DESCRIPTION": "Provide a user to timeout",
"NAME": "user"
},
"DURATION": {
"DESCRIPTION": "Provide how long the timeout will last",
"NAME": "duration"
},
"REASON": {
"DESCRIPTION": "Provide a reason for timing out this user",
"NAME": "reason"
},
"USER": {
"DESCRIPTION": "Provide a user to timeout",
"NAME": "user"
}
}
},
"WARN": {
"DESCRIPTION": "Warn a user in the server",
"NAME": "warn",
"OPTIONS": {
"USER": {
"DESCRIPTION": "Provide a user to warn",
"NAME": "user"
},
"REASON": {
"DESCRIPTION": "Provide a reason for warning this user",
"NAME": "reason"
},
"USER": {
"DESCRIPTION": "Provide a user to warn",
"NAME": "user"
}
}
},
Expand Down
5 changes: 3 additions & 2 deletions i18n/en-US/responses.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"BAN_SUCCESS_TITLE": "Banned!",
"BAN_USER": "Banned",
"BIRB": "Random birb",
"CASE_CREATED_AT": "Created at",
"CASE_ID": "Case ID",
"CASE_MODERATOR": "Moderator",
"CASE_NEW": "New case!",
Expand Down Expand Up @@ -98,10 +99,10 @@
"LISTCASE_NEXT": "Next",
"LISTCASE_NO_CASES": "{{- user}} doesn't have any cases!",
"LISTCASE_TITLE": "Case history for {{- user}}",
"MESSAGE_MENTION": "Hi! Type `/` to see my commands",
"MESSAGE_DELETE_AUTHOR": "Author",
"MESSAGE_DELETE_CONTENT": "Content",
"MESSAGE_DELETE_TITLE": "Message deleted",
"MESSAGE_MENTION": "Hi! Type `/` to see my commands",
"MESSAGE_UPDATE_AUTHOR": "Author",
"MESSAGE_UPDATE_NEW_CONTENT": "New content",
"MESSAGE_UPDATE_OLD_CONTENT": "Old content",
Expand Down Expand Up @@ -175,8 +176,8 @@
"STATS_COMMAND_COUNT": "Command count",
"STATS_SERVER_COUNT": "Server count",
"STATS_USER_COUNT": "User count",
"SUPPORT_TITLE": "Need some help?",
"SUPPORT_DESCRIPTION": "Join the support server [here]({{- link}})",
"SUPPORT_TITLE": "Need some help?",
"TIMEOUT": "Timeout",
"TIMEOUT_BOT_PERMISSIONS_MISSING": "I can't timeout this member, I don't have permissions needed to timeout!",
"TIMEOUT_DURATION_INVALID": "The duration you provided is invalid!",
Expand Down
16 changes: 8 additions & 8 deletions i18n/pl/commands.json
Original file line number Diff line number Diff line change
Expand Up @@ -375,31 +375,31 @@
"DESCRIPTION": "Wyślij użytkownika na przerwę (podobne do mute)",
"NAME": "timeout",
"OPTIONS": {
"USER": {
"DESCRIPTION": "Podaj użytkownika, którego chcesz wysłać na przerwę",
"NAME": "użytkownik"
},
"DURATION": {
"DESCRIPTION": "Podaj, jak długo ma trwać przerwa",
"NAME": "długość"
},
"REASON": {
"DESCRIPTION": "Podaj powód przerwy",
"NAME": "powód"
},
"USER": {
"DESCRIPTION": "Podaj użytkownika, którego chcesz wysłać na przerwę",
"NAME": "użytkownik"
}
}
},
"WARN": {
"DESCRIPTION": "Daj użytkownikowi ostrzeżenie",
"NAME": "ostrzeżenie",
"OPTIONS": {
"USER": {
"DESCRIPTION": "Podaj użytkownika, któremu chcesz wstawić ostrzeżenie",
"NAME": "użytkownik"
},
"REASON": {
"DESCRIPTION": "Podaj powód ostrzeżenia",
"NAME": "powód"
},
"USER": {
"DESCRIPTION": "Podaj użytkownika, któremu chcesz wstawić ostrzeżenie",
"NAME": "użytkownik"
}
}
},
Expand Down
5 changes: 3 additions & 2 deletions i18n/pl/responses.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"BAN_SUCCESS_TITLE": "Zbanowano!",
"BAN_USER": "Zbanowany",
"BIRB": "Randomowy ptak",
"CASE_CREATED_AT": "Utworzono",
"CASE_ID": "ID kary",
"CASE_MODERATOR": "Ukarany przez",
"CASE_NEW": "Nowa kara!",
Expand Down Expand Up @@ -98,10 +99,10 @@
"LISTCASE_NEXT": "Następna strona",
"LISTCASE_NO_CASES": "{{- user}} nie ma żadnych kar!",
"LISTCASE_TITLE": "Historia kar dla {{- user}}",
"MESSAGE_MENTION": "Cześć! Wpisz `/` aby zobaczyć moje komendy",
"MESSAGE_DELETE_AUTHOR": "Autor",
"MESSAGE_DELETE_CONTENT": "Treść",
"MESSAGE_DELETE_TITLE": "Usunięto wiadomość",
"MESSAGE_MENTION": "Cześć! Wpisz `/` aby zobaczyć moje komendy",
"MESSAGE_UPDATE_AUTHOR": "Autor",
"MESSAGE_UPDATE_NEW_CONTENT": "Nowa treść",
"MESSAGE_UPDATE_OLD_CONTENT": "Stara treść",
Expand Down Expand Up @@ -175,8 +176,8 @@
"STATS_COMMAND_COUNT": "Ilość komend",
"STATS_SERVER_COUNT": "Ilość serwerów",
"STATS_USER_COUNT": "Ilość użytkowników",
"SUPPORT_TITLE": "Potrzebujesz pomocy?",
"SUPPORT_DESCRIPTION": "Dołącz na serwer support [tutaj]({{- link}})",
"SUPPORT_TITLE": "Potrzebujesz pomocy?",
"TIMEOUT": "Przerwa",
"TIMEOUT_BOT_PERMISSIONS_MISSING": "Nie mogę wyciszyć tego członka, nie posiadam potrzebnych permisji!",
"TIMEOUT_DURATION_INVALID": "Podałeś nieprawidłową długość wyciszenia!",
Expand Down
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ model Case {
moderatedUser BigInt @map("moderated_user")
type String
reason String
createdAt BigInt @map("created_at")
@@id([caseId, guildId])
@@index([moderatedUser])
Expand Down
21 changes: 21 additions & 0 deletions src/api/guilds/[id]/cases/[caseId].ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,76 @@ import type { FastifyReply, FastifyRequest } from 'fastify';
export async function patchCase(client: FluorineClient, req: FastifyRequest, res: FastifyReply) {
const { id: guildId, case: caseId } = req.params as { id: string; case: string };
const { id: userId } = await client.oauth.verify(req.cookies.authorization);

const member = await client.guilds.cache.get(guildId)?.members.fetch(userId);

if (!member) {
return res.status(404).send({ error: 'Guild not found' });
}

if (!member.permissions.has(PermissionFlagsBits.ManageGuild)) {
return res.status(403).send({ error: 'Missing permissions' });
}

const { reason } = JSON.parse(req.body as string) as { reason: string };

if (!reason) {
return res.status(400).send({ error: 'Invalid body' });
}

const caseData = await client.cases.getOne(guildId, parseInt(caseId));

if (!caseData) {
return res.status(404).send({ error: 'Case not found' });
}

await client.cases.editReason(guildId, parseInt(caseId), reason);
res.status(204).send();
}

export async function getCase(client: FluorineClient, req: FastifyRequest, res: FastifyReply) {
const { id: guildId, case: caseId } = req.params as { id: string; case: string };
const { id: userId } = await client.oauth.verify(req.cookies.authorization);

const member = await client.guilds.cache.get(guildId)?.members.fetch(userId);

if (!member) {
return res.status(404).send({ error: 'Guild not found' });
}

if (!member.permissions.has(PermissionFlagsBits.ManageGuild)) {
return res.status(403).send({ error: 'Missing permissions' });
}

const caseData = await client.cases.getOne(guildId, parseInt(caseId));

if (!caseData) {
return res.status(404).send({ error: 'Case not found' });
}

res.header('Content-Type', 'application/json');
res.send(JSON.stringify(caseData, (key, value) => (typeof value === 'bigint' ? value.toString() : value)));
}

export async function deleteCase(client: FluorineClient, req: FastifyRequest, res: FastifyReply) {
const { id: guildId, case: caseId } = req.params as { id: string; case: string };
const { id: userId } = await client.oauth.verify(req.cookies.authorization);

const member = await client.guilds.cache.get(guildId)?.members.fetch(userId);

if (!member) {
return res.status(404).send({ error: 'Guild not found' });
}
if (!member.permissions.has(PermissionFlagsBits.ManageGuild)) {
return res.status(403).send({ error: 'Missing permissions' });
}

const caseData = await client.cases.getOne(guildId, parseInt(caseId));

if (!caseData) {
return res.status(404).send({ error: 'Case not found' });
}

await client.cases.delete(guildId, parseInt(caseId));
res.status(204).send();
}
14 changes: 11 additions & 3 deletions src/api/guilds/[id]/cases/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,26 @@ export async function getCases(client: FluorineClient, req: FastifyRequest, repl

const { id: userId } = client.oauth.verify(authorization);
const guild = client.guilds.cache.get(guildId);

if (!guild) {
return reply.status(404).send({ error: 'Guild not found' });
}

const member = await guild.members.fetch(userId);

if (!member.permissions.has(PermissionsBitField.Flags.ManageGuild)) {
return reply.status(403).send({ error: 'Missing permissions' });
}

const cases = await client.cases.getGuild(guildId);

const casesWithIsoString = cases.map(caseData => ({
...caseData,
createdAt: new Date(Number(caseData.createdAt)).toISOString()
}));

reply.header('Content-Type', 'application/json');
reply.send(
JSON.stringify(await client.cases.getGuild(guildId), (key, value) =>
typeof value === 'bigint' ? value.toString() : value
)
JSON.stringify(casesWithIsoString, (key, value) => (typeof value === 'bigint' ? value.toString() : value))
);
}
10 changes: 6 additions & 4 deletions src/classes/modules/CasesModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export class CasesModule {
caseCreator: BigInt(caseCreator.id),
moderatedUser: BigInt(moderatedUser.id),
type,
reason
reason,
createdAt: BigInt(Date.now())
}
});

Expand Down Expand Up @@ -89,6 +90,7 @@ export class CasesModule {
channel.send({ embeds: [embed] });
}
}

async getGuild(id: string) {
return this.client.prisma.case.findMany({
where: {
Expand All @@ -112,11 +114,11 @@ export class CasesModule {
}

async delete(guildId: string, caseId: number) {
const Case = await this.getOne(guildId, caseId);
const caseData = await this.getOne(guildId, caseId);
const guild = this.client.guilds.cache.get(guildId);
const user = await this.client.users.fetch(Case.moderatedUser.toString());
const user = await this.client.users.fetch(caseData.moderatedUser.toString());

switch (Case.type) {
switch (caseData.type) {
case 'ban':
await guild.bans.remove(user);
break;
Expand Down
9 changes: 8 additions & 1 deletion src/commands/case/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,14 @@ export async function onInteraction(
const chunkPage = interaction.isCommand() ? page : componentPage;

chunk[chunkPage].forEach(caseData => {
embed.addFields([{ rawName: `#${caseData.caseId} ${caseData.type}`, rawValue: caseData.reason }]);
embed.addFields([
{
rawName: `#${caseData.caseId} (${client.i18n.t(caseData.type.toUpperCase(), {
lng: interaction.locale
})}) <t:${caseData.createdAt / 1000n}:R>`,
rawValue: caseData.reason
}
]);
});

if (chunk.length > 1) {
Expand Down
6 changes: 5 additions & 1 deletion src/commands/case/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ export async function onSlashCommand(client: FluorineClient, interaction: ChatIn
name: 'CASE_TYPE',
value: caseObj.type.toUpperCase()
},
{ name: 'CASE_REASON', rawValue: caseObj.reason }
{ name: 'CASE_REASON', rawValue: caseObj.reason },
{
name: 'CASE_CREATED_AT',
rawValue: `<t:${caseObj.createdAt / 1000n}:d> (<t:${caseObj.createdAt / 1000n}:R>)`
}
]);

interaction.reply({ embeds: [embed] });
Expand Down

0 comments on commit 135b528

Please sign in to comment.