♻️ @events/messageCreate
This commit is contained in:
parent
215cecb62b
commit
7d17fb9047
8 changed files with 245 additions and 176 deletions
|
@ -1,36 +1,11 @@
|
||||||
// Dependencies
|
|
||||||
import { Message } from "discord.js";
|
import { Message } from "discord.js";
|
||||||
|
import modules from "@events/messageCreate/modules";
|
||||||
|
|
||||||
// Modules
|
|
||||||
import points from "./modules/points";
|
|
||||||
import credits from "./modules/credits";
|
|
||||||
import counters from "./modules/counters";
|
|
||||||
import fetchUser from "../../helpers/fetchUser";
|
|
||||||
import fetchGuild from "../../helpers/fetchGuild";
|
|
||||||
|
|
||||||
// Function
|
|
||||||
export default {
|
export default {
|
||||||
name: "messageCreate",
|
name: "messageCreate",
|
||||||
async execute(message: Message) {
|
async execute(message: Message) {
|
||||||
const { author, guild } = message;
|
await modules.counters.execute(message);
|
||||||
|
await modules.points.execute(message);
|
||||||
if (author?.bot) return;
|
await modules.counters.execute(message);
|
||||||
|
|
||||||
if (guild === null) return;
|
|
||||||
|
|
||||||
// Get guild object
|
|
||||||
const guildObj = await fetchGuild(guild);
|
|
||||||
|
|
||||||
// Get guild object
|
|
||||||
const userObj = await fetchUser(author, guild);
|
|
||||||
|
|
||||||
// Execute Module - Credits
|
|
||||||
await credits(guildObj, userObj, message);
|
|
||||||
|
|
||||||
// Execute Module - Points
|
|
||||||
await points(guildObj, userObj, message);
|
|
||||||
|
|
||||||
// Execute Module - Counters
|
|
||||||
await counters(message);
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,44 +0,0 @@
|
||||||
import { Message } from "discord.js";
|
|
||||||
|
|
||||||
import logger from "@logger";
|
|
||||||
|
|
||||||
import counterSchema from "@schemas/counter";
|
|
||||||
|
|
||||||
export default async (message: Message) => {
|
|
||||||
const { guild, channel, content } = message;
|
|
||||||
|
|
||||||
if (channel?.type !== "GUILD_TEXT") return;
|
|
||||||
|
|
||||||
const counter = await counterSchema.findOne({
|
|
||||||
guildId: guild?.id,
|
|
||||||
channelId: channel.id,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (counter === null) return;
|
|
||||||
|
|
||||||
logger?.verbose(
|
|
||||||
`Channel: ${channel.name} (${channel.id}) is an active counter channel`
|
|
||||||
);
|
|
||||||
|
|
||||||
if (content !== counter.word) {
|
|
||||||
logger?.verbose(`Message: ${content} is not the counter word`);
|
|
||||||
return message.delete();
|
|
||||||
}
|
|
||||||
|
|
||||||
logger?.verbose(`Message: ${content} is the counter word`);
|
|
||||||
|
|
||||||
await counterSchema
|
|
||||||
.findOneAndUpdate(
|
|
||||||
{
|
|
||||||
guildId: guild?.id,
|
|
||||||
channelId: channel.id,
|
|
||||||
},
|
|
||||||
{ $inc: { counter: 1 } }
|
|
||||||
)
|
|
||||||
.then(async () => {
|
|
||||||
logger?.verbose(`Counter incremented`);
|
|
||||||
})
|
|
||||||
.catch(async () => {
|
|
||||||
logger?.error(`Failed to increment counter`);
|
|
||||||
});
|
|
||||||
};
|
|
58
src/events/messageCreate/modules/counters/index.ts
Normal file
58
src/events/messageCreate/modules/counters/index.ts
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
import { Message } from "discord.js";
|
||||||
|
|
||||||
|
import logger from "@logger";
|
||||||
|
import counterSchema from "@schemas/counter";
|
||||||
|
|
||||||
|
export default {
|
||||||
|
execute: async (message: Message) => {
|
||||||
|
const { guild, author, content, channel } = message;
|
||||||
|
|
||||||
|
if (guild == null) return;
|
||||||
|
if (author.bot) return;
|
||||||
|
if (channel?.type !== "GUILD_TEXT") return;
|
||||||
|
|
||||||
|
const { id: guildId } = guild;
|
||||||
|
const { id: channelId } = channel;
|
||||||
|
|
||||||
|
const counter = await counterSchema.findOne({
|
||||||
|
guildId,
|
||||||
|
channelId,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!counter) {
|
||||||
|
logger.verbose(
|
||||||
|
`No counter found for guild ${guildId} and channel ${channelId}`
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (content !== counter.word) {
|
||||||
|
logger.verbose(
|
||||||
|
`Counter word ${counter.word} does not match message ${content}`
|
||||||
|
);
|
||||||
|
|
||||||
|
await message.delete();
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
counter.counter += 1;
|
||||||
|
await counter
|
||||||
|
.save()
|
||||||
|
.then(async () => {
|
||||||
|
logger.verbose(
|
||||||
|
`Counter for guild ${guildId} and channel ${channelId} is now ${counter.counter}`
|
||||||
|
);
|
||||||
|
})
|
||||||
|
.catch(async (err) => {
|
||||||
|
logger.error(
|
||||||
|
`Error saving counter for guild ${guildId} and channel ${channelId}`,
|
||||||
|
err
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
logger.verbose(
|
||||||
|
`Counter word ${counter.word} was found in message ${content} from ${author.tag} (${author.id}) in guild: ${guild?.name} (${guild?.id})`
|
||||||
|
);
|
||||||
|
},
|
||||||
|
};
|
|
@ -1,51 +0,0 @@
|
||||||
import logger from "../../../logger";
|
|
||||||
import timeouts from "../../../database/schemas/timeout";
|
|
||||||
import { Message } from "discord.js";
|
|
||||||
export default async (guildDB: any, userDB: any, message: Message) => {
|
|
||||||
const { guild, author, content } = message;
|
|
||||||
|
|
||||||
if (content.length < guildDB.credits.minimumLength) return;
|
|
||||||
|
|
||||||
const isTimeout = await timeouts.findOne({
|
|
||||||
guildId: guild?.id,
|
|
||||||
userId: author?.id,
|
|
||||||
timeoutId: "2022-03-15-17-42",
|
|
||||||
});
|
|
||||||
|
|
||||||
if (isTimeout) {
|
|
||||||
return logger?.verbose(
|
|
||||||
`User: ${author?.tag} (${author?.id}) in guild: ${guild?.name} (${guild?.id}) is on timeout for credits`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
userDB.credits += guildDB.credits.rate;
|
|
||||||
|
|
||||||
await userDB
|
|
||||||
.save()
|
|
||||||
.then(async () => {
|
|
||||||
return logger?.verbose(
|
|
||||||
`User: ${author?.tag} (${author?.id}) in guild: ${guild?.name} (${guild?.id})credits incremented`
|
|
||||||
);
|
|
||||||
})
|
|
||||||
.catch(async (error: any) => {
|
|
||||||
return logger?.error(error);
|
|
||||||
});
|
|
||||||
|
|
||||||
await timeouts.create({
|
|
||||||
guildId: guild?.id,
|
|
||||||
userId: author.id,
|
|
||||||
timeoutId: "2022-03-15-17-42",
|
|
||||||
});
|
|
||||||
|
|
||||||
setTimeout(async () => {
|
|
||||||
logger?.verbose(
|
|
||||||
`User: ${author?.tag} (${author?.id}) in guild: ${guild?.name} (${guild?.id}) timeout removed for points`
|
|
||||||
);
|
|
||||||
|
|
||||||
await timeouts.deleteOne({
|
|
||||||
guildId: guild?.id,
|
|
||||||
userId: author?.id,
|
|
||||||
timeoutId: "2022-03-15-17-42",
|
|
||||||
});
|
|
||||||
}, guildDB.credits.timeout);
|
|
||||||
};
|
|
85
src/events/messageCreate/modules/credits/index.ts
Normal file
85
src/events/messageCreate/modules/credits/index.ts
Normal file
|
@ -0,0 +1,85 @@
|
||||||
|
import logger from "@logger";
|
||||||
|
import timeouts from "@schemas/timeout";
|
||||||
|
import { Message } from "discord.js";
|
||||||
|
|
||||||
|
import fetchUser from "@helpers/fetchUser";
|
||||||
|
import fetchGuild from "@helpers/fetchGuild";
|
||||||
|
|
||||||
|
export default {
|
||||||
|
execute: async (message: Message) => {
|
||||||
|
const { guild, author, content, channel } = message;
|
||||||
|
|
||||||
|
if (guild == null) return;
|
||||||
|
if (author.bot) return;
|
||||||
|
if (channel?.type !== "GUILD_TEXT") return;
|
||||||
|
|
||||||
|
const { id: guildId } = guild;
|
||||||
|
const { id: userId } = author;
|
||||||
|
|
||||||
|
const guildData = await fetchGuild(guild);
|
||||||
|
const userData = await fetchUser(author, guild);
|
||||||
|
|
||||||
|
if (content.length < guildData.credits.minimumLength) return;
|
||||||
|
|
||||||
|
const timeoutData = {
|
||||||
|
guildId,
|
||||||
|
userId,
|
||||||
|
timeoutId: "2022-04-14-13-51-00",
|
||||||
|
};
|
||||||
|
|
||||||
|
const timeout = await timeouts.findOne(timeoutData);
|
||||||
|
|
||||||
|
if (timeout) {
|
||||||
|
logger.verbose(
|
||||||
|
`User ${userId} in guild ${guildId} is on timeout 2022-04-14-13-51-00`
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
userData.credits += guildData.credits.rate;
|
||||||
|
|
||||||
|
await userData
|
||||||
|
.save()
|
||||||
|
.then(async () => {
|
||||||
|
logger.verbose(
|
||||||
|
`User ${userId} in guild ${guildId} has ${userData.credits} credits`
|
||||||
|
);
|
||||||
|
})
|
||||||
|
.catch(async (err) => {
|
||||||
|
logger.error(
|
||||||
|
`Error saving credits for user ${userId} in guild ${guildId}`,
|
||||||
|
err
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
await timeouts
|
||||||
|
.create(timeoutData)
|
||||||
|
.then(async () => {
|
||||||
|
logger.verbose(
|
||||||
|
`Timeout 2022-04-14-13-51-00 for user ${userId} in guild ${guildId} has been created`
|
||||||
|
);
|
||||||
|
})
|
||||||
|
.catch(async (err) => {
|
||||||
|
logger.error(
|
||||||
|
`Error creating timeout 2022-04-14-13-51-00 for user ${userId} in guild ${guildId}`,
|
||||||
|
err
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
setTimeout(async () => {
|
||||||
|
await timeouts
|
||||||
|
.deleteOne(timeoutData)
|
||||||
|
.then(async () => {
|
||||||
|
logger.verbose(
|
||||||
|
`Timeout 2022-04-14-13-51-00 for user ${userId} in guild ${guildId} has been deleted`
|
||||||
|
);
|
||||||
|
})
|
||||||
|
.catch(async (err) => {
|
||||||
|
logger.error(
|
||||||
|
`Error deleting timeout 2022-04-14-13-51-00 for user ${userId} in guild ${guildId}`,
|
||||||
|
err
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}, guildData.credits.timeout);
|
||||||
|
},
|
||||||
|
};
|
9
src/events/messageCreate/modules/index.ts
Normal file
9
src/events/messageCreate/modules/index.ts
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
import counters from "@events/messageCreate/modules/counters";
|
||||||
|
import credits from "@events/messageCreate/modules/credits";
|
||||||
|
import points from "@events/messageCreate/modules/points";
|
||||||
|
|
||||||
|
export default {
|
||||||
|
counters,
|
||||||
|
credits,
|
||||||
|
points,
|
||||||
|
};
|
|
@ -1,52 +0,0 @@
|
||||||
import logger from "../../../logger";
|
|
||||||
import timeouts from "../../../database/schemas/timeout";
|
|
||||||
|
|
||||||
import { Message } from "discord.js";
|
|
||||||
export default async (guildDB: any, userDB: any, message: Message) => {
|
|
||||||
const { author, guild, content } = message;
|
|
||||||
|
|
||||||
if (content.length < guildDB.points.minimumLength) return;
|
|
||||||
|
|
||||||
const isTimeout = await timeouts.findOne({
|
|
||||||
guildId: guild?.id,
|
|
||||||
userId: author.id,
|
|
||||||
timeoutId: "2022-03-15-17-41",
|
|
||||||
});
|
|
||||||
|
|
||||||
if (isTimeout) {
|
|
||||||
return logger?.verbose(
|
|
||||||
`User: ${author.tag} (${author.id}) in guild: ${guild?.name} (${guild?.id}) is on timeout for points`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
userDB.points += guildDB.points.rate;
|
|
||||||
|
|
||||||
await userDB
|
|
||||||
.save()
|
|
||||||
.then(async () => {
|
|
||||||
return logger?.verbose(
|
|
||||||
`User: ${author.tag} (${author.id}) in guild: ${guild?.name} (${guild?.id}) points incremented`
|
|
||||||
);
|
|
||||||
})
|
|
||||||
.catch(async (error: any) => {
|
|
||||||
logger?.error(error);
|
|
||||||
});
|
|
||||||
|
|
||||||
await timeouts.create({
|
|
||||||
guildId: guild?.id,
|
|
||||||
userId: author.id,
|
|
||||||
timeoutId: "2022-03-15-17-41",
|
|
||||||
});
|
|
||||||
|
|
||||||
setTimeout(async () => {
|
|
||||||
logger?.verbose(
|
|
||||||
`User: ${author.tag} (${author.id}) in guild: ${guild?.name} (${guild?.id}) timeout removed for points`
|
|
||||||
);
|
|
||||||
|
|
||||||
await timeouts.deleteOne({
|
|
||||||
guildId: guild?.id,
|
|
||||||
userId: author.id,
|
|
||||||
timeoutId: "2022-03-15-17-41",
|
|
||||||
});
|
|
||||||
}, guildDB.points.timeout);
|
|
||||||
};
|
|
89
src/events/messageCreate/modules/points/index.ts
Normal file
89
src/events/messageCreate/modules/points/index.ts
Normal file
|
@ -0,0 +1,89 @@
|
||||||
|
import logger from "@logger";
|
||||||
|
import timeouts from "@schemas/timeout";
|
||||||
|
|
||||||
|
import fetchUser from "@helpers/fetchUser";
|
||||||
|
import fetchGuild from "@helpers/fetchGuild";
|
||||||
|
|
||||||
|
import { Message } from "discord.js";
|
||||||
|
export default {
|
||||||
|
execute: async (message: Message) => {
|
||||||
|
const { guild, author, content, channel } = message;
|
||||||
|
|
||||||
|
if (guild == null) return;
|
||||||
|
if (author.bot) return;
|
||||||
|
if (channel?.type !== "GUILD_TEXT") return;
|
||||||
|
|
||||||
|
const { id: guildId } = guild;
|
||||||
|
const { id: userId } = author;
|
||||||
|
|
||||||
|
const guildData = await fetchGuild(guild);
|
||||||
|
const userData = await fetchUser(author, guild);
|
||||||
|
|
||||||
|
if (content.length < guildData.credits.minimumLength) return;
|
||||||
|
|
||||||
|
const timeoutData = {
|
||||||
|
guildId,
|
||||||
|
userId,
|
||||||
|
timeoutId: "2022-04-14-14-15-00",
|
||||||
|
};
|
||||||
|
|
||||||
|
const timeout = await timeouts.findOne(timeoutData);
|
||||||
|
|
||||||
|
if (timeout) {
|
||||||
|
logger.verbose(
|
||||||
|
`User ${author.tag} (${author.id}) in guild: ${guild?.name} (${guild?.id} is on timeout 2022-04-14-14-15-00`
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
userData.points += guildData.points.rate;
|
||||||
|
|
||||||
|
await userData
|
||||||
|
.save()
|
||||||
|
.then(async () => {
|
||||||
|
logger.verbose(
|
||||||
|
`Successfully saved user ${author.tag} (${author.id}) in guild: ${guild?.name} (${guild?.id})`
|
||||||
|
);
|
||||||
|
})
|
||||||
|
.catch(async (err) => {
|
||||||
|
logger.error(
|
||||||
|
`Error saving points for user ${author.tag} (${author.id}) in guild: ${guild?.name} (${guild?.id})`,
|
||||||
|
err
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
logger.verbose(
|
||||||
|
`User ${author.tag} (${author.id}) in guild: ${guild?.name} (${guild?.id}) has ${userData.points} points`
|
||||||
|
);
|
||||||
|
|
||||||
|
await timeouts
|
||||||
|
.create(timeoutData)
|
||||||
|
.then(async () => {
|
||||||
|
logger.verbose(
|
||||||
|
`Successfully created timeout for user ${author.tag} (${author.id}) in guild: ${guild?.name} (${guild?.id})`
|
||||||
|
);
|
||||||
|
})
|
||||||
|
.catch(async (err) => {
|
||||||
|
logger.error(
|
||||||
|
`Error creating timeout 2022-04-14-14-15-00 for user ${author.tag} (${author.id}) in guild: ${guild?.name} (${guild?.id})`,
|
||||||
|
err
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
setTimeout(async () => {
|
||||||
|
await timeouts
|
||||||
|
.deleteOne(timeoutData)
|
||||||
|
.then(async () => {
|
||||||
|
logger.verbose(
|
||||||
|
`Successfully deleted timeout 2022-04-14-14-15-00 for user ${author.tag} (${author.id}) in guild: ${guild?.name} (${guild?.id})`
|
||||||
|
);
|
||||||
|
})
|
||||||
|
.catch(async (err) => {
|
||||||
|
logger.error(
|
||||||
|
`Error deleting timeout 2022-04-14-14-15-00 for user ${author.tag} (${author.id}) in guild: ${guild?.name} (${guild?.id})`,
|
||||||
|
err
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}, guildData.points.timeout);
|
||||||
|
},
|
||||||
|
};
|
Loading…
Add table
Reference in a new issue