refactor: 🧑‍💻 refactored event messageCreate

Remove unnecessary code in the messageCreate event
This commit is contained in:
Axel Olausson Holtenäs 2022-12-21 07:46:42 +01:00
parent 30115dcc71
commit c4c03eecfa
5 changed files with 137 additions and 197 deletions

View file

@ -1,22 +1,15 @@
import { Message } from "discord.js";
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 = {
type: "on",
};
// Execute the function
export const execute = async (message: Message) => {
await modules.credits.execute(message);
await modules.points.execute(message);
await modules.counters.execute(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);
await creditsExecute(message);
await pointsExecute(message);
await countersExecute(message);
};

View file

@ -2,68 +2,66 @@ import { ChannelType, Message } from "discord.js";
import prisma from "../../../../handlers/database";
import logger from "../../../../middlewares/logger";
export default {
execute: async (message: Message) => {
const { guild, author, content, channel } = message;
export default async (message: Message) => {
const { guild, author, content, channel } = message;
if (!guild) return;
if (author.bot) return;
if (channel?.type !== ChannelType.GuildText) return;
if (!guild) return;
if (author.bot) return;
if (channel?.type !== ChannelType.GuildText) return;
const messages = await message.channel.messages.fetch({ limit: 2 });
const lastMessage = messages.last();
const messages = await message.channel.messages.fetch({ limit: 2 });
const lastMessage = messages.last();
const channelCounter = await prisma.guildCounters.findUnique({
where: {
guildId_channelId: {
guildId: guild.id,
channelId: channel.id,
},
const channelCounter = await prisma.guildCounters.findUnique({
where: {
guildId_channelId: {
guildId: guild.id,
channelId: channel.id,
},
});
},
});
if (!channelCounter) {
logger.debug("No counters found in channel.");
return;
}
if (!channelCounter) {
logger.debug("No counters found in channel.");
return;
}
if (
lastMessage?.author.id === author.id &&
channel.id === channelCounter.channelId
) {
logger.silly(
`${author.username} sent the last message therefor not allowing again.`
);
await message.delete();
return;
}
if (
lastMessage?.author.id === author.id &&
channel.id === channelCounter.channelId
) {
logger.silly(
`${author.username} sent the last message therefor not allowing again.`
);
await message.delete();
return;
}
if (content !== channelCounter.triggerWord) {
logger.silly(
`Counter word ${channelCounter.triggerWord} does not match message ${content}`
);
if (content !== channelCounter.triggerWord) {
logger.silly(
`Counter word ${channelCounter.triggerWord} does not match message ${content}`
);
await message.delete();
return;
}
await message.delete();
return;
}
const updateGuildCounter = await prisma.guildCounters.update({
where: {
guildId_channelId: {
guildId: guild.id,
channelId: channel.id,
},
const updateGuildCounter = await prisma.guildCounters.update({
where: {
guildId_channelId: {
guildId: guild.id,
channelId: channel.id,
},
data: {
count: {
increment: 1,
},
},
data: {
count: {
increment: 1,
},
});
},
});
logger.silly(updateGuildCounter);
logger.silly(updateGuildCounter);
if (!updateGuildCounter)
logger.error(`Failed to update counter - ${updateGuildCounter}`);
},
if (!updateGuildCounter)
logger.error(`Failed to update counter - ${updateGuildCounter}`);
};

View file

@ -4,86 +4,46 @@ import creditsGive from "../../../../helpers/credits/give";
import cooldown from "../../../../middlewares/cooldown";
import logger from "../../../../middlewares/logger";
export default {
execute: async (message: Message) => {
const { guild, author, content, channel } = message;
export default async (message: Message) => {
const { guild, author, content, channel } = message;
if (!guild) return;
if (author.bot) return;
if (channel.type !== ChannelType.GuildText) return;
if (!guild) return;
if (author.bot) return;
if (channel.type !== ChannelType.GuildText) return;
const createGuildMember = await prisma.guildMember.upsert({
where: {
userId_guildId: {
userId: author.id,
guildId: guild.id,
},
},
update: {},
create: {
user: {
connectOrCreate: {
create: {
id: author.id,
},
where: {
id: author.id,
},
const upsertGuildConfigCredits = await prisma.guildConfigCredits.upsert({
where: {
id: guild.id,
},
update: {},
create: {
guild: {
connectOrCreate: {
create: {
id: guild.id,
},
},
guild: {
connectOrCreate: {
create: {
id: guild.id,
},
where: {
id: guild.id,
},
where: {
id: guild.id,
},
},
},
include: {
user: true,
guild: true,
},
});
},
include: {
guild: true,
},
});
logger.silly(createGuildMember);
logger.silly(upsertGuildConfigCredits);
const upsertGuildConfigCredits = await prisma.guildConfigCredits.upsert({
where: {
id: guild.id,
},
update: {},
create: {
guild: {
connectOrCreate: {
create: {
id: guild.id,
},
where: {
id: guild.id,
},
},
},
},
include: {
guild: true,
},
});
if (content.length < upsertGuildConfigCredits.minimumLength) return;
logger.silly(upsertGuildConfigCredits);
await cooldown(
guild,
author,
"event-messageCreate-credits",
upsertGuildConfigCredits.timeout,
true
);
if (content.length < upsertGuildConfigCredits.minimumLength) return;
await cooldown(
guild,
author,
"event-messageCreate-credits",
upsertGuildConfigCredits.timeout,
true
);
await creditsGive(guild, author, upsertGuildConfigCredits.rate);
},
await creditsGive(guild, author, upsertGuildConfigCredits.rate);
};

View file

@ -1,9 +0,0 @@
import counters from "./counters";
import credits from "./credits";
import points from "./points";
export default {
counters,
credits,
points,
};

View file

@ -3,65 +3,63 @@ import prisma from "../../../../handlers/database";
import cooldown from "../../../../middlewares/cooldown";
import logger from "../../../../middlewares/logger";
export default {
execute: async (message: Message) => {
const { guild, author, content, channel } = message;
export default async (message: Message) => {
const { guild, author, content, channel } = message;
if (!guild) return;
if (author.bot) return;
if (channel.type !== ChannelType.GuildText) return;
if (!guild) return;
if (author.bot) return;
if (channel.type !== ChannelType.GuildText) return;
const upsertGuildConfigPoints = await prisma.guildConfigPoints.upsert({
where: {
id: guild.id,
},
update: {},
create: {
guild: {
connectOrCreate: {
create: {
id: guild.id,
},
where: {
id: guild.id,
},
const upsertGuildConfigPoints = await prisma.guildConfigPoints.upsert({
where: {
id: guild.id,
},
update: {},
create: {
guild: {
connectOrCreate: {
create: {
id: guild.id,
},
where: {
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,
},
});
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: {
pointsEarned: {
increment: upsertGuildConfigPoints.rate,
},
data: {
pointsEarned: {
increment: upsertGuildConfigPoints.rate,
},
},
});
},
});
logger.silly(updateGuildMember);
logger.silly(updateGuildMember);
if (!updateGuildMember)
throw new Error("Failed to update guildMember object");
},
if (!updateGuildMember)
throw new Error("Failed to update guildMember object");
};