Finish "new" command, and other stuff

This commit is contained in:
Isaac
2021-04-05 18:38:00 +01:00
parent 01519df0cf
commit 4b3ba238bd
15 changed files with 369 additions and 255 deletions

33
src/commands/close.js Normal file
View File

@@ -0,0 +1,33 @@
const { MessageEmbed } = require('discord.js');
const Command = require('../modules/commands/command');
const { footer } = require('../utils/discord');
module.exports = class CloseCommand extends Command {
constructor(client) {
const i18n = client.i18n.get(client.config.locale);
super(client, {
internal: true,
name: i18n('commands.close.name'),
description: i18n('commands.close.description'),
aliases: [
i18n('commands.close.aliases.delete'),
],
process_args: false,
args: [
{
name: i18n('commands.close.args.ticket.name'),
description: i18n('commands.close.args.ticket.description'),
example: i18n('commands.close.args.ticket.example'),
required: false,
}
]
});
}
async execute(message, args) {
let settings = await message.guild.settings;
const i18n = this.client.i18n.get(settings.locale);
}
};

View File

@@ -1,5 +1,7 @@
const { MessageEmbed } = require('discord.js');
const Command = require('../modules/commands/command');
const { footer } = require('../utils/discord');
const { letters } = require('../utils/emoji');
module.exports = class NewCommand extends Command {
constructor(client) {
@@ -29,27 +31,131 @@ module.exports = class NewCommand extends Command {
let settings = await message.guild.settings;
const i18n = this.client.i18n.get(settings.locale);
let { count: cat_count, rows: categories } = await this.client.db.models.Category.findAndCountAll({
const editOrSend = async (msg, content) => {
if (msg) return await msg.edit(content);
else return await message.channel.send(content);
};
const create = async (cat_row, response) => {
let tickets = await this.client.db.models.Ticket.findAndCountAll({
where: {
category: cat_row.id,
creator: message.author.id,
open: true
}
});
if (tickets.count >= cat_row.max_per_member) {
if (cat_row.max_per_member === 1) {
response = await editOrSend(response,
new MessageEmbed()
.setColor(settings.error_colour)
.setAuthor(message.author.username, message.author.displayAvatarURL())
.setTitle(i18n('commands.new.response.has_a_ticket.title'))
.setDescription(i18n('commands.new.response.has_a_ticket.description', tickets.rows[0].id))
.setFooter(footer(settings.footer, i18n('message_will_be_deleted_in', 15)), message.guild.iconURL())
);
} else {
let list = tickets.rows.map(row => {
if (row.topic) {
let description = row.topic.substring(0, 30);
let ellipses = description.length > 30 ? '...' : '';
return `<#${row.id}>: \`${description}${ellipses}\``;
} else {
return `<#${row.id}>`;
}
});
response = await editOrSend(response,
new MessageEmbed()
.setColor(settings.error_colour)
.setAuthor(message.author.username, message.author.displayAvatarURL())
.setTitle(i18n('commands.new.response.max_tickets.title', tickets.count))
.setDescription(i18n('commands.new.response.max_tickets.description', settings.command_prefix, list.join('\n')))
.setFooter(footer(settings.footer, i18n('message_will_be_deleted_in', 15)), message.guild.iconURL())
);
}
} else {
let t_row = await this.client.tickets.create(message.guild.id, message.author.id, cat_row.id, args);
response = await editOrSend(response,
new MessageEmbed()
.setColor(settings.success_colour)
.setAuthor(message.author.username, message.author.displayAvatarURL())
.setTitle(i18n('commands.new.response.created.title'))
.setDescription(i18n('commands.new.response.created.description', `<#${t_row.id}>`))
.setFooter(footer(settings.footer, i18n('message_will_be_deleted_in', 15)), message.guild.iconURL())
);
}
setTimeout(async () => {
await response.delete();
await message.delete();
}, 15000);
};
let categories = await this.client.db.models.Category.findAndCountAll({
where: {
guild: message.guild.id
}
});
switch (cat_count) {
case 0:
if (categories.count === 0) {
return await message.channel.send(
new MessageEmbed()
.setColor(settings.error_colour)
.setAuthor(message.author.username, message.author.displayAvatarURL())
.setTitle(i18n('commands.new.response.no_categories.title'))
.setDescription(i18n('commands.new.response.no_categories.description'))
.setFooter(settings.footer, message.guild.iconURL())
);
case 1:
break;
default:
} else if (categories.count === 1) {
create(categories.rows[0]);
} else {
let letters_array = Object.values(letters);
let category_list = categories.rows.map((category, i) => `${letters_array[i]} » ${category.name}`);
let collector_message = await message.channel.send(
new MessageEmbed()
.setColor(settings.colour)
.setAuthor(message.author.username, message.author.displayAvatarURL())
.setTitle(i18n('commands.new.response.select_category.title'))
.setDescription(i18n('commands.new.response.select_category.description', category_list.join('\n')))
.setFooter(footer(settings.footer, i18n('collector_expires_in', 30)), message.guild.iconURL())
);
for (let i in categories.rows) {
await collector_message.react(letters_array[i]);
}
const collector_filter = (reaction, user) => {
let allowed = letters_array.slice(0, categories.count);
return user.id === message.author.id && allowed.includes(reaction.emoji.name);
};
let collector = collector_message.createReactionCollector(collector_filter, {
time: 30000
});
collector.on('collect', async (reaction) => {
let index = letters_array.findIndex(value => value === reaction.emoji.name);
if (index === -1) return await collector_message.delete({ timeout: 15000 });
await collector_message.reactions.removeAll();
create(categories.rows[index], collector_message);
});
collector.on('end', async (collected) => {
if (collected.size === 0) {
await collector_message.reactions.removeAll();
await collector_message.edit(
new MessageEmbed()
.setColor(settings.error_colour)
.setAuthor(message.author.username, message.author.displayAvatarURL())
.setTitle(i18n('commands.new.response.select_category_timeout.title'))
.setDescription(i18n('commands.new.response.select_category_timeout.description', category_list.join('\n')))
.setFooter(footer(settings.footer, i18n('message_will_be_deleted_in', 15)), message.guild.iconURL())
);
collector_message.delete({ timeout: 15000 });
}
});
}
// this.client.tickets.create(message.guild.id, message.member.id, '825861413687787560', args.topic);
}
};

View File

@@ -31,6 +31,7 @@ module.exports = class SettingsCommand extends Command {
settings.colour = data.colour;
settings.command_prefix = data.command_prefix;
settings.error_colour = data.error_colour;
settings.footer = data.footer;
settings.locale = data.locale;
settings.log_messages = data.log_messages;
settings.success_colour = data.success_colour;
@@ -114,6 +115,7 @@ module.exports = class SettingsCommand extends Command {
colour: settings.colour,
command_prefix: settings.command_prefix,
error_colour: settings.error_colour,
footer: settings.footer,
locale: settings.locale,
log_messages: settings.log_messages,
success_colour: settings.success_colour,