Skip to content
This repository has been archived by the owner on Oct 12, 2024. It is now read-only.

Commit

Permalink
Migrate to using prisma
Browse files Browse the repository at this point in the history
  • Loading branch information
RealEthanPlayzDev committed Oct 10, 2023
1 parent f4fd1a4 commit 1f57c28
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 48 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ yarn compile

To run the bot:
```
yarn prisma db push
yarn start
```

## Configuration file
The configuration file uses ``dotenv``, create a file named ".ENV" on the project root and use the following example:
```
METEORIUMBOTTOKEN=bot_token_here
METEORIUMMONGODBURI=mongodb_uri_here
METEORIUMPOSTGRESURL=postgres_url_here
METEORIUMHOLODEXTOKEN=holodex_token_here_optional
METEORIUMAPPLICATIONID=bot_app_id_here
RATELIMITMAXLIMIT=rate_limit_maximum_limit_before_nodejs_terminates_PUT_A_NUMBER_HERE
Expand Down
19 changes: 19 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "postgresql"
url = env("METEORIUMPOSTGRESURL")
}

model Guild {
GuildId String @unique
EnforceSayInExecutor Boolean @default(false)
DisabledCommands String[] @default([])
DisabledCommandCategories String[] @default([])
MuteRoleId String @default("")
}
4 changes: 2 additions & 2 deletions src/commands/Moderation/SayIn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ export const Command: MeteoriumCommand = {
const Ephemeral = interaction.options.getBoolean("ephemeral", false) ? true : false;
await interaction.deferReply({ ephemeral: Ephemeral });

const GuildSetting = await client.Database.Guilds.findOne({ GuildId: String(interaction.guildId) });
const GuildSetting = await client.Database.guild.findUnique({ where: { GuildId: String(interaction.guildId) } });
if (!GuildSetting) return await interaction.editReply({ content: "No guild setting inside database?" });

const ShowExecutorName = GuildSetting.EnforceSayinExecutor && !interaction.member.permissions.has("Administrator", true) ? true : (interaction.options.getBoolean("showexecutorname", false) ? true : false);
const ShowExecutorName = GuildSetting.EnforceSayInExecutor && !interaction.member.permissions.has("Administrator", true) ? true : (interaction.options.getBoolean("showexecutorname", false) ? true : false);
const Message = (ShowExecutorName ? `${interaction.options.getString("message", true)}\n\n(Sayin command executed by ${interaction.user.tag} (${interaction.user.id}))` : interaction.options.getString("message", true));
const Channel = interaction.options.getChannel("channel", false) ? interaction.options.getChannel("channel") : interaction.channel;
const ReplyTarget = interaction.options.getString("replyto", false);
Expand Down
18 changes: 8 additions & 10 deletions src/commands/Moderation/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const Command: MeteoriumCommand = {
}

// Getting the schema for this guild from the database
const GuildSchema = await client.Database.Guilds.findOne({ GuildId: interaction.guildId });
const GuildSchema = await client.Database.guild.findUnique({ where: { GuildId: interaction.guildId } });
if(!GuildSchema) return await interaction.editReply({ content: "Guild does not have schematic?" })

// Subcommand switch
Expand All @@ -56,7 +56,7 @@ export const Command: MeteoriumCommand = {
switch(Subcommand) {
case("enforcesayinexecutor"): {
const Enabled = interaction.options.getBoolean("enabled", true);
client.Database.Guilds.updateOne({ GuildId: interaction.guildId }, { EnforceSayinExecutor: Enabled }).then(async() => {
client.Database.guild.update({ where: { GuildId: GuildSchema.GuildId }, data: { EnforceSayInExecutor: Enabled } }).then(async() => {
return await interaction.editReply({ content: `Successfully configured the \`\`EnforceSayinExecutor\`\` setting (new value is ${Enabled})` });
}).catch(async(err) => {
console.error(`Error while update guild configuration:\n${err}`);
Expand All @@ -66,7 +66,7 @@ export const Command: MeteoriumCommand = {
}
case("muterole"): {
const Role = interaction.options.getRole("role", true);
client.Database.Guilds.updateOne({ GuildId: interaction.guildId }, { MuteRoleId: Role.id }).then(async() => {
client.Database.guild.update({ where: { GuildId: GuildSchema.GuildId }, data: { MuteRoleId: Role.id } }).then(async() => {
return await interaction.editReply({ content: `Successfully configured the \`\`MuteRoleId\`\` setting (new value is ${Role.id})` });
}).catch(async(err) => {
console.error(`Error while update guild configuration:\n${err}`);
Expand All @@ -78,12 +78,10 @@ export const Command: MeteoriumCommand = {
break;
}
case("disabledcommands"): {
const GuildSetting = await client.Database.Guilds.findOne({ GuildId: interaction.guildId });
if (!GuildSetting) return;
switch(Subcommand) {
case("add"): {
const TargetDisabledCommands = interaction.options.getString("commands", true).split(",")
const UpdatedDisabledCommands = GuildSetting.DisabledCommands.concat(TargetDisabledCommands);
const UpdatedDisabledCommands = GuildSchema.DisabledCommands.concat(TargetDisabledCommands);

// Check if command names are valid
let InvalidCommands = []
Expand All @@ -97,7 +95,7 @@ export const Command: MeteoriumCommand = {
});

// Update in database
client.Database.Guilds.updateOne({ GuildId: interaction.guildId }, { DisabledCommands: UpdatedDisabledCommands }).then(async() => {
client.Database.guild.update({ where: { GuildId: GuildSchema.GuildId }, data: { DisabledCommands: UpdatedDisabledCommands } }).then(async() => {
return await interaction.editReply({ content: `Successfully added the new disabled commands.` });
}).catch(async(err) => {
console.error(`Error while update guild configuration:\n${err}`);
Expand All @@ -107,13 +105,13 @@ export const Command: MeteoriumCommand = {
}
case("remove"): {
const TargetRemoveDisabledCommands = interaction.options.getString("commands", true).split(",");
const UpdatedDisabledCommands = GuildSetting.DisabledCommands
const UpdatedDisabledCommands = GuildSchema.DisabledCommands

// Remove the commands that the user wants to remove
UpdatedDisabledCommands.filter(item => { return TargetRemoveDisabledCommands.indexOf(item) === -1 });

// Update in database
client.Database.Guilds.updateOne({ GuildId: interaction.guildId }, { DisabledCommands: UpdatedDisabledCommands }).then(async() => {
client.Database.guild.update({ where: { GuildId: GuildSchema.GuildId }, data: { DisabledCommands: UpdatedDisabledCommands } }).then(async() => {
return await interaction.editReply({ content: `Successfully removed the disabled commands.` });
}).catch(async(err) => {
console.error(`Error while update guild configuration:\n${err}`);
Expand All @@ -126,7 +124,7 @@ export const Command: MeteoriumCommand = {
embeds: [
new MeteoriumEmbedBuilder(undefined, interaction.user)
.setTitle("List of disabled commands")
.setDescription(`\`\`\`\n${GuildSetting.DisabledCommands.join(", ")}\n\`\`\``)
.setDescription(`\`\`\`\n${GuildSchema.DisabledCommands.join(", ")}\n\`\`\``)
]
});
}
Expand Down
6 changes: 3 additions & 3 deletions src/events/guildCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import type { MeteoriumEvent } from ".";

export const Event: MeteoriumEvent<"guildCreate"> = {
async Callback(client, guild) {
if (client.Database.Guilds.findOne({ GuildId: guild.id }) === null) {
console.log(`Creating new GuildSetting for ${guild.id}`);
client.Database.Guilds.insertOne(client.Database.CreateGuildSetting(guild.id));
if (client.Database.guild.findUnique({ where: { GuildId: guild.id } }) === null) {
console.log(`Creating new guild in database for ${guild.id}`);
client.Database.guild.create({ data: { GuildId: guild.id } });
}
return;
}
Expand Down
8 changes: 3 additions & 5 deletions src/util/MeteoriumClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { config } from "dotenv";
import { Player } from "discord-player";
import { HolodexApiClient } from 'holodex.js';
import { lyricsExtractor } from '@discord-player/extractor';
import { PrismaClient } from "@prisma/client";

import * as Commands from '../commands';
import * as Events from "../events";
import { MeteoriumDatabase } from "./MeteoriumDatabase";

const ParseDotEnvConfig = () => {
if (!process.env.METEORIUMBOTTOKEN) { config({"path": "./.ENV"}); }
Expand All @@ -25,14 +26,11 @@ const ParseDotEnvConfig = () => {
export class MeteoriumClient extends Client<true> {
public Config = ParseDotEnvConfig();
public Commands = new Collection<string, Commands.MeteoriumCommand>;
public Database = new MeteoriumDatabase(this.Config.MongoDB_URI);
public Database = new PrismaClient();
public DiscordPlayer = new Player(this);
public LyricsExtractor = lyricsExtractor(this.Config.GeniusAPIKey);
public HolodexClient = new HolodexApiClient({ apiKey: this.Config.HolodexAPIKey });
public override async login() {
console.log("Connecting to Mongo database");
await Promise.all([ this.Database.connect() ]);

console.log("Loading discord-player default extractors");
this.DiscordPlayer.extractors.loadDefault();

Expand Down
27 changes: 0 additions & 27 deletions src/util/MeteoriumDatabase.ts

This file was deleted.

0 comments on commit 1f57c28

Please sign in to comment.