Skip to content

Commit

Permalink
Fix bugs in InteractionCreate and MessageCreate events
Browse files Browse the repository at this point in the history
  • Loading branch information
appujet committed Apr 14, 2024
1 parent a2052df commit 6b674f3
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 321 deletions.
318 changes: 0 additions & 318 deletions src/database/server.ts
Original file line number Diff line number Diff line change
@@ -1,294 +1,3 @@
/* import Database from 'better-sqlite3';
import config from '../config.js';
const db = new Database('./lavamusic.db', {
fileMustExist: false,
readonly: false,
});
db.pragma('journal_mode=WAL');
export default class ServerData {
constructor() {
this.intialize();
}
public intialize(): void {
db.prepare(
'CREATE TABLE IF NOT EXISTS guild (guildId TEXT PRIMARY KEY, prefix TEXT)'
).run();
db.prepare(
'CREATE TABLE IF NOT EXISTS stay (guildId TEXT PRIMARY KEY, textId TEXT, voiceId TEXT)'
).run();
db.prepare('CREATE TABLE IF NOT EXISTS dj (guildId TEXT PRIMARY KEY, mode BOOLEAN)').run();
db.prepare('CREATE TABLE IF NOT EXISTS roles (guildId TEXT, roleId TEXT)').run();
db.prepare(
'CREATE TABLE IF NOT EXISTS playlist (userId TEXT, name TEXT, songs TEXT)'
).run();
db.prepare(
'CREATE TABLE IF NOT EXISTS botchannel (guildId TEXT PRIMARY KEY, textId TEXT)'
).run();
db.prepare(
'CREATE TABLE IF NOT EXISTS setup (guildId TEXT PRIMARY KEY, textId TEXT, messageId TEXT)'
).run();
db.prepare(
'CREATE TABLE IF NOT EXISTS premium (userId TEXT PRIMARY KEY, guildId TEXT)'
).run();
}
public get(guildId: string): any {
let data = db.prepare('SELECT * FROM guild WHERE guildId = ?').get(guildId);
if (!data) {
db.prepare('INSERT INTO guild (guildId) VALUES (?)').run(guildId);
data = db.prepare('SELECT * FROM guild WHERE guildId = ?').get(guildId);
}
return data;
}
public setPrefix(guildId: string, prefix: string): void {
const data: any = db.prepare('SELECT * FROM guild WHERE guildId = ?').get(guildId);
if (!data) {
db.prepare('INSERT INTO guild (guildId, prefix) VALUES (?, ?)').run(guildId, prefix);
} else {
db.prepare('UPDATE guild SET prefix = ? WHERE guildId = ?').run(prefix, guildId);
}
}
public set_247(guildId: string, textId: string, voiceId: string): void {
let data = db.prepare('SELECT * FROM stay WHERE guildId = ?').get(guildId);
if (!data) {
db.prepare('INSERT INTO stay (guildId, textId, voiceId) VALUES (?, ?, ?)').run(
guildId,
textId,
voiceId
);
} else {
db.prepare('UPDATE stay SET textId = ?, voiceId = ? WHERE guildId = ?').run(
textId,
voiceId,
guildId
);
}
}
public delete_247(guildId: string): void {
const data: any = db.prepare('SELECT * FROM stay WHERE guildId = ?').get(guildId);
if (!data) return;
db.prepare('DELETE FROM stay WHERE guildId = ?').run(guildId);
}
public setDj(guildId: string, mode: boolean): void {
const modeNum = mode ? 1 : 0;
let data = db.prepare('SELECT * FROM dj WHERE guildId = ?').get(guildId);
if (!data) {
db.prepare('INSERT INTO dj (guildId, mode) VALUES (?, ?)').run(guildId, modeNum);
} else {
db.prepare('UPDATE dj SET mode = ? WHERE guildId = ?').run(modeNum, guildId);
}
}
public getPrefix(guildId: string): any {
const data: any = db.prepare('SELECT * FROM guild WHERE guildId = ?').get(guildId);
if (!data) {
db.prepare('INSERT INTO guild (guildId, prefix) VALUES (?, ?)').run(
guildId,
config.prefix
);
return {
prefix: config.prefix,
};
} else {
return data;
}
}
public get_247(guildId?: string): any {
if (guildId) {
const data: any = db.prepare('SELECT * FROM stay WHERE guildId = ?').get(guildId);
if (!data) {
return false;
} else {
return data;
}
} else {
const data: any = db.prepare('SELECT * FROM stay').all();
if (!data) {
return false;
} else {
return data;
}
}
}
public getDj(guildId: string): any {
const data: any = db.prepare('SELECT * FROM dj WHERE guildId = ?').get(guildId);
if (!data) {
db.prepare('INSERT INTO dj (guildId) VALUES (?)').run(guildId);
return false;
} else {
return data;
}
}
public getRoles(guildId: string): any {
const data: any = db.prepare('SELECT * FROM roles WHERE guildId = ?').all(guildId);
if (!data) {
return false;
} else {
return data;
}
}
public addRole(guildId: string, roleId: string): void {
const data: any = db
.prepare('SELECT * FROM roles WHERE guildId = ? AND roleId = ?')
.get(guildId, roleId);
if (!data) {
db.prepare('INSERT INTO roles (guildId, roleId) VALUES (?, ?)').run(guildId, roleId);
}
}
public removeRole(guildId: string, roleId: string): void {
const data: any = db
.prepare('SELECT * FROM roles WHERE guildId = ? AND roleId = ?')
.get(guildId, roleId);
if (data) {
db.prepare('DELETE FROM roles WHERE guildId = ? AND roleId = ?').run(guildId, roleId);
}
}
public clearRoles(guildId: string): void {
const data: any = db.prepare('SELECT * FROM roles WHERE guildId = ?').all(guildId);
if (data) {
db.prepare('DELETE FROM roles WHERE guildId = ?').run(guildId);
}
}
public getBotChannel(guildId: string): any {
const data: any = db.prepare('SELECT * FROM botchannel WHERE guildId = ?').get(guildId);
if (!data) {
db.prepare('INSERT INTO botchannel (guildId) VALUES (?)').run(guildId);
return false;
} else {
return data;
}
}
public setBotChannel(guildId: string, textId: string): void {
let data = db.prepare('SELECT * FROM botchannel WHERE guildId = ?').get(guildId);
if (!data) {
db.prepare('INSERT INTO botchannel (guildId, textId) VALUES (?, ?)').run(
guildId,
textId
);
} else {
db.prepare('UPDATE botchannel SET textId = ? WHERE guildId = ?').run(textId, guildId);
}
}
public getSetup(guildId: string): any {
const data: any = db.prepare('SELECT * FROM setup WHERE guildId = ?').get(guildId);
if (!data) {
db.prepare('INSERT INTO setup (guildId) VALUES (?)').run(guildId);
return false;
} else {
return data;
}
}
public setSetup(guildId: string, textId: string, messageId: string): void {
let data = db.prepare('SELECT * FROM setup WHERE guildId = ?').get(guildId);
if (!data) {
db.prepare('INSERT INTO setup (guildId, textId, messageId) VALUES (?, ?, ?)').run(
guildId,
textId,
messageId
);
} else {
db.prepare('UPDATE setup SET textId = ?, messageId = ? WHERE guildId = ?').run(
textId,
messageId,
guildId
);
}
}
public deleteSetup(guildId: string): void {
const data: any = db.prepare('SELECT * FROM setup WHERE guildId = ?').get(guildId);
if (!data) return;
db.prepare('DELETE FROM setup WHERE guildId = ?').run(guildId);
}
public getUser(userId: string): any {
const data: any = db.prepare('SELECT * FROM user WHERE userId = ?').get(userId);
if (!data) {
db.prepare('INSERT INTO user (userId) VALUES (?)').run(userId);
return false;
} else {
return data;
}
}
public getPLaylist(userId: string, name: string): any {
const data: any = db
.prepare('SELECT * FROM playlist WHERE userId = ? AND name = ?')
.get(userId, name);
if (!data) {
return false;
} else {
return data;
}
}
public createPlaylist(userId: string, name: string): void {
const data: any = db
.prepare('SELECT * FROM playlist WHERE userId = ? AND name = ?')
.get(userId, name);
if (!data) {
db.prepare('INSERT INTO playlist (userId, name) VALUES (?, ?)').run(userId, name);
} else {
throw new Error('Playlist already exists');
}
}
public deletePlaylist(userId: string, name: string): void {
const data: any = db
.prepare('SELECT * FROM playlist WHERE userId = ? AND name = ?')
.get(userId, name);
if (data) {
db.prepare('DELETE FROM playlist WHERE userId = ? AND name = ?').run(userId, name);
} else {
throw new Error('Playlist does not exist');
}
}
public addSong(userId: string, name: string, song: string): void {
const data: any = db
.prepare('SELECT * FROM playlist WHERE userId = ? AND name = ?')
.get(userId, name);
if (data) {
const existingSongs = JSON.parse(data.songs || '[]');
const updatedSongs = existingSongs.concat(song);
db.prepare('UPDATE playlist SET songs = ? WHERE userId = ? AND name = ?').run(
JSON.stringify(updatedSongs),
userId,
name
);
} else {
throw new Error('Playlist does not exist');
}
}
public removeSong(userId: string, name: string, song: string): void {
const data: any = db
.prepare('SELECT * FROM playlist WHERE userId = ? AND name = ?')
.get(userId, name);
if (data) {
db.prepare('UPDATE playlist SET songs = ? WHERE userId = ? AND name = ?').run(
JSON.stringify(data.songs.filter((s: string) => s !== song)),
userId,
name
);
} else {
throw new Error('Playlist does not exist');
}
}
}
*/

