♻️ Migrate counters to Prisma
This commit is contained in:
parent
46d95b59ef
commit
56e3110e2b
4 changed files with 65 additions and 96 deletions
|
@ -1,42 +0,0 @@
|
|||
import { Snowflake } from "discord.js";
|
||||
import { model, Schema } from "mongoose";
|
||||
|
||||
export interface ICounter {
|
||||
guildId: Snowflake;
|
||||
channelId: Snowflake;
|
||||
word: string;
|
||||
counter: number;
|
||||
}
|
||||
|
||||
const counterSchema = new Schema<ICounter>(
|
||||
{
|
||||
guildId: {
|
||||
type: String,
|
||||
required: true,
|
||||
unique: false,
|
||||
index: true,
|
||||
},
|
||||
channelId: {
|
||||
type: String,
|
||||
required: true,
|
||||
unique: true,
|
||||
index: true,
|
||||
},
|
||||
word: {
|
||||
type: String,
|
||||
required: true,
|
||||
unique: false,
|
||||
index: true,
|
||||
},
|
||||
counter: {
|
||||
type: Number,
|
||||
required: true,
|
||||
unique: false,
|
||||
index: true,
|
||||
default: 0,
|
||||
},
|
||||
},
|
||||
{ timestamps: true }
|
||||
);
|
||||
|
||||
export default model<ICounter>("counter", counterSchema);
|
|
@ -1,6 +1,6 @@
|
|||
import { ChannelType, Message } from "discord.js";
|
||||
import logger from "../../../../../middlewares/logger";
|
||||
import counterSchema from "../../../../../models/counter";
|
||||
import prisma from "../../../../../prisma";
|
||||
|
||||
export default {
|
||||
execute: async (message: Message) => {
|
||||
|
@ -13,21 +13,20 @@ export default {
|
|||
const messages = await message.channel.messages.fetch({ limit: 2 });
|
||||
const lastMessage = messages.last();
|
||||
|
||||
const { id: guildId } = guild;
|
||||
const { id: channelId } = channel;
|
||||
|
||||
const counter = await counterSchema.findOne({
|
||||
guildId,
|
||||
channelId,
|
||||
const channelCounter = await prisma.guildCounter.findUnique({
|
||||
where: {
|
||||
guildId_channelId: {
|
||||
guildId: guild.id,
|
||||
channelId: channel.id,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (counter === null) {
|
||||
throw new Error("No counter found in database.");
|
||||
}
|
||||
if (!channelCounter) throw new Error("No counters found in channel.");
|
||||
|
||||
if (
|
||||
lastMessage?.author.id === author.id &&
|
||||
channel.id === counter.channelId
|
||||
channel.id === channelCounter.channelId
|
||||
) {
|
||||
logger.silly(
|
||||
`${author.username} sent the last message therefor not allowing again.`
|
||||
|
@ -36,29 +35,32 @@ export default {
|
|||
return;
|
||||
}
|
||||
|
||||
if (content !== counter.word) {
|
||||
if (content !== channelCounter.triggerWord) {
|
||||
logger.silly(
|
||||
`Counter word ${counter.word} does not match message ${content}`
|
||||
`Counter word ${channelCounter.triggerWord} does not match message ${content}`
|
||||
);
|
||||
|
||||
await message.delete();
|
||||
return;
|
||||
}
|
||||
|
||||
counter.counter += 1;
|
||||
await counter
|
||||
.save()
|
||||
.then(() => {
|
||||
logger.silly(
|
||||
`Counter for guild ${guildId} and channel ${channelId} is now ${counter.counter}`
|
||||
);
|
||||
})
|
||||
.catch(() => {
|
||||
throw new Error(`Error saving counter to database.`);
|
||||
});
|
||||
const updateGuildCounter = await prisma.guildCounter.update({
|
||||
where: {
|
||||
guildId_channelId: {
|
||||
guildId: guild.id,
|
||||
channelId: channel.id,
|
||||
},
|
||||
},
|
||||
data: {
|
||||
count: {
|
||||
increment: 1,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
logger.silly(
|
||||
`Counter word ${counter.word} was found in message ${content} from ${author.tag} (${author.id}) in guild: ${guild?.name} (${guild?.id})`
|
||||
);
|
||||
logger.silly(updateGuildCounter);
|
||||
|
||||
if (!updateGuildCounter)
|
||||
logger.error(`Failed to update counter - ${updateGuildCounter}`);
|
||||
},
|
||||
};
|
||||
|
|
|
@ -1,36 +1,38 @@
|
|||
// Dependencies
|
||||
import { Message } from "discord.js";
|
||||
|
||||
// Models
|
||||
import logger from "../../../../middlewares/logger";
|
||||
import counterSchema from "../../../../models/counter";
|
||||
import prisma from "../../../../prisma";
|
||||
|
||||
export default async (message: Message) => {
|
||||
const { guild, channel, author, content } = message;
|
||||
|
||||
const counter = await counterSchema?.findOne({
|
||||
guildId: guild?.id,
|
||||
channelId: channel?.id,
|
||||
if (!guild) throw new Error("Guild not found");
|
||||
if (!channel) throw new Error("Channel not found");
|
||||
|
||||
const channelCounter = await prisma.guildCounter.findUnique({
|
||||
where: {
|
||||
guildId_channelId: {
|
||||
guildId: guild.id,
|
||||
channelId: channel.id,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (counter === null)
|
||||
return logger?.silly(
|
||||
`No counter found for guild: ${guild?.name} (${guild?.id})`
|
||||
);
|
||||
const { word } = counter;
|
||||
if (!channelCounter) throw new Error("No counter found in channel.");
|
||||
|
||||
const messages = await message.channel.messages.fetch({ limit: 1 });
|
||||
const lastMessage = messages.last();
|
||||
|
||||
if (!lastMessage) return;
|
||||
|
||||
if (content !== word) return;
|
||||
if (content !== channelCounter.triggerWord) return;
|
||||
|
||||
if (lastMessage.author.id === message.author.id) return;
|
||||
|
||||
channel?.send(`${author} said **${word}**.`);
|
||||
logger?.silly(`${author} said ${word} in ${channel}`);
|
||||
channel?.send(`${author} said **${channelCounter.triggerWord}**.`);
|
||||
logger?.silly(`${author} said ${channelCounter.triggerWord} in ${channel}`);
|
||||
return logger?.silly(
|
||||
`User: ${author?.tag} (${author?.id}) in guild: ${guild?.name} (${guild?.id}) said the counter word: ${word}`
|
||||
`User: ${author?.tag} (${author?.id}) in guild: ${guild?.name} (${guild?.id}) said the counter word: ${channelCounter.triggerWord}`
|
||||
);
|
||||
};
|
||||
|
|
|
@ -2,31 +2,38 @@
|
|||
import { Message } from "discord.js";
|
||||
// Models
|
||||
import logger from "../../../../middlewares/logger";
|
||||
import counterSchema from "../../../../models/counter";
|
||||
import prisma from "../../../../prisma";
|
||||
|
||||
export default async (message: Message) => {
|
||||
const { guild, channel, author, content } = message;
|
||||
|
||||
const counter = await counterSchema?.findOne({
|
||||
guildId: guild?.id,
|
||||
channelId: channel?.id,
|
||||
if (!guild) throw new Error("Guild not found");
|
||||
|
||||
if (!channel) throw new Error("Channel not found");
|
||||
|
||||
const channelCounter = await prisma.guildCounter.findUnique({
|
||||
where: {
|
||||
guildId_channelId: {
|
||||
guildId: guild.id,
|
||||
channelId: channel.id,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (counter === null)
|
||||
if (!channelCounter) throw new Error("No counters found in channel.");
|
||||
|
||||
if (content === channelCounter.triggerWord)
|
||||
return logger?.silly(
|
||||
`No counter found for guild: ${guild?.name} (${guild?.id})`
|
||||
);
|
||||
const { word } = counter;
|
||||
if (content === word)
|
||||
return logger?.silly(
|
||||
`User: ${author?.tag} (${author?.id}) in guild: ${guild?.name} (${guild?.id}) said the counter word: ${word}`
|
||||
`User: ${author?.tag} (${author?.id}) in guild: ${guild?.name} (${guild?.id}) said the counter word: ${channelCounter.triggerWord}`
|
||||
);
|
||||
|
||||
await message
|
||||
.delete()
|
||||
.then(async () => {
|
||||
await channel?.send(`${author} said **${word}**.`);
|
||||
logger?.silly(`${author} said ${word} in ${channel}`);
|
||||
await channel?.send(`${author} said **${channelCounter.triggerWord}**.`);
|
||||
logger?.silly(
|
||||
`${author} said ${channelCounter.triggerWord} in ${channel}`
|
||||
);
|
||||
})
|
||||
.catch((error) => {
|
||||
logger.error(error);
|
||||
|
|
Loading…
Add table
Reference in a new issue