Skip to content

Commit

Permalink
refactor(badge): fix parameter types
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-737 committed Aug 20, 2023
1 parent 726fc9a commit aad351c
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 18 deletions.
8 changes: 2 additions & 6 deletions src/Commands/Staff/badge.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ChatInputCommandInteraction, SlashCommandBuilder } from 'discord.js';
import { getDb } from '../../Utils/functions/utils';

export default {
staff: true,
Expand Down Expand Up @@ -69,14 +68,11 @@ export default {
const user = interaction.options.getUser('user');
const badge = interaction.options.getString('badge');

const database = getDb();
const userBadges = database?.userBadges;

if (subcommand === 'list') {
require('../../Scripts/badge/list').execute(interaction, userBadges, user);
require('../../Scripts/badge/list').execute(interaction, user);
}
else {
require(`../../Scripts/badge/${subcommand}`).execute(interaction, userBadges, user, badge);
require(`../../Scripts/badge/${subcommand}`).execute(interaction, user, badge);
}
},
};
10 changes: 5 additions & 5 deletions src/Scripts/badge/add.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Prisma } from '@prisma/client';
import { ChatInputCommandInteraction, User } from 'discord.js';
import { getDb } from '../../Utils/functions/utils';

module.exports = {
async execute(
interaction: ChatInputCommandInteraction,
dbCollection: Prisma.userBadgesDelegate<Prisma.RejectOnNotFound | Prisma.RejectPerOperation | undefined>,
user: User,
badge: string,
) {
const userInCollection = await dbCollection.findFirst({ where: { userId: user.id } });
const db = getDb();
const userInCollection = await db.userBadges.findFirst({ where: { userId: user.id } });

if (userInCollection) {
const userBadges = userInCollection.badges;
Expand All @@ -18,13 +18,13 @@ module.exports = {
return;
}
else {
await dbCollection.update({ where: { userId: user.id }, data: { badges: [...userBadges, badge] } });
await db.userBadges.update({ where: { userId: user.id }, data: { badges: [...userBadges, badge] } });
await interaction.reply(`Badge \`${badge}\` added to ${user.username}.`);
return;
}
}
else {
await dbCollection.create({ data: { userId: user.id, badges: [badge] } });
await db.userBadges.create({ data: { userId: user.id, badges: [badge] } });
await interaction.reply(`Badge \`${badge}\` added to ${user.username}.`);
return;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Scripts/badge/list.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Prisma } from '@prisma/client';
import { ChatInputCommandInteraction, User } from 'discord.js';
import { getDb } from '../../Utils/functions/utils';

export = {
async execute(
interaction: ChatInputCommandInteraction,
dbCollection: Prisma.userBadgesDelegate<Prisma.RejectOnNotFound | Prisma.RejectPerOperation | undefined>,
user: User,
) {
const userInCollection = await dbCollection.findFirst({ where: { userId: user.id } });
const db = getDb();
const userInCollection = await db.userBadges.findFirst({ where: { userId: user.id } });
if (!userInCollection) {
await interaction.reply(`User ${user.username} doesn't have any badges!`);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Scripts/badge/remove.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { Prisma } from '@prisma/client';
import { ChatInputCommandInteraction, User } from 'discord.js';
import { getDb } from '../../Utils/functions/utils';

export = {
async execute(
interaction: ChatInputCommandInteraction,
dbCollection: Prisma.userBadgesDelegate<Prisma.RejectOnNotFound | Prisma.RejectPerOperation | undefined>,
user: User,
badge: string,
) {
const userInCollection = await dbCollection.findFirst({ where: { userId: user.id } });
const db = getDb();
const userInCollection = await db.userBadges.findFirst({ where: { userId: user.id } });

if (userInCollection) {
const userBadges = userInCollection.badges;

if (userBadges.includes(badge)) {
userBadges.splice(userBadges.indexOf(badge), 1);
await dbCollection.update({ where: { userId: user.id }, data: { badges: userBadges } });
await db.userBadges.update({ where: { userId: user.id }, data: { badges: userBadges } });
await interaction.reply(`Removed badge \`${badge}\` from user ${user.username}.`);
}
else {
Expand Down

0 comments on commit aad351c

Please sign in to comment.