import { PrismaClient, Guild, Playlist, Dj, Role, Botchannel, Setup, Stay, Song, } from '@prisma/client';
import config from '../config.js';
Expand Down Expand Up @@ -373,18 +82,6 @@ export default class ServerData {
});
}

public async getPrefix(guildId: string): Promise<Guild | null> {
const data = await this.get(guildId);
if (!data) {
return {
guildId,
prefix: config.prefix,
};
} else {
return data;
}
}

public async get_247(guildId?: string): Promise<Stay | Stay[]> {
if (guildId) {
return await this.prisma.stay.findUnique({
Expand Down Expand Up @@ -534,22 +231,7 @@ export default class ServerData {
},
});
}
/* model Playlist {
id String @id @default(dbgenerated())
userId String
name String
songs Song[]

@@unique([userId, name])
}
model Song {
id String @id @default(dbgenerated())
track String
playlistId String
playlist Playlist @relation(fields: [playlistId], references: [id])
}
*/
public async addSong(userId: string, name: string, song: string): Promise<void> {
const playlist = await this.getPLaylist(userId, name);
if (playlist) {
Expand Down
1 change: 1 addition & 0 deletions src/events/client/InteractionCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default class InteractionCreate extends Event {
interaction.type === InteractionType.ApplicationCommand
) {
const { commandName } = interaction;
await this.client.db.get(interaction.guildId); // get or create guild data
const command = this.client.commands.get(interaction.commandName);
if (!command) return;
const ctx = new Context(interaction as any, interaction.options.data as any);
Expand Down
6 changes: 3 additions & 3 deletions src/events/client/MessageCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ export default class MessageCreate extends Event {
return this.client.emit('setupSystem', message);
}
}
let prefix = await this.client.db.getPrefix(message.guildId);
let guild = await this.client.db.get(message.guildId);

const mention = new RegExp(`^<@!?${this.client.user.id}>( |)$`);
if (message.content.match(mention)) {
await message.reply({
content: `Hey, my prefix for this server is \`${prefix.prefix}\` Want more info? then do \`${prefix.prefix}help\`\nStay Safe, Stay Awesome!`,
content: `Hey, my prefix for this server is \`${guild.prefix}\` Want more info? then do \`${guild.prefix}help\`\nStay Safe, Stay Awesome!`,
});
return;
}
const escapeRegex = (str: string): string => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const prefixRegex = new RegExp(
`^(<@!?${this.client.user.id}>|${escapeRegex(prefix.prefix)})\\s*`
`^(<@!?${this.client.user.id}>|${escapeRegex(guild.prefix)})\\s*`
);
if (!prefixRegex.test(message.content)) return;
const [matchedPrefix] = message.content.match(prefixRegex);
Expand Down

0 comments on commit 6b674f3

Please sign in to comment.