clean up: autoformat

This commit is contained in:
Baipyrus 2024-04-11 12:09:32 +02:00
parent 034beca51c
commit 09f9280ae1

View File

@ -1,60 +1,60 @@
import { Keywords, Responses, sequelize } from '../../database.js'; import { Keywords, Responses, sequelize } from '../../database.js';
import { Events, Message } from 'discord.js'; import { Events, Message } from 'discord.js';
import { Op } from 'sequelize'; import { Op } from 'sequelize';
export const name = Events.MessageCreate; export const name = Events.MessageCreate;
/** @param {Message} message */ /** @param {Message} message */
export async function execute(message) { export async function execute(message) {
// Ignore direct messages and own messages // Ignore direct messages and own messages
if (!message.inGuild() || message.author.id === process.env.CLIENT) return; if (!message.inGuild() || message.author.id === process.env.CLIENT) return;
// Split message content into words // Split message content into words
const words = message.content const words = message.content
.toLowerCase() .toLowerCase()
.split(/\s+/) .split(/\s+/)
.flatMap((word) => { .flatMap((word) => {
const without = word.replace(/[^\w\s]/g, ''); const without = word.replace(/[^\w\s]/g, '');
return without !== word ? [word, without] : word; return without !== word ? [word, without] : word;
}) })
.filter((w, i, a) => a.indexOf(w) === i); .filter((w, i, a) => a.indexOf(w) === i);
// Get guild keywords // Get guild keywords
/** @type {import('../../models/keywords.js').Keyword[]} */ /** @type {import('../../models/keywords.js').Keyword[]} */
const keywords = await Keywords.findAll({ const keywords = await Keywords.findAll({
where: { where: {
guild: message.guildId, guild: message.guildId,
name: { name: {
[Op.in]: words [Op.in]: words
} }
} }
}); });
// Ignore if no keywords found // Ignore if no keywords found
if (keywords.length === 0) return; if (keywords.length === 0) return;
// Get guild responses // Get guild responses
/** @type {import('../../models/responses.js').Response|null} */ /** @type {import('../../models/responses.js').Response|null} */
const response = await Responses.findOne({ const response = await Responses.findOne({
where: { where: {
keyword: { keyword: {
[Op.in]: keywords.map((keyword) => keyword.id) [Op.in]: keywords.map((keyword) => keyword.id)
} }
}, },
order: sequelize.random() order: sequelize.random()
}); });
// Ignore if no response found // Ignore if no response found
if (response.length === null) return; if (response.length === null) return;
// Send response, selecting exactly one at random // Send response, selecting exactly one at random
await message.reply({ await message.reply({
content: response.response, content: response.response,
allowedMentions: { allowedMentions: {
repliedUser: false repliedUser: false
} }
}); });
console.info( console.info(
`[INFO] Responded to keyword with '${response.name}' in guild with ID '${message.guild.id}'.` `[INFO] Responded to keyword with '${response.name}' in guild with ID '${message.guild.id}'.`
); );
} }