|
|
|
@ -115,34 +115,67 @@ async function addResponse(interaction) {
|
|
|
|
|
await interaction.showModal(modal);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @param {ChatInputCommandInteraction} interaction */
|
|
|
|
|
async function removeKeyword(interaction) {
|
|
|
|
|
const { options } = interaction;
|
|
|
|
|
|
|
|
|
|
// Get command options
|
|
|
|
|
const keyword = options.getString('name');
|
|
|
|
|
|
|
|
|
|
// Try deleting keyword from database
|
|
|
|
|
await Keywords.destroy({
|
|
|
|
|
where: {
|
|
|
|
|
guild: interaction.guildId,
|
|
|
|
|
name: keyword
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Reply with success
|
|
|
|
|
await interaction.reply({
|
|
|
|
|
content: `Keyword '${keyword}' was successfully deleted!`,
|
|
|
|
|
ephemeral: true
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @param {ChatInputCommandInteraction} interaction */
|
|
|
|
|
async function removeResponse(interaction) {
|
|
|
|
|
const { options } = interaction;
|
|
|
|
|
|
|
|
|
|
// Get command options
|
|
|
|
|
/** @type {'keyword'|'response'} */
|
|
|
|
|
const type = options.getString('type');
|
|
|
|
|
const keyword = options.getString('keyword');
|
|
|
|
|
const name = options.getString('name');
|
|
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
|
case 'keyword':
|
|
|
|
|
// Try removing keyword
|
|
|
|
|
await Keywords.destroy({
|
|
|
|
|
where: {
|
|
|
|
|
guild: interaction.guildId,
|
|
|
|
|
name
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
break;
|
|
|
|
|
case 'response':
|
|
|
|
|
// Try removing response
|
|
|
|
|
await Responses.destroy({
|
|
|
|
|
where: {
|
|
|
|
|
guild: interaction.guildId,
|
|
|
|
|
name
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
// Find keyword in database
|
|
|
|
|
/** @type {import('../../models/keywords.js').Keyword|null} */
|
|
|
|
|
const found = await Keywords.findOne({
|
|
|
|
|
where: {
|
|
|
|
|
guild: interaction.guildId,
|
|
|
|
|
name: keyword
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Abort if keyword not found
|
|
|
|
|
if (found === null) {
|
|
|
|
|
await interaction.reply({
|
|
|
|
|
content: 'Unknown keyword was specified!',
|
|
|
|
|
ephemeral: true
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Try deleting response from database
|
|
|
|
|
await Responses.destroy({
|
|
|
|
|
where: {
|
|
|
|
|
keyword: found.id,
|
|
|
|
|
name
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Reply with success
|
|
|
|
|
await interaction.reply({
|
|
|
|
|
content: `Response with name '${name}' was successfully deleted!`,
|
|
|
|
|
ephemeral: true
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @param {ChatInputCommandInteraction} interaction */
|
|
|
|
@ -175,11 +208,62 @@ async function listResponse(interaction) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @param {ChatInputCommandInteraction} interaction */
|
|
|
|
|
async function infoResponse(interaction) {
|
|
|
|
|
async function responseInfos(interaction) {
|
|
|
|
|
const { options } = interaction;
|
|
|
|
|
|
|
|
|
|
// Get command options
|
|
|
|
|
const keyword = options.getString('keyword');
|
|
|
|
|
const name = options.getString('name');
|
|
|
|
|
|
|
|
|
|
// Find keyword in database
|
|
|
|
|
/** @type {import('../../models/keywords.js').Keyword|null} */
|
|
|
|
|
const found = await Keywords.findOne({
|
|
|
|
|
where: {
|
|
|
|
|
guild: interaction.guildId,
|
|
|
|
|
name: keyword
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Abort if keyword not found
|
|
|
|
|
if (found === null) {
|
|
|
|
|
await interaction.reply({
|
|
|
|
|
content: 'Unknown keyword was specified!',
|
|
|
|
|
ephemeral: true
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find response in database
|
|
|
|
|
/** @type {import('../../models/responses.js').Response|null} */
|
|
|
|
|
const response = await Responses.findOne({
|
|
|
|
|
where: {
|
|
|
|
|
keyword: found.id,
|
|
|
|
|
name
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Abort if response not found
|
|
|
|
|
if (response === null) {
|
|
|
|
|
await interaction.reply({
|
|
|
|
|
content: 'Unknown response was specified!',
|
|
|
|
|
ephemeral: true
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Reply with success
|
|
|
|
|
await interaction.reply({
|
|
|
|
|
content: `Response with name '${name}' has data of \`${response.response}\`!`,
|
|
|
|
|
ephemeral: true
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @param {ChatInputCommandInteraction} interaction */
|
|
|
|
|
async function keywordInfos(interaction) {
|
|
|
|
|
const { options } = interaction;
|
|
|
|
|
|
|
|
|
|
// Get command options
|
|
|
|
|
const keyword = options.getString('name');
|
|
|
|
|
|
|
|
|
|
// Find keyword in database
|
|
|
|
|
/** @type {import('../../models/keywords.js').Keyword|null} */
|
|
|
|
@ -203,13 +287,21 @@ async function infoResponse(interaction) {
|
|
|
|
|
/** @type {import('../../models/responses.js').Response[]} */
|
|
|
|
|
const responses = await Responses.findAll({
|
|
|
|
|
where: {
|
|
|
|
|
guild: interaction.guildId,
|
|
|
|
|
name: found.id
|
|
|
|
|
keyword: found.id
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Abort if no responses registered
|
|
|
|
|
if (responses.length === 0) {
|
|
|
|
|
await interaction.reply({
|
|
|
|
|
content: 'No responses have been registered yet!',
|
|
|
|
|
ephemeral: true
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Join list of responses
|
|
|
|
|
const joined = responses.map((response) => response.response).join('\n- ');
|
|
|
|
|
const joined = responses.map((response) => response.name).join('\n- ');
|
|
|
|
|
|
|
|
|
|
// Reply with list of responses
|
|
|
|
|
await interaction.reply({
|
|
|
|
@ -218,11 +310,13 @@ async function infoResponse(interaction) {
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param {string} guildId
|
|
|
|
|
* @param {string} focused
|
|
|
|
|
*/
|
|
|
|
|
async function keywordAutocomplete(guildId, focused) {
|
|
|
|
|
/** @param {AutocompleteInteraction} interaction */
|
|
|
|
|
async function keywordAutocomplete(interaction, focused) {
|
|
|
|
|
const { options, guildId } = interaction;
|
|
|
|
|
|
|
|
|
|
// Get command options
|
|
|
|
|
if (!focused) focused = options.getFocused();
|
|
|
|
|
|
|
|
|
|
// Get list of keywords from database
|
|
|
|
|
/** @type {import('../../models/keywords.js').Keyword[]} */
|
|
|
|
|
const keywords = await Keywords.findAll({
|
|
|
|
@ -239,11 +333,15 @@ async function keywordAutocomplete(guildId, focused) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param {string} guildId
|
|
|
|
|
* @param {AutocompleteInteraction} interaction
|
|
|
|
|
* @param {string} focused
|
|
|
|
|
* @param {string} keyword
|
|
|
|
|
*/
|
|
|
|
|
async function responseAutocomplete(guildId, focused, keyword) {
|
|
|
|
|
async function completeResponses(interaction, focused) {
|
|
|
|
|
const { options, guildId } = interaction;
|
|
|
|
|
|
|
|
|
|
// Get command options
|
|
|
|
|
const keyword = options.getString('keyword');
|
|
|
|
|
|
|
|
|
|
// Get keyword
|
|
|
|
|
/** @type {import('../../models/keywords.js').Keyword} */
|
|
|
|
|
const found = await Keywords.findOne({
|
|
|
|
@ -260,7 +358,6 @@ async function responseAutocomplete(guildId, focused, keyword) {
|
|
|
|
|
? []
|
|
|
|
|
: await Responses.findAll({
|
|
|
|
|
where: {
|
|
|
|
|
guild: guildId,
|
|
|
|
|
keyword: found.id
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
@ -269,27 +366,22 @@ async function responseAutocomplete(guildId, focused, keyword) {
|
|
|
|
|
const filtered = responses.filter((choice) => choice.name.startsWith(focused));
|
|
|
|
|
|
|
|
|
|
// Respond with possible suggestions
|
|
|
|
|
await interaction.respond(
|
|
|
|
|
filtered.map((choice) => ({ name: choice.name, value: choice.response }))
|
|
|
|
|
);
|
|
|
|
|
await interaction.respond(filtered.map((choice) => ({ name: choice.name, value: choice.name })));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @param {AutocompleteInteraction} interaction */
|
|
|
|
|
async function removeAutocomplete(interaction) {
|
|
|
|
|
async function responseAutocomplete(interaction) {
|
|
|
|
|
const { options } = interaction;
|
|
|
|
|
|
|
|
|
|
const type = options.getString('type');
|
|
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
|
// Get command options
|
|
|
|
|
const focused = options.getFocused(true);
|
|
|
|
|
const { name, value } = focused;
|
|
|
|
|
switch (name) {
|
|
|
|
|
case 'keyword':
|
|
|
|
|
await keywordAutocomplete(interaction.guildId, options.getFocused());
|
|
|
|
|
keywordAutocomplete(interaction, value);
|
|
|
|
|
break;
|
|
|
|
|
case 'response':
|
|
|
|
|
await responseAutocomplete(
|
|
|
|
|
interaction.guildId,
|
|
|
|
|
options.getFocused(),
|
|
|
|
|
options.getString('keyword')
|
|
|
|
|
);
|
|
|
|
|
case 'name':
|
|
|
|
|
completeResponses(interaction, value);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -322,41 +414,79 @@ export const data = new SlashCommandBuilder()
|
|
|
|
|
.setRequired(true)
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
.addSubcommand((subcommand) =>
|
|
|
|
|
subcommand
|
|
|
|
|
.addSubcommandGroup((group) =>
|
|
|
|
|
group
|
|
|
|
|
.setName('remove')
|
|
|
|
|
.setDescription('Unregisters a response to a keyword.')
|
|
|
|
|
.addStringOption((option) =>
|
|
|
|
|
option
|
|
|
|
|
.setName('type')
|
|
|
|
|
.setDescription('The type of data to be removed.')
|
|
|
|
|
.setRequired(true)
|
|
|
|
|
.addChoices(
|
|
|
|
|
{ name: 'Keyword', value: 'keyword' },
|
|
|
|
|
{ name: 'Response', value: 'response' }
|
|
|
|
|
.setDescription('Unregisters a response or a keyword.')
|
|
|
|
|
.addSubcommand((subcommand) =>
|
|
|
|
|
subcommand
|
|
|
|
|
.setName('keyword')
|
|
|
|
|
.setDescription('Deletes a keyword completely.')
|
|
|
|
|
.addStringOption((option) =>
|
|
|
|
|
option
|
|
|
|
|
.setName('name')
|
|
|
|
|
.setDescription('The keyword to be deleted.')
|
|
|
|
|
.setAutocomplete(true)
|
|
|
|
|
.setRequired(true)
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
.addStringOption((option) =>
|
|
|
|
|
option
|
|
|
|
|
.setName('name')
|
|
|
|
|
.setDescription('The name of the data to be removed.')
|
|
|
|
|
.setAutocomplete(true)
|
|
|
|
|
.setRequired(true)
|
|
|
|
|
.addSubcommand((subcommand) =>
|
|
|
|
|
subcommand
|
|
|
|
|
.setName('response')
|
|
|
|
|
.setDescription('Unregisters a response of a keyword.')
|
|
|
|
|
.addStringOption((option) =>
|
|
|
|
|
option
|
|
|
|
|
.setName('keyword')
|
|
|
|
|
.setDescription('The keyword that would trigger the response.')
|
|
|
|
|
.setAutocomplete(true)
|
|
|
|
|
.setRequired(true)
|
|
|
|
|
)
|
|
|
|
|
.addStringOption((option) =>
|
|
|
|
|
option
|
|
|
|
|
.setName('name')
|
|
|
|
|
.setDescription('The name of the data to be removed.')
|
|
|
|
|
.setAutocomplete(true)
|
|
|
|
|
.setRequired(true)
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
.addSubcommand((subcommand) =>
|
|
|
|
|
subcommand.setName('list').setDescription('Lists all registered keywords.')
|
|
|
|
|
)
|
|
|
|
|
.addSubcommand((subcommand) =>
|
|
|
|
|
subcommand
|
|
|
|
|
.addSubcommandGroup((group) =>
|
|
|
|
|
group
|
|
|
|
|
.setName('info')
|
|
|
|
|
.setDescription('Shows responses of a registered keyword.')
|
|
|
|
|
.addStringOption((option) =>
|
|
|
|
|
option
|
|
|
|
|
.setDescription('Lists information about a response or a keyword.')
|
|
|
|
|
.addSubcommand((subcommand) =>
|
|
|
|
|
subcommand
|
|
|
|
|
.setName('keyword')
|
|
|
|
|
.setDescription('The keyword to show the details of.')
|
|
|
|
|
.setAutocomplete(true)
|
|
|
|
|
.setRequired(true)
|
|
|
|
|
.setDescription('Lists registered responses of a keyword.')
|
|
|
|
|
.addStringOption((option) =>
|
|
|
|
|
option
|
|
|
|
|
.setName('name')
|
|
|
|
|
.setDescription('The keyword to be shown the details of.')
|
|
|
|
|
.setAutocomplete(true)
|
|
|
|
|
.setRequired(true)
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
.addSubcommand((subcommand) =>
|
|
|
|
|
subcommand
|
|
|
|
|
.setName('response')
|
|
|
|
|
.setDescription('Lists the data being sent by a response.')
|
|
|
|
|
.addStringOption((option) =>
|
|
|
|
|
option
|
|
|
|
|
.setName('keyword')
|
|
|
|
|
.setDescription('The keyword that would trigger the response.')
|
|
|
|
|
.setAutocomplete(true)
|
|
|
|
|
.setRequired(true)
|
|
|
|
|
)
|
|
|
|
|
.addStringOption((option) =>
|
|
|
|
|
option
|
|
|
|
|
.setName('name')
|
|
|
|
|
.setDescription('The name of the data to be listed.')
|
|
|
|
|
.setAutocomplete(true)
|
|
|
|
|
.setRequired(true)
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
/** @param {ModalSubmitInteraction} interaction */
|
|
|
|
@ -364,9 +494,9 @@ export async function modalSubmit(interaction) {
|
|
|
|
|
const { fields } = interaction;
|
|
|
|
|
|
|
|
|
|
// Get text inputs from modal
|
|
|
|
|
const name = fields.getTextInputValue('name');
|
|
|
|
|
const keyword = fields.getTextInputValue('keyword');
|
|
|
|
|
const response = fields.getTextInputValue('response');
|
|
|
|
|
const name = fields.getTextInputValue('name').toLowerCase();
|
|
|
|
|
|
|
|
|
|
// Get id of keyword
|
|
|
|
|
/** @type {import('../../models/keywords.js').Keyword} */
|
|
|
|
@ -376,20 +506,50 @@ export async function modalSubmit(interaction) {
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Abort if response exists
|
|
|
|
|
if (
|
|
|
|
|
(await Responses.findOne({
|
|
|
|
|
where: {
|
|
|
|
|
keyword: found.id,
|
|
|
|
|
name
|
|
|
|
|
}
|
|
|
|
|
})) !== null
|
|
|
|
|
) {
|
|
|
|
|
await interaction.reply({
|
|
|
|
|
content: `Response with name '${name}' already exists!`,
|
|
|
|
|
ephemeral: true
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create new response data with keyword attached
|
|
|
|
|
await Responses.create({ keyword: found.id, name, response });
|
|
|
|
|
|
|
|
|
|
// Reply with success
|
|
|
|
|
await interaction.reply({
|
|
|
|
|
content: `Successfully registered '${name}' as response to '${keyword}'!`,
|
|
|
|
|
ephemeral: true
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
/** @param {AutocompleteInteraction} interaction */
|
|
|
|
|
export async function autocomplete(interaction) {
|
|
|
|
|
const { options } = interaction;
|
|
|
|
|
|
|
|
|
|
switch (options.getSubcommand()) {
|
|
|
|
|
case 'remove':
|
|
|
|
|
removeAutocomplete(interaction);
|
|
|
|
|
const command = options.getSubcommand();
|
|
|
|
|
const group = options.getSubcommandGroup();
|
|
|
|
|
const joined = group === null ? command : `${group} ${command}`;
|
|
|
|
|
|
|
|
|
|
switch (joined) {
|
|
|
|
|
case 'info keyword':
|
|
|
|
|
case 'remove keyword':
|
|
|
|
|
await keywordAutocomplete(interaction);
|
|
|
|
|
break;
|
|
|
|
|
case 'info response':
|
|
|
|
|
case 'remove response':
|
|
|
|
|
await responseAutocomplete(interaction);
|
|
|
|
|
break;
|
|
|
|
|
case 'add':
|
|
|
|
|
case 'info':
|
|
|
|
|
keywordAutocomplete(interaction.guildId, interaction.options.getFocused());
|
|
|
|
|
keywordAutocomplete(interaction);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -397,21 +557,31 @@ export async function autocomplete(interaction) {
|
|
|
|
|
export async function execute(interaction) {
|
|
|
|
|
const { options } = interaction;
|
|
|
|
|
|
|
|
|
|
switch (options.getSubcommand()) {
|
|
|
|
|
const command = options.getSubcommand();
|
|
|
|
|
const group = options.getSubcommandGroup();
|
|
|
|
|
const joined = group === null ? command : `${group} ${command}`;
|
|
|
|
|
|
|
|
|
|
switch (joined) {
|
|
|
|
|
case 'create':
|
|
|
|
|
createResponse(interaction);
|
|
|
|
|
break;
|
|
|
|
|
case 'add':
|
|
|
|
|
addResponse(interaction);
|
|
|
|
|
break;
|
|
|
|
|
case 'remove':
|
|
|
|
|
case 'remove keyword':
|
|
|
|
|
removeKeyword(interaction);
|
|
|
|
|
break;
|
|
|
|
|
case 'remove response':
|
|
|
|
|
removeResponse(interaction);
|
|
|
|
|
break;
|
|
|
|
|
case 'list':
|
|
|
|
|
listResponse(interaction);
|
|
|
|
|
break;
|
|
|
|
|
case 'info':
|
|
|
|
|
infoResponse(interaction);
|
|
|
|
|
case 'info keyword':
|
|
|
|
|
keywordInfos(interaction);
|
|
|
|
|
break;
|
|
|
|
|
case 'info response':
|
|
|
|
|
responseInfos(interaction);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|