132 lines
5.2 KiB
JavaScript
132 lines
5.2 KiB
JavaScript
const { InteractionType } = require('discord.js');
|
|
const { UserUtils } = require('../util');
|
|
const { Card, Character, User, Band } = require('../models');
|
|
const Sequelize = require('sequelize');
|
|
const { QUALITY_NAMES } = require('../config/constants');
|
|
const { TestStore } = require('../stores');
|
|
module.exports = {
|
|
name: "interactionCreate",
|
|
async execute (interaction) {
|
|
let isRegistered = await UserUtils.registrationCheck(interaction);
|
|
if (!isRegistered) return;
|
|
if (interaction.type !== InteractionType.ApplicationCommandAutocomplete) return;
|
|
console.log(`Autocomplete request from ${interaction.user.tag} (${interaction.user.id}) for ${interaction.commandName} with ${interaction.options.getFocused(true).value}`);
|
|
|
|
let user = await UserUtils.getUserByDiscordId(interaction.member.id);
|
|
let focusedOption = interaction.options.getFocused(true);
|
|
let choices = [];
|
|
|
|
if (interaction.commandName === "burn") {
|
|
choices = await this.fetchCards(focusedOption, { user: user, ownedOnly: true });
|
|
}
|
|
|
|
if (interaction.commandName === "trade") {
|
|
if (focusedOption.name === "card") {
|
|
choices = await this.fetchCards(focusedOption, { user: user, ownedOnly: true });
|
|
}
|
|
}
|
|
|
|
if (interaction.commandName === 'view') {
|
|
const viewType = interaction.options.getString('type');
|
|
|
|
switch (viewType) {
|
|
case 'card':
|
|
choices = await this.fetchCards(focusedOption, { user: user});
|
|
case 'character':
|
|
if(focusedOption.value.length < 3) break;
|
|
const characters = await Character.findAll({
|
|
where: {
|
|
name: {
|
|
[Sequelize.Op.like]: `%${focusedOption.value}%`
|
|
}
|
|
},
|
|
limit: 10
|
|
});
|
|
for (let i = 0; i < characters.length; i++) {
|
|
choices.push({
|
|
name: characters[i].name,
|
|
value: `${characters[i].id}`
|
|
});
|
|
}
|
|
break;
|
|
case 'band':
|
|
break;
|
|
}
|
|
}
|
|
if (interaction.commandName === 'collection') {
|
|
const character = interaction.options.getString('character');
|
|
const band = interaction.options.getString('band');
|
|
const quality = interaction.options.getString('quality');
|
|
//TODO: avoid duplicate code hehe
|
|
switch (focusedOption.name) {
|
|
case 'character':
|
|
if(focusedOption.value.length < 3) break;
|
|
const characters = await Character.findAll({
|
|
where: {
|
|
name: {
|
|
[Sequelize.Op.like]: `%${focusedOption.value}%`
|
|
}
|
|
},
|
|
limit: 10
|
|
});
|
|
for (let i = 0; i < characters.length; i++) {
|
|
choices.push({
|
|
name: characters[i].name,
|
|
value: `${characters[i].id}`
|
|
});
|
|
}
|
|
break;
|
|
case 'band':
|
|
if(focusedOption.value.length < 3) break;
|
|
const bands = await Band.findAll({
|
|
where: {
|
|
name: {
|
|
[Sequelize.Op.like]: `%${focusedOption.value}%`
|
|
}
|
|
},
|
|
limit: 10
|
|
});
|
|
for (let i = 0; i < bands.length; i++) {
|
|
choices.push({
|
|
name: bands[i].name,
|
|
value: `${bands[i].id}`
|
|
});
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
if (choices.length > 0) {
|
|
console.log(choices);
|
|
await interaction.respond(choices);
|
|
}
|
|
return;
|
|
},
|
|
async fetchCards (focusedOption, options={}) {
|
|
let choices = [];
|
|
let condition = {
|
|
where: {
|
|
identifier: {
|
|
[Sequelize.Op.like]: `%${focusedOption.value}%`
|
|
},
|
|
burned: false
|
|
},
|
|
include: [{ model: Character }, { model: User }],
|
|
limit: 10
|
|
}
|
|
if (options.ownedOnly) {
|
|
condition.where.userId = { [Sequelize.Op.eq]: options.user.id };
|
|
}
|
|
const cards = await Card.findAll(condition);
|
|
for (let i = 0; i < cards.length; i++) {
|
|
let owned = "";
|
|
if (options.user) {
|
|
owned = cards[i].userId === options.user.id ? " (owned)" : "";
|
|
}
|
|
choices.push({
|
|
name: `${cards[i].identifier} - ${cards[i].Character.name} (${QUALITY_NAMES[cards[i].quality]})${owned}`,
|
|
value: cards[i].identifier
|
|
});
|
|
}
|
|
return choices;
|
|
}
|
|
} |