refactor: autoformat with prettier

This commit is contained in:
Baipyrus 2024-02-06 16:18:06 +01:00
parent 32ceb7646b
commit 4fea72ad88
15 changed files with 227 additions and 212 deletions

View File

@ -1,3 +1,3 @@
## Bot invite ## Bot invite
link [here](https://discord.com/api/oauth2/authorize?client_id=1203887026282438686&permissions=8&scope=applications.commands+bot). link [here](https://discord.com/api/oauth2/authorize?client_id=1203887026282438686&permissions=8&scope=applications.commands+bot).

View File

@ -24,7 +24,7 @@ export async function modalSubmit(interaction) {
if (role === null) { if (role === null) {
await interaction.reply({ await interaction.reply({
content: 'Could not fetch role! Please contact server staff.', content: 'Could not fetch role! Please contact server staff.',
ephemeral: true, ephemeral: true
}); });
return; return;
} }
@ -43,21 +43,24 @@ export async function execute(interaction) {
.setStyle(TextInputStyle.Short) .setStyle(TextInputStyle.Short)
.setCustomId('message') .setCustomId('message')
.setRequired(true) .setRequired(true)
.setValue(id)); .setValue(id)
);
const role = new ActionRowBuilder().addComponents( const role = new ActionRowBuilder().addComponents(
new TextInputBuilder() new TextInputBuilder()
.setLabel('Enter exactly one role ID.') .setLabel('Enter exactly one role ID.')
.setStyle(TextInputStyle.Short) .setStyle(TextInputStyle.Short)
.setCustomId('role') .setCustomId('role')
.setRequired(true)); .setRequired(true)
);
const emoji = new ActionRowBuilder().addComponents( const emoji = new ActionRowBuilder().addComponents(
new TextInputBuilder() new TextInputBuilder()
.setLabel('Enter exactly one emoji.') .setLabel('Enter exactly one emoji.')
.setStyle(TextInputStyle.Short) .setStyle(TextInputStyle.Short)
.setCustomId('emoji') .setCustomId('emoji')
.setRequired(true)); .setRequired(true)
);
modal.addComponents(message, role, emoji); modal.addComponents(message, role, emoji);

View File

@ -15,7 +15,7 @@ export async function execute(interaction) {
// Reply successfully to acknowledge command // Reply successfully to acknowledge command
await interaction.reply({ await interaction.reply({
content: `Successfully saved data from message! Add roles to it with reference ID '${id}'.`, content: `Successfully saved data from message! Add roles to it with reference ID '${id}'.`,
ephemeral: true, ephemeral: true
}); });
console.info(`[INFO] New self roles on message with ID: '${id}'.`); console.info(`[INFO] New self roles on message with ID: '${id}'.`);
@ -24,7 +24,7 @@ export async function execute(interaction) {
// Reply failed to acknowledge command // Reply failed to acknowledge command
await interaction.reply({ await interaction.reply({
content: 'Failed to save data from message!', content: 'Failed to save data from message!',
ephemeral: true, ephemeral: true
}); });
} }
} }

View File

@ -1,6 +1,5 @@
import { Op } from 'sequelize';
import { SlashCommandBuilder } from 'discord.js'; import { SlashCommandBuilder } from 'discord.js';
import { Message, RoleEmojiPair } from './../../database.js'; import { Message } from './../../database.js';
import { addSelfRoles } from '../../shared.js'; import { addSelfRoles } from '../../shared.js';
const createSelfRoles = async (interaction) => { const createSelfRoles = async (interaction) => {
@ -22,7 +21,7 @@ const registerSelfRoles = async (interaction) => {
const id = options.getString('id'); const id = options.getString('id');
const response = { const response = {
success: false, success: false,
msgID: null, msgID: null
}; };
try { try {
@ -32,7 +31,7 @@ const registerSelfRoles = async (interaction) => {
// Reply successfully to acknowledge command // Reply successfully to acknowledge command
await interaction.reply({ await interaction.reply({
content: 'Successfully fetched message!', content: 'Successfully fetched message!',
ephemeral: true, ephemeral: true
}); });
response.success = true; response.success = true;
@ -45,7 +44,7 @@ const registerSelfRoles = async (interaction) => {
// Reply failed to acknowledge command // Reply failed to acknowledge command
await interaction.reply({ await interaction.reply({
content: 'Failed to fetch message!', content: 'Failed to fetch message!',
ephemeral: true, ephemeral: true
}); });
} }
return response; return response;
@ -55,60 +54,64 @@ export const data = new SlashCommandBuilder()
.setName('self_roles') .setName('self_roles')
.setDMPermission(false) .setDMPermission(false)
.setDescription('Manages reactions for self roles.') .setDescription('Manages reactions for self roles.')
.addSubcommand(subcommand => .addSubcommand((subcommand) =>
subcommand subcommand
.setName('create') .setName('create')
.setDescription('Creates new message in channel.') .setDescription('Creates new message in channel.')
.addStringOption(option => .addStringOption((option) =>
option option
.setName('text') .setName('text')
.setRequired(true) .setRequired(true)
.setDescription('The text to be displayed in the message.'))) .setDescription('The text to be displayed in the message.')
.addSubcommand(subcommand => )
)
.addSubcommand((subcommand) =>
subcommand subcommand
.setName('register') .setName('register')
.setDescription('Registers an existing message.') .setDescription('Registers an existing message.')
.addStringOption(option => .addStringOption((option) =>
option option
.setName('id') .setName('id')
.setRequired(true) .setRequired(true)
.setDescription('The ID to reference the message to be used.'))) .setDescription('The ID to reference the message to be used.')
.addSubcommand(subcommand => )
)
.addSubcommand((subcommand) =>
subcommand subcommand
.setName('add') .setName('add')
.setDescription('Add a role-emoji-pair to a message.') .setDescription('Add a role-emoji-pair to a message.')
.addStringOption(option => .addStringOption((option) =>
option option
.setName('id') .setName('id')
.setRequired(true) .setRequired(true)
.setDescription('The ID to reference the message to be used.')) .setDescription('The ID to reference the message to be used.')
.addRoleOption(option => )
option .addRoleOption((option) =>
.setName('role') option.setName('role').setRequired(true).setDescription('The role be assigned to.')
.setRequired(true) )
.setDescription('The role be assigned to.')) .addStringOption((option) =>
.addStringOption(option => option.setName('emoji').setRequired(true).setDescription('The emoji to be reacted with.')
option )
.setName('emoji') );
.setRequired(true)
.setDescription('The emoji to be reacted with.')));
export async function execute(interaction) { export async function execute(interaction) {
const { options } = interaction; const { options } = interaction;
let createNew = false, id; let createNew = false,
id;
switch (options.getSubcommand()) { switch (options.getSubcommand()) {
case 'create': case 'create':
id = await createSelfRoles(interaction); id = await createSelfRoles(interaction);
// Flag to create new database entry // Flag to create new database entry
createNew = true; createNew = true;
break; break;
case 'register': case 'register': {
const response = await registerSelfRoles(interaction); const response = await registerSelfRoles(interaction);
id = response.msgID ?? id; id = response.msgID ?? id;
// Flag to create new database entry // Flag to create new database entry
createNew = response.success; createNew = response.success;
break; break;
case 'add': }
case 'add': {
// Get command options // Get command options
const msgID = options.getString('id'); const msgID = options.getString('id');
const role = options.getRole('role'); const role = options.getRole('role');
@ -116,6 +119,7 @@ export async function execute(interaction) {
// Try adding self role pair // Try adding self role pair
await addSelfRoles(interaction, msgID, role, emoji); await addSelfRoles(interaction, msgID, role, emoji);
break; break;
}
} }
if (createNew) { if (createNew) {
@ -128,7 +132,7 @@ export async function execute(interaction) {
// Reply failed to acknowledge command // Reply failed to acknowledge command
await interaction.followUp({ await interaction.followUp({
content: 'Failed to save data from message!', content: 'Failed to save data from message!',
ephemeral: true, ephemeral: true
}); });
} }

View File

@ -1,20 +1,20 @@
import defineRoleEmojiPair from './models/roleEmojiPairs.js'; import defineRoleEmojiPair from './models/roleEmojiPairs.js';
import defineVoiceChannel from './models/voiceChannels.js'; import defineVoiceChannel from './models/voiceChannels.js';
import defineMessage from './models/messages.js'; import defineMessage from './models/messages.js';
import { Sequelize } from 'sequelize'; import { Sequelize } from 'sequelize';
import { config } from 'dotenv'; import { config } from 'dotenv';
config(); config();
const { DB_NAME } = process.env; const { DB_NAME } = process.env;
const sequelize = new Sequelize({ const sequelize = new Sequelize({
storage: `${DB_NAME}.sqlite`, storage: `${DB_NAME}.sqlite`,
dialect: 'sqlite', dialect: 'sqlite',
logging: false, logging: false
}); });
const RoleEmojiPair = defineRoleEmojiPair(sequelize); const RoleEmojiPair = defineRoleEmojiPair(sequelize);
const VoiceChannel = defineVoiceChannel(sequelize); const VoiceChannel = defineVoiceChannel(sequelize);
const Message = defineMessage(sequelize); const Message = defineMessage(sequelize);
sequelize.sync(); sequelize.sync();
export { sequelize, RoleEmojiPair, VoiceChannel, Message }; export { sequelize, RoleEmojiPair, VoiceChannel, Message };

View File

@ -34,4 +34,5 @@ getFiles(cmdPath)
(await Promise.all(files.map(importAndCheck))) (await Promise.all(files.map(importAndCheck)))
.filter((module) => module !== 0) .filter((module) => module !== 0)
.map((module) => module.data.toJSON()) .map((module) => module.data.toJSON())
).then(putCommands); )
.then(putCommands);

View File

@ -23,7 +23,11 @@ const executeCommand = async (interaction, command) => {
const genericExecute = async (interaction, command, name, description, cmdName) => { const genericExecute = async (interaction, command, name, description, cmdName) => {
try { try {
console.info(`[INFO] Command ${(cmdName ?? interaction.commandName) ?? 'anonymous'} ${description ?? `used "${name}"`}.`); console.info(
`[INFO] Command ${cmdName ?? interaction.commandName ?? 'anonymous'} ${
description ?? `used "${name}"`
}.`
);
await command[name](interaction); await command[name](interaction);
} catch (error) { } catch (error) {
console.error(error); console.error(error);

View File

@ -11,7 +11,7 @@ export async function execute(reaction, user) {
const msgID = reaction.message.id; const msgID = reaction.message.id;
const message = await Message.findOne({ const message = await Message.findOne({
where: { where: {
id: msgID, id: msgID
} }
}); });
// Ignore if unregistered // Ignore if unregistered
@ -22,7 +22,7 @@ export async function execute(reaction, user) {
const rep = await RoleEmojiPair.findOne({ const rep = await RoleEmojiPair.findOne({
where: { where: {
message: msgID, message: msgID,
emoji, emoji
} }
}); });
// Deny if unregistered // Deny if unregistered

View File

@ -11,7 +11,7 @@ export async function execute(reaction, user) {
const msgID = reaction.message.id; const msgID = reaction.message.id;
const message = await Message.findOne({ const message = await Message.findOne({
where: { where: {
id: msgID, id: msgID
} }
}); });
// Ignore if unregistered // Ignore if unregistered
@ -22,7 +22,7 @@ export async function execute(reaction, user) {
const rep = await RoleEmojiPair.findOne({ const rep = await RoleEmojiPair.findOne({
where: { where: {
message: msgID, message: msgID,
emoji, emoji
} }
}); });
// Deny if unregistered // Deny if unregistered

View File

@ -4,7 +4,7 @@ export const name = Events.VoiceStateUpdate;
export async function execute(oldState, newState) { export async function execute(oldState, newState) {
console.debug('[DEBUG] Voice State Update'); console.debug('[DEBUG] Voice State Update');
const change = (!!oldState.channel) ^ (!!newState.channel); const change = !!oldState.channel ^ !!newState.channel;
if (!change) return; if (!change) return;
const guild = newState.guild.name; const guild = newState.guild.name;

View File

@ -37,13 +37,10 @@ const evtPath = join(__dirname, 'events');
getFiles(cmdPath) getFiles(cmdPath)
// For each command file // For each command file
.then(async (files) => .then(async (files) =>
(await Promise.all(files.map(importAndCheck))) (await Promise.all(files.map(importAndCheck))).filter((module) => module !== 0)
.filter((module) => module !== 0) )
).then(async (commands) => { .then(async (commands) => {
const files = await getFiles(evtPath); const files = await getFiles(evtPath);
const events = await Promise.all( const events = await Promise.all(files.map(async (filePath) => await import(filePath)));
files.map(async (filePath) =>
await import(filePath)
));
runClient(commands, events); runClient(commands, events);
}); });

View File

@ -1,10 +1,10 @@
import { DataTypes } from "sequelize"; import { DataTypes } from 'sequelize';
export default function(sequelize) { export default function (sequelize) {
return sequelize.define('Messages', { return sequelize.define('Messages', {
id: { id: {
type: DataTypes.STRING, type: DataTypes.STRING,
primaryKey: true, primaryKey: true
}, }
}); });
} }

View File

@ -1,25 +1,25 @@
import { DataTypes, Deferrable } from "sequelize"; import { DataTypes, Deferrable } from 'sequelize';
export default function(sequelize) { export default function (sequelize) {
return sequelize.define('RoleEmojiPairs', { return sequelize.define('RoleEmojiPairs', {
id: { id: {
defaultValue: DataTypes.UUIDV4, defaultValue: DataTypes.UUIDV4,
type: DataTypes.UUID, type: DataTypes.UUID,
primaryKey: true, primaryKey: true
}, },
message: { message: {
type: DataTypes.STRING, type: DataTypes.STRING,
references: { references: {
deferrable: Deferrable.INITIALLY_IMMEDIATE, deferrable: Deferrable.INITIALLY_IMMEDIATE,
model: 'Messages', model: 'Messages',
key: 'id', key: 'id'
}, }
}, },
role: { role: {
type: DataTypes.STRING, type: DataTypes.STRING
}, },
emoji: { emoji: {
type: DataTypes.STRING, type: DataTypes.STRING
} }
}); });
} }

View File

@ -1,10 +1,10 @@
import { DataTypes } from "sequelize"; import { DataTypes } from 'sequelize';
export default function(sequelize) { export default function (sequelize) {
return sequelize.define('VoiceChannel', { return sequelize.define('VoiceChannel', {
id: { id: {
type: DataTypes.STRING, type: DataTypes.STRING,
primaryKey: true, primaryKey: true
}, }
}); });
} }

202
shared.js
View File

@ -1,98 +1,104 @@
import { Op } from "sequelize"; import { Op } from 'sequelize';
import { Message, RoleEmojiPair } from "./database.js"; import { Message, RoleEmojiPair } from './database.js';
const saveMessageData = async (id, role, emoji) => { const saveMessageData = async (id, role, emoji) => {
// Try finding message // Try finding message
const msg = await Message.findOne({ where: { id } }); const msg = await Message.findOne({ where: { id } });
if (msg === null) throw new Error(`No message with ID '${id}' could be found!`); if (msg === null) throw new Error(`No message with ID '${id}' could be found!`);
// Try finding existing entry // Try finding existing entry
const rep = await RoleEmojiPair.findOne({ const rep = await RoleEmojiPair.findOne({
where: { where: {
[Op.or]: [ [Op.or]: [
{ {
message: id, message: id,
role: role.id role: role.id
}, { },
message: id, {
emoji message: id,
} emoji
] }
} ]
}); }
if (rep !== null) throw new Error(`Existing RoleEmojiPair entry with (partial) data {message:${id},role:${role.id},emoji:${emoji}}!`); });
if (rep !== null)
// Create database entry for pair throw new Error(
await RoleEmojiPair.create({ message: id, role: role.id, emoji }); `Existing RoleEmojiPair entry with (partial) data {message:${id},role:${role.id},emoji:${emoji}}!`
}; );
export const addSelfRoles = async (interaction, msgID, role, emoji) => { // Create database entry for pair
const { channel } = interaction; await RoleEmojiPair.create({ message: id, role: role.id, emoji });
};
let step = 'fetch';
try { export const addSelfRoles = async (interaction, msgID, role, emoji) => {
// Get message by id const { channel } = interaction;
const message = await channel.messages.fetch(msgID);
let step = 'fetch';
step = 'save data from'; try {
await saveMessageData(msgID, role, emoji); // Get message by id
const message = await channel.messages.fetch(msgID);
step = 'react to';
// React with emoji to message step = 'save data from';
await message.react(emoji); await saveMessageData(msgID, role, emoji);
// Reply successfully to acknowledge command step = 'react to';
await interaction.reply({ // React with emoji to message
content: 'Added new entry for self roles!', await message.react(emoji);
ephemeral: true,
}); // Reply successfully to acknowledge command
await interaction.reply({
console.info(`[INFO] Added new entry to get role with ID '${role.id}' using '${emoji}'.`); content: 'Added new entry for self roles!',
} catch (error) { ephemeral: true
console.error(error); });
// Reply failed to acknowledge command console.info(`[INFO] Added new entry to get role with ID '${role.id}' using '${emoji}'.`);
await interaction.reply({ } catch (error) {
content: `Failed to ${step} message!`, console.error(error);
ephemeral: true,
}); // Reply failed to acknowledge command
} await interaction.reply({
} content: `Failed to ${step} message!`,
import { join } from 'path'; ephemeral: true
import { readdir } from 'fs/promises'; });
}
const required = ['data', 'execute']; };
const optional = ['autocomplete', 'modalSubmit']; import { join } from 'path';
import { readdir } from 'fs/promises';
export const getFiles = async (dir) => {
const dirents = await readdir(dir, { withFileTypes: true }); const required = ['data', 'execute'];
const files = await Promise.all(dirents.map((dirent) => { const optional = ['autocomplete', 'modalSubmit'];
const res = join(dir, dirent.name);
return dirent.isDirectory() ? getFiles(res) : res; export const getFiles = async (dir) => {
})); const dirents = await readdir(dir, { withFileTypes: true });
return Array.prototype.concat(...files); const files = await Promise.all(
}; dirents.map((dirent) => {
const res = join(dir, dirent.name);
export const importAndCheck = async (filePath) => { return dirent.isDirectory() ? getFiles(res) : res;
if (!filePath.endsWith('.js') || filePath.endsWith('.example.js')) { })
// Skip this file );
return 0; return Array.prototype.concat(...files);
} };
const command = await import(filePath);
// Warn incomplete commands export const importAndCheck = async (filePath) => {
if (!required.every((name) => name in command)) { if (!filePath.endsWith('.js') || filePath.endsWith('.example.js')) {
console.error( // Skip this file
`[ERROR] The command at ${filePath} is missing a required "data" or "execute" property.` return 0;
); }
return 0; const command = await import(filePath);
} // Warn incomplete commands
const properties = optional.filter((name) => !(name in command)); if (!required.every((name) => name in command)) {
if (properties.length > 0) console.error(
properties.forEach((name) => `[ERROR] The command at ${filePath} is missing a required "data" or "execute" property.`
console.warn( );
`[WARNING] The command at ${filePath} is missing an optional "${name}" property.` return 0;
) }
); const properties = optional.filter((name) => !(name in command));
// Add command to collection if (properties.length > 0)
return command; properties.forEach((name) =>
}; console.warn(
`[WARNING] The command at ${filePath} is missing an optional "${name}" property.`
)
);
// Add command to collection
return command;
};