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

@ -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');
@ -117,6 +120,7 @@ export async function execute(interaction) {
await addSelfRoles(interaction, msgID, role, emoji); await addSelfRoles(interaction, msgID, role, emoji);
break; break;
} }
}
if (createNew) { if (createNew) {
try { try {
@ -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

@ -9,7 +9,7 @@ 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);

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
}, }
}); });
} }

View File

@ -1,5 +1,5 @@
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
@ -13,14 +13,18 @@ const saveMessageData = async (id, role, emoji) => {
{ {
message: id, message: id,
role: role.id role: role.id
}, { },
{
message: id, message: id,
emoji emoji
} }
] ]
} }
}); });
if (rep !== null) throw new Error(`Existing RoleEmojiPair entry with (partial) data {message:${id},role:${role.id},emoji:${emoji}}!`); if (rep !== null)
throw new Error(
`Existing RoleEmojiPair entry with (partial) data {message:${id},role:${role.id},emoji:${emoji}}!`
);
// Create database entry for pair // Create database entry for pair
await RoleEmojiPair.create({ message: id, role: role.id, emoji }); await RoleEmojiPair.create({ message: id, role: role.id, emoji });
@ -44,7 +48,7 @@ export const addSelfRoles = async (interaction, msgID, role, emoji) => {
// Reply successfully to acknowledge command // Reply successfully to acknowledge command
await interaction.reply({ await interaction.reply({
content: 'Added new entry for self roles!', content: 'Added new entry for self roles!',
ephemeral: true, ephemeral: true
}); });
console.info(`[INFO] Added new entry to get role with ID '${role.id}' using '${emoji}'.`); console.info(`[INFO] Added new entry to get role with ID '${role.id}' using '${emoji}'.`);
@ -54,10 +58,10 @@ export const addSelfRoles = async (interaction, msgID, role, emoji) => {
// Reply failed to acknowledge command // Reply failed to acknowledge command
await interaction.reply({ await interaction.reply({
content: `Failed to ${step} message!`, content: `Failed to ${step} message!`,
ephemeral: true, ephemeral: true
}); });
} }
} };
import { join } from 'path'; import { join } from 'path';
import { readdir } from 'fs/promises'; import { readdir } from 'fs/promises';
@ -66,10 +70,12 @@ const optional = ['autocomplete', 'modalSubmit'];
export const getFiles = async (dir) => { export const getFiles = async (dir) => {
const dirents = await readdir(dir, { withFileTypes: true }); const dirents = await readdir(dir, { withFileTypes: true });
const files = await Promise.all(dirents.map((dirent) => { const files = await Promise.all(
dirents.map((dirent) => {
const res = join(dir, dirent.name); const res = join(dir, dirent.name);
return dirent.isDirectory() ? getFiles(res) : res; return dirent.isDirectory() ? getFiles(res) : res;
})); })
);
return Array.prototype.concat(...files); return Array.prototype.concat(...files);
}; };