generated from nhcarrigan/template
All checks were successful
Node.js CI / Lint and Test (push) Successful in 1m20s
### Explanation _No response_ ### Issue _No response_ ### Attestations - [x] I have read and agree to the [Code of Conduct](https://docs.nhcarrigan.com/community/coc/) - [x] I have read and agree to the [Community Guidelines](https://docs.nhcarrigan.com/community/guide/). - [x] My contribution complies with the [Contributor Covenant](https://docs.nhcarrigan.com/dev/covenant/). ### Dependencies - [x] I have pinned the dependencies to a specific patch version. ### Style - [x] I have run the linter and resolved any errors. - [x] My pull request uses an appropriate title, matching the conventional commit standards. - [x] My scope of feat/fix/chore/etc. correctly matches the nature of changes in my pull request. ### Tests - [ ] My contribution adds new code, and I have added tests to cover it. - [ ] My contribution modifies existing code, and I have updated the tests to reflect these changes. - [ ] All new and existing tests pass locally with my changes. - [ ] Code coverage remains at or above the configured threshold. ### Documentation _No response_ ### Versioning _No response_ Reviewed-on: #4 Co-authored-by: Naomi Carrigan <commits@nhcarrigan.com> Co-committed-by: Naomi Carrigan <commits@nhcarrigan.com>
96 lines
4.6 KiB
JavaScript
96 lines
4.6 KiB
JavaScript
/**
|
|
* @copyright nhcarrigan
|
|
* @license Naomi's Public License
|
|
* @author Naomi Carrigan
|
|
*/
|
|
import { ai } from "../modules/ai.js";
|
|
import { checkGuildEntitlement, checkUserEntitlement, } from "../utils/checkEntitlement.js";
|
|
import { errorHandler } from "../utils/errorHandler.js";
|
|
/**
|
|
* Handles the creation of a message in Discord. If Hikari is mentioned in the message,
|
|
* trigger a response.
|
|
* @param hikari - Hikari's Discord instance.
|
|
* @param message - The message payload from Discord.
|
|
*/
|
|
// eslint-disable-next-line max-lines-per-function -- This function is large, but it handles a lot of logic.
|
|
const guildMessageCreate = async (hikari, message) => {
|
|
try {
|
|
if (!hikari.user || !message.mentions.has(hikari.user.id)) {
|
|
return;
|
|
}
|
|
await message.channel.sendTyping();
|
|
const hasSubscription = await checkGuildEntitlement(hikari, message.guild);
|
|
if (!hasSubscription) {
|
|
await message.reply({
|
|
content:
|
|
// eslint-disable-next-line stylistic/max-len -- Big boi string.
|
|
"Your server is not currently subscribed to use this service. Unfortunately, due to Discord restrictions, we cannot offer server subscriptions just yet. We are hard at work on our own payment system, so stay tuned! Until then, you can subscribe as a user and ask questions by DMing me directly!",
|
|
});
|
|
return;
|
|
}
|
|
if (!message.channel.isThread()) {
|
|
const thread = await message.startThread({
|
|
autoArchiveDuration: 60,
|
|
name: `Thread for ${message.author.username}`,
|
|
});
|
|
// Wait five seconds for the thread to be created
|
|
await new Promise((resolve) => {
|
|
// eslint-disable-next-line no-promise-executor-return -- We want to wait for a bit.
|
|
return setTimeout(resolve, 5000);
|
|
});
|
|
await ai(hikari, [message], message.member?.nickname ?? message.author.displayName, thread);
|
|
return;
|
|
}
|
|
const previousMessages = await message.channel.messages.fetch({
|
|
limit: 100,
|
|
});
|
|
await ai(hikari, [...previousMessages.values()], message.member?.nickname ?? message.author.displayName, message.channel);
|
|
}
|
|
catch (error) {
|
|
const id = await errorHandler(error, "message create event");
|
|
await message.reply({
|
|
content: `Something went wrong while processing your request. Please try again later, or [reach out in our support channel](<https://discord.com/channels/1354624415861833870/1385797209706201198>).\n-# ${id}`,
|
|
});
|
|
}
|
|
};
|
|
/**
|
|
* Processes the creation of a direct message in Discord.
|
|
* @param hikari - Hikari's Discord instance.
|
|
* @param message - The message payload from Discord.
|
|
*/
|
|
const directMessageCreate = async (hikari, message) => {
|
|
try {
|
|
if (message.author.bot || message.content === "<Clear History>") {
|
|
// Ignore bot messages and the clear history message
|
|
return;
|
|
}
|
|
await message.channel.sendTyping();
|
|
const hasSubscription = await checkUserEntitlement(hikari, message.author);
|
|
if (!hasSubscription) {
|
|
await message.reply({
|
|
content:
|
|
// eslint-disable-next-line stylistic/max-len -- Big boi string.
|
|
"You are not currently subscribed to use this service. Please note that a user subscription is NOT the same as a server subscription.",
|
|
});
|
|
return;
|
|
}
|
|
const historyRequest = await message.channel.messages.fetch({ limit: 100 });
|
|
const history = [...historyRequest.values()];
|
|
const clearMessageIndex = history.findIndex((messageInner) => {
|
|
return messageInner.content === "<Clear History>";
|
|
});
|
|
if (clearMessageIndex !== -1) {
|
|
// Remove the clear message and everything sent before it, which means everything after in the array because the array is backwards
|
|
history.splice(clearMessageIndex, history.length - clearMessageIndex);
|
|
}
|
|
await ai(hikari, history.reverse(), message.member?.nickname ?? message.author.displayName, message.channel);
|
|
}
|
|
catch (error) {
|
|
const id = await errorHandler(error, "message create event");
|
|
await message.reply({
|
|
content: `Something went wrong while processing your request. Please try again later, or [reach out in our support channel](<https://discord.com/channels/1354624415861833870/1385797209706201198>).\n-# ${id}`,
|
|
});
|
|
}
|
|
};
|
|
export { guildMessageCreate, directMessageCreate };
|