refactor: 🧑💻 refactored event messageCreate
Remove unnecessary code in the messageCreate event
This commit is contained in:
parent
e8bfd0cc15
commit
d62baa0b9c
5 changed files with 137 additions and 197 deletions
|
@ -1,22 +1,15 @@
|
||||||
import { Message } from "discord.js";
|
import { Message } from "discord.js";
|
||||||
import { IEventOptions } from "../../interfaces/EventOptions";
|
import { IEventOptions } from "../../interfaces/EventOptions";
|
||||||
import modules from "./modules";
|
import countersExecute from "./modules/counters";
|
||||||
|
import creditsExecute from "./modules/credits";
|
||||||
|
import pointsExecute from "./modules/points";
|
||||||
|
|
||||||
export const options: IEventOptions = {
|
export const options: IEventOptions = {
|
||||||
type: "on",
|
type: "on",
|
||||||
};
|
};
|
||||||
|
|
||||||
// Execute the function
|
|
||||||
export const execute = async (message: Message) => {
|
export const execute = async (message: Message) => {
|
||||||
await modules.credits.execute(message);
|
await creditsExecute(message);
|
||||||
await modules.points.execute(message);
|
await pointsExecute(message);
|
||||||
await modules.counters.execute(message);
|
await countersExecute(message);
|
||||||
|
|
||||||
if (!message.member) return;
|
|
||||||
if (message.author.bot) return;
|
|
||||||
|
|
||||||
// client.emit("guildMemberAdd", message.member);
|
|
||||||
// client.emit("guildMemberRemove", message.member);
|
|
||||||
// client.emit("messageDelete", message);
|
|
||||||
// client.emit("messageUpdate", message, message);
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -2,68 +2,66 @@ import { ChannelType, Message } from "discord.js";
|
||||||
import prisma from "../../../../handlers/database";
|
import prisma from "../../../../handlers/database";
|
||||||
import logger from "../../../../middlewares/logger";
|
import logger from "../../../../middlewares/logger";
|
||||||
|
|
||||||
export default {
|
export default async (message: Message) => {
|
||||||
execute: async (message: Message) => {
|
const { guild, author, content, channel } = message;
|
||||||
const { guild, author, content, channel } = message;
|
|
||||||
|
|
||||||
if (!guild) return;
|
if (!guild) return;
|
||||||
if (author.bot) return;
|
if (author.bot) return;
|
||||||
if (channel?.type !== ChannelType.GuildText) return;
|
if (channel?.type !== ChannelType.GuildText) return;
|
||||||
|
|
||||||
const messages = await message.channel.messages.fetch({ limit: 2 });
|
const messages = await message.channel.messages.fetch({ limit: 2 });
|
||||||
const lastMessage = messages.last();
|
const lastMessage = messages.last();
|
||||||
|
|
||||||
const channelCounter = await prisma.guildCounters.findUnique({
|
const channelCounter = await prisma.guildCounters.findUnique({
|
||||||
where: {
|
where: {
|
||||||
guildId_channelId: {
|
guildId_channelId: {
|
||||||
guildId: guild.id,
|
guildId: guild.id,
|
||||||
channelId: channel.id,
|
channelId: channel.id,
|
||||||
},
|
|
||||||
},
|
},
|
||||||
});
|
},
|
||||||
|
});
|
||||||
|
|
||||||
if (!channelCounter) {
|
if (!channelCounter) {
|
||||||
logger.debug("No counters found in channel.");
|
logger.debug("No counters found in channel.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
lastMessage?.author.id === author.id &&
|
lastMessage?.author.id === author.id &&
|
||||||
channel.id === channelCounter.channelId
|
channel.id === channelCounter.channelId
|
||||||
) {
|
) {
|
||||||
logger.silly(
|
logger.silly(
|
||||||
`${author.username} sent the last message therefor not allowing again.`
|
`${author.username} sent the last message therefor not allowing again.`
|
||||||
);
|
);
|
||||||
await message.delete();
|
await message.delete();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (content !== channelCounter.triggerWord) {
|
if (content !== channelCounter.triggerWord) {
|
||||||
logger.silly(
|
logger.silly(
|
||||||
`Counter word ${channelCounter.triggerWord} does not match message ${content}`
|
`Counter word ${channelCounter.triggerWord} does not match message ${content}`
|
||||||
);
|
);
|
||||||
|
|
||||||
await message.delete();
|
await message.delete();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const updateGuildCounter = await prisma.guildCounters.update({
|
const updateGuildCounter = await prisma.guildCounters.update({
|
||||||
where: {
|
where: {
|
||||||
guildId_channelId: {
|
guildId_channelId: {
|
||||||
guildId: guild.id,
|
guildId: guild.id,
|
||||||
channelId: channel.id,
|
channelId: channel.id,
|
||||||
},
|
|
||||||
},
|
},
|
||||||
data: {
|
},
|
||||||
count: {
|
data: {
|
||||||
increment: 1,
|
count: {
|
||||||
},
|
increment: 1,
|
||||||
},
|
},
|
||||||
});
|
},
|
||||||
|
});
|
||||||
|
|
||||||
logger.silly(updateGuildCounter);
|
logger.silly(updateGuildCounter);
|
||||||
|
|
||||||
if (!updateGuildCounter)
|
if (!updateGuildCounter)
|
||||||
logger.error(`Failed to update counter - ${updateGuildCounter}`);
|
logger.error(`Failed to update counter - ${updateGuildCounter}`);
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -4,86 +4,46 @@ import creditsGive from "../../../../helpers/credits/give";
|
||||||
import cooldown from "../../../../middlewares/cooldown";
|
import cooldown from "../../../../middlewares/cooldown";
|
||||||
import logger from "../../../../middlewares/logger";
|
import logger from "../../../../middlewares/logger";
|
||||||
|
|
||||||
export default {
|
export default async (message: Message) => {
|
||||||
execute: async (message: Message) => {
|
const { guild, author, content, channel } = message;
|
||||||
const { guild, author, content, channel } = message;
|
|
||||||
|
|
||||||
if (!guild) return;
|
if (!guild) return;
|
||||||
if (author.bot) return;
|
if (author.bot) return;
|
||||||
if (channel.type !== ChannelType.GuildText) return;
|
if (channel.type !== ChannelType.GuildText) return;
|
||||||
|
|
||||||
const createGuildMember = await prisma.guildMember.upsert({
|
const upsertGuildConfigCredits = await prisma.guildConfigCredits.upsert({
|
||||||
where: {
|
where: {
|
||||||
userId_guildId: {
|
id: guild.id,
|
||||||
userId: author.id,
|
},
|
||||||
guildId: guild.id,
|
update: {},
|
||||||
},
|
create: {
|
||||||
},
|
guild: {
|
||||||
update: {},
|
connectOrCreate: {
|
||||||
create: {
|
create: {
|
||||||
user: {
|
id: guild.id,
|
||||||
connectOrCreate: {
|
|
||||||
create: {
|
|
||||||
id: author.id,
|
|
||||||
},
|
|
||||||
where: {
|
|
||||||
id: author.id,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
where: {
|
||||||
guild: {
|
id: guild.id,
|
||||||
connectOrCreate: {
|
|
||||||
create: {
|
|
||||||
id: guild.id,
|
|
||||||
},
|
|
||||||
where: {
|
|
||||||
id: guild.id,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
include: {
|
},
|
||||||
user: true,
|
include: {
|
||||||
guild: true,
|
guild: true,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
logger.silly(createGuildMember);
|
logger.silly(upsertGuildConfigCredits);
|
||||||
|
|
||||||
const upsertGuildConfigCredits = await prisma.guildConfigCredits.upsert({
|
if (content.length < upsertGuildConfigCredits.minimumLength) return;
|
||||||
where: {
|
|
||||||
id: guild.id,
|
|
||||||
},
|
|
||||||
update: {},
|
|
||||||
create: {
|
|
||||||
guild: {
|
|
||||||
connectOrCreate: {
|
|
||||||
create: {
|
|
||||||
id: guild.id,
|
|
||||||
},
|
|
||||||
where: {
|
|
||||||
id: guild.id,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
include: {
|
|
||||||
guild: true,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
logger.silly(upsertGuildConfigCredits);
|
await cooldown(
|
||||||
|
guild,
|
||||||
|
author,
|
||||||
|
"event-messageCreate-credits",
|
||||||
|
upsertGuildConfigCredits.timeout,
|
||||||
|
true
|
||||||
|
);
|
||||||
|
|
||||||
if (content.length < upsertGuildConfigCredits.minimumLength) return;
|
await creditsGive(guild, author, upsertGuildConfigCredits.rate);
|
||||||
|
|
||||||
await cooldown(
|
|
||||||
guild,
|
|
||||||
author,
|
|
||||||
"event-messageCreate-credits",
|
|
||||||
upsertGuildConfigCredits.timeout,
|
|
||||||
true
|
|
||||||
);
|
|
||||||
|
|
||||||
await creditsGive(guild, author, upsertGuildConfigCredits.rate);
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,9 +0,0 @@
|
||||||
import counters from "./counters";
|
|
||||||
import credits from "./credits";
|
|
||||||
import points from "./points";
|
|
||||||
|
|
||||||
export default {
|
|
||||||
counters,
|
|
||||||
credits,
|
|
||||||
points,
|
|
||||||
};
|
|
|
@ -3,65 +3,63 @@ import prisma from "../../../../handlers/database";
|
||||||
import cooldown from "../../../../middlewares/cooldown";
|
import cooldown from "../../../../middlewares/cooldown";
|
||||||
import logger from "../../../../middlewares/logger";
|
import logger from "../../../../middlewares/logger";
|
||||||
|
|
||||||
export default {
|
export default async (message: Message) => {
|
||||||
execute: async (message: Message) => {
|
const { guild, author, content, channel } = message;
|
||||||
const { guild, author, content, channel } = message;
|
|
||||||
|
|
||||||
if (!guild) return;
|
if (!guild) return;
|
||||||
if (author.bot) return;
|
if (author.bot) return;
|
||||||
if (channel.type !== ChannelType.GuildText) return;
|
if (channel.type !== ChannelType.GuildText) return;
|
||||||
|
|
||||||
const upsertGuildConfigPoints = await prisma.guildConfigPoints.upsert({
|
const upsertGuildConfigPoints = await prisma.guildConfigPoints.upsert({
|
||||||
where: {
|
where: {
|
||||||
id: guild.id,
|
id: guild.id,
|
||||||
},
|
},
|
||||||
update: {},
|
update: {},
|
||||||
create: {
|
create: {
|
||||||
guild: {
|
guild: {
|
||||||
connectOrCreate: {
|
connectOrCreate: {
|
||||||
create: {
|
create: {
|
||||||
id: guild.id,
|
id: guild.id,
|
||||||
},
|
},
|
||||||
where: {
|
where: {
|
||||||
id: guild.id,
|
id: guild.id,
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
include: {
|
},
|
||||||
guild: true,
|
include: {
|
||||||
|
guild: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
logger.silly(upsertGuildConfigPoints);
|
||||||
|
|
||||||
|
if (content.length < upsertGuildConfigPoints.minimumLength) return;
|
||||||
|
|
||||||
|
await cooldown(
|
||||||
|
guild,
|
||||||
|
author,
|
||||||
|
"event-messageCreate-points",
|
||||||
|
upsertGuildConfigPoints.timeout,
|
||||||
|
true
|
||||||
|
);
|
||||||
|
|
||||||
|
const updateGuildMember = await prisma.guildMember.update({
|
||||||
|
where: {
|
||||||
|
userId_guildId: {
|
||||||
|
userId: author.id,
|
||||||
|
guildId: guild.id,
|
||||||
},
|
},
|
||||||
});
|
},
|
||||||
|
data: {
|
||||||
logger.silly(upsertGuildConfigPoints);
|
pointsEarned: {
|
||||||
|
increment: upsertGuildConfigPoints.rate,
|
||||||
if (content.length < upsertGuildConfigPoints.minimumLength) return;
|
|
||||||
|
|
||||||
await cooldown(
|
|
||||||
guild,
|
|
||||||
author,
|
|
||||||
"event-messageCreate-points",
|
|
||||||
upsertGuildConfigPoints.timeout,
|
|
||||||
true
|
|
||||||
);
|
|
||||||
|
|
||||||
const updateGuildMember = await prisma.guildMember.update({
|
|
||||||
where: {
|
|
||||||
userId_guildId: {
|
|
||||||
userId: author.id,
|
|
||||||
guildId: guild.id,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
data: {
|
},
|
||||||
pointsEarned: {
|
});
|
||||||
increment: upsertGuildConfigPoints.rate,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
logger.silly(updateGuildMember);
|
logger.silly(updateGuildMember);
|
||||||
|
|
||||||
if (!updateGuildMember)
|
if (!updateGuildMember)
|
||||||
throw new Error("Failed to update guildMember object");
|
throw new Error("Failed to update guildMember object");
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Reference in a new issue