Setup and migrate to Prisma (#456)

This commit is contained in:
Peerawas Archavanuntakun 2022-01-06 03:30:32 +07:00 committed by GitHub
parent 129d121364
commit 51d378e4cb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
30 changed files with 605 additions and 273 deletions

View file

@ -1,8 +1,8 @@
import {TextChannel, Message, GuildChannel, ThreadChannel} from 'discord.js';
import {injectable} from 'inversify';
import {Settings} from '../models/index.js';
import errorMsg from '../utils/error-msg.js';
import Command from '.';
import {prisma} from '../utils/db.js';
@injectable()
export default class implements Command {
@ -17,9 +17,13 @@ export default class implements Command {
public async execute(msg: Message, args: string []): Promise<void> {
if (args.length === 0) {
// Show current settings
const settings = await Settings.findByPk(msg.guild!.id);
const settings = await prisma.setting.findUnique({
where: {
guildId: msg.guild!.id,
},
});
if (settings) {
if (settings?.channel) {
let response = `prefix: \`${settings.prefix}\`\n`;
// eslint-disable-next-line @typescript-eslint/no-base-to-string
response += `channel: ${msg.guild!.channels.cache.get(settings.channel)!.toString()}\n`;
@ -47,7 +51,14 @@ export default class implements Command {
case 'prefix': {
const newPrefix = args[1];
await Settings.update({prefix: newPrefix}, {where: {guildId: msg.guild!.id}});
await prisma.setting.update({
where: {
guildId: msg.guild!.id,
},
data: {
prefix: newPrefix,
},
});
await msg.channel.send(`👍 prefix updated to \`${newPrefix}\``);
break;
@ -63,7 +74,14 @@ export default class implements Command {
}
if (channel && channel.type === 'GUILD_TEXT') {
await Settings.update({channel: channel.id}, {where: {guildId: msg.guild!.id}});
await prisma.setting.update({
where: {
guildId: msg.guild!.id,
},
data: {
channel: channel.id,
},
});
await Promise.all([
(channel as TextChannel).send('hey apparently I\'m bound to this channel now'),
@ -83,7 +101,15 @@ export default class implements Command {
return;
}
await Settings.update({playlistLimit}, {where: {guildId: msg.guild!.id}});
await prisma.setting.update({
where: {
guildId: msg.guild!.id,
},
data: {
playlistLimit,
},
});
await msg.channel.send(`👍 playlist-limit updated to ${playlistLimit}`);
break;
}

View file

@ -2,8 +2,8 @@ import {Message, Util} from 'discord.js';
import {injectable} from 'inversify';
import Command from '.';
import {TYPES} from '../types.js';
import {Settings} from '../models/index.js';
import container from '../inversify.config.js';
import {prisma} from '../utils/db.js';
@injectable()
export default class implements Command {
@ -21,7 +21,11 @@ export default class implements Command {
this.commands = container.getAll<Command>(TYPES.Command);
}
const settings = await Settings.findOne({where: {guildId: msg.guild!.id}});
const settings = await prisma.setting.findUnique({
where: {
guildId: msg.guild!.id,
},
});
if (!settings) {
return;

View file

@ -10,7 +10,7 @@ import LoadingMessage from '../utils/loading-message.js';
import errorMsg from '../utils/error-msg.js';
import Command from '.';
import GetSongs from '../services/get-songs.js';
import Settings from '../models/settings.js';
import {prisma} from '../utils/db.js';
@injectable()
export default class implements Command {
@ -41,8 +41,15 @@ export default class implements Command {
// eslint-disable-next-line complexity
public async execute(msg: Message, args: string[]): Promise<void> {
const [targetVoiceChannel] = getMemberVoiceChannel(msg.member!) ?? getMostPopularVoiceChannel(msg.guild!);
const settings = await Settings.findByPk(msg.guild!.id);
const {playlistLimit} = settings!;
const setting = await prisma.setting.findUnique({
where: {
guildId: msg.guild!.id,
}});
if (!setting) {
throw new Error(`Couldn't find settings for guild ${msg.guild!.id}`);
}
const {playlistLimit} = setting;
const res = new LoadingMessage(msg.channel as TextChannel);
await res.start();

View file

@ -1,8 +1,8 @@
import {Message} from 'discord.js';
import {injectable} from 'inversify';
import {Shortcut, Settings} from '../models/index.js';
import errorMsg from '../utils/error-msg.js';
import Command from '.';
import {prisma} from '../utils/db.js';
@injectable()
export default class implements Command {
@ -18,7 +18,11 @@ export default class implements Command {
public async execute(msg: Message, args: string []): Promise<void> {
if (args.length === 0) {
// Get shortcuts for guild
const shortcuts = await Shortcut.findAll({where: {guildId: msg.guild!.id}});
const shortcuts = await prisma.shortcut.findMany({
where: {
guildId: msg.guild!.id,
},
});
if (shortcuts.length === 0) {
await msg.channel.send('no shortcuts exist');
@ -26,7 +30,11 @@ export default class implements Command {
}
// Get prefix for guild
const settings = await Settings.findOne({where: {guildId: msg.guild!.id}});
const settings = await prisma.setting.findUnique({
where: {
guildId: msg.guild!.id,
},
});
if (!settings) {
return;
@ -48,7 +56,12 @@ export default class implements Command {
switch (action) {
case 'set': {
const shortcut = await Shortcut.findOne({where: {guildId: msg.guild!.id, shortcut: shortcutName}});
const shortcut = await prisma.shortcut.findFirst({
where: {
guildId: msg.guild!.id,
shortcut: shortcutName,
},
});
const command = args.slice(2).join(' ');
@ -60,10 +73,15 @@ export default class implements Command {
return;
}
await shortcut.update(newShortcut);
await prisma.shortcut.update({
where: {
id: shortcut.id,
},
data: newShortcut,
});
await msg.channel.send('shortcut updated');
} else {
await Shortcut.create(newShortcut);
await prisma.shortcut.create({data: newShortcut});
await msg.channel.send('shortcut created');
}
@ -72,7 +90,12 @@ export default class implements Command {
case 'delete': {
// Check if shortcut exists
const shortcut = await Shortcut.findOne({where: {guildId: msg.guild!.id, shortcut: shortcutName}});
const shortcut = await prisma.shortcut.findFirst({
where: {
guildId: msg.guild!.id,
shortcut: shortcutName,
},
});
if (!shortcut) {
await msg.channel.send(errorMsg('shortcut doesn\'t exist'));
@ -85,7 +108,11 @@ export default class implements Command {
return;
}
await shortcut.destroy();
await prisma.shortcut.delete({
where: {
id: shortcut.id,
},
});
await msg.channel.send('shortcut deleted');