Skip to content

Commit

Permalink
feat: improve settings command
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklvh committed Mar 25, 2024
1 parent 9cefb68 commit 93b362f
Show file tree
Hide file tree
Showing 3 changed files with 301 additions and 245 deletions.
44 changes: 34 additions & 10 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,48 @@ datasource db {
}

model Guild {
id String @id
id String @id
members Member[]
logging Logging?
fun Fun?
banned Boolean? @default(false)
starboard Starboard?
modlogs Modlog[]
}

model Fun {
enabled Boolean @default(false)
economyEnabled Boolean @default(false)
countingId String?
countingEnabled Boolean @default(false)
guildId String @unique
guild Guild @relation(fields: [guildId], references: [id])
}

model Logging {
enabled Boolean @default(false)
modlogId String?
welcomeId String?
auditlogId String?
members Member[]
logging Boolean? @default(false)
welcome Boolean? @default(false)
starboard Boolean? @default(false)
fun Boolean? @default(false)
banned Boolean? @default(false)
welcomeId String?
guildId String @unique
guild Guild @relation(fields: [guildId], references: [id])
}

model Starboard {
enabled Boolean @default(false)
starsRequired Int? @default(5)
starredMessages String[]
guildId String @unique
guild Guild @relation(fields: [guildId], references: [id])
}

model Member {
id String @id
id String @id
coins Int?
xp Int?
level Int?
Guild Guild @relation(fields: [guildId], references: [id])
guildId String
Guild Guild @relation(fields: [guildId], references: [id])
}

model Modlog {
Expand All @@ -39,6 +62,7 @@ model Modlog {
type ModerationType
createdAt DateTime @unique @default(now())
guildId String
guild Guild @relation(fields: [guildId], references: [id])
}

model Ban {
Expand Down
83 changes: 48 additions & 35 deletions src/classes/Helpers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ModerationType } from "@prisma/client";
import { container } from "@sapphire/framework";
import {
Guild,
Guild as DiscordGuild,
type GuildTextBasedChannel,
ChatInputCommandInteraction,
User,
Expand All @@ -11,19 +11,47 @@ import {
import { ModerationTypeNamesPast } from "../types/index.js";

export class Helpers {
public async welcomeChecks(
guild: Guild
): Promise<GuildTextBasedChannel | false> {
const guildInDB = await container.prisma.guild.findUnique({
where: {
id: guild.id,
public async getGuild(guildId: string) {
let guild = await container.prisma.guild.findUnique({
where: { id: guildId },
include: {
logging: true,
starboard: true,
fun: true,
modlogs: true,
},
});

if (!guildInDB || !guildInDB.logging || !guildInDB.welcomeId) return false;
if (!guild) {
guild = await container.prisma.guild.create({
data: {
id: guildId,
starboard: { create: {} },
logging: { create: {} },
fun: { create: {} },
},
include: {
logging: true,
starboard: true,
fun: true,
modlogs: true,
},
});
}

return guild;
}

public async welcomeChecks(
guild: DiscordGuild
): Promise<GuildTextBasedChannel | false> {
const guildInDB = await this.getGuild(guild.id);

if (!guildInDB.logging?.enabled || !guildInDB.logging.welcomeId)
return false;

const welcomeChannel = await container.client.channels
.fetch(guildInDB.welcomeId)
.fetch(guildInDB.logging.welcomeId)
.catch(() =>
container.logger.error(
`Invalid welcome channel ID for Guild ${guild.name} (${guild.id})`
Expand All @@ -36,18 +64,15 @@ export class Helpers {
}

public async auditlogChecks(
guild: Guild
guild: DiscordGuild
): Promise<GuildTextBasedChannel | false> {
const guildInDB = await container.prisma.guild.findUnique({
where: {
id: guild.id,
},
});
const guildInDB = await this.getGuild(guild.id);

if (!guildInDB || !guildInDB.logging || !guildInDB.auditlogId) return false;
if (!guildInDB.logging?.enabled || !guildInDB.logging.auditlogId)
return false;

const loggingChannel = await container.client.channels
.fetch(guildInDB.auditlogId)
.fetch(guildInDB.logging.auditlogId)
.catch(() =>
container.logger.error(
`Invalid logging channel ID for Guild ${guild.name} (${guild.id})`
Expand Down Expand Up @@ -99,7 +124,7 @@ export class Helpers {
private async sendModMsgToUser(
type: ModerationType,
user: User,
guild: Guild,
guild: DiscordGuild,
reason: string
) {
const embed = new EmbedBuilder()
Expand Down Expand Up @@ -144,33 +169,21 @@ export class Helpers {

private async sendModMsgToModlog(
type: ModerationType,
guild: Guild,
guild: DiscordGuild,
user: User,
moderator: User,
reason: string
) {
let guildInDB = await container.prisma.guild.findUnique({
where: {
id: guild.id,
},
})!;

if (!guildInDB) {
guildInDB = await container.prisma.guild.create({
data: {
id: guild.id,
},
})!;
}
const guildInDB = await this.getGuild(guild.id);

if (!guildInDB || !guildInDB.modlogId) return;
if (!guildInDB.logging!.enabled || !guildInDB.logging!.modlogId) return;

const modlogChannel = await guild.channels
.fetch(guildInDB.modlogId!)
.fetch(guildInDB.logging!.modlogId)
.catch(() =>
container.logger.error(
`[${guild.name} (${guild.id})] Could not fetch modlog channel ${
guildInDB!.modlogId
guildInDB.logging!.modlogId
}`
)
);
Expand Down
Loading

0 comments on commit 93b362f

Please sign in to comment.