mirror of
https://github.com/citra-emu/discord-bot.git
synced 2025-01-28 23:40:59 +00:00
Update specs.ts
Move the EndListener logic to it's own function Improve comments Fix a bug where the buttons wouldn't disable along with the input handler after mtime if they weren't interacted with
This commit is contained in:
parent
d164dbf8ba
commit
9d06b6b5e4
|
@ -1,14 +1,14 @@
|
|||
import { ActionRowBuilder, ButtonBuilder, ButtonStyle, Client, EmbedBuilder, Interaction, Message, MessageActionRow, MessageButton, MessageEmbed } from 'discord.js';
|
||||
import { ActionRowBuilder, ButtonBuilder, ButtonStyle, Client, EmbedBuilder, Interaction, Message, MessageActionRow, MessageButton, MessageEmbed, sentMessage } from 'discord.js';
|
||||
import { search as fakeapi } from 'gsmarena-api';
|
||||
|
||||
// Variables you might want to change:
|
||||
// allowed roles
|
||||
// Allowed roles:
|
||||
export const roles = ["@everyone"];
|
||||
// how long before previous messages can no longer be interacted with. default is 200000 (200 seconds)
|
||||
// How long before previous messages can no longer be interacted with. default is 200000 (200 seconds)
|
||||
const mtime = 200000
|
||||
|
||||
|
||||
// button config
|
||||
// Button config:
|
||||
const next = new ButtonBuilder()
|
||||
.setCustomId('next')
|
||||
.setLabel('Next Result >')
|
||||
|
@ -22,7 +22,7 @@ const previous = new ButtonBuilder()
|
|||
const row = new ActionRowBuilder()
|
||||
.addComponents(previous, next);
|
||||
|
||||
// function to create embed with current device information
|
||||
// Create embed with current device information.
|
||||
function createEmbed(device: any, searchQuery: string, results: number, index: number, devices: array) {
|
||||
let name = device.name;
|
||||
let chipset = device.description.match(/(?:display,)(.*)(?:chipset)/)?.[1].trim() || 'Unknown'; // parsing json data
|
||||
|
@ -32,15 +32,15 @@ function createEmbed(device: any, searchQuery: string, results: number, index: n
|
|||
let battery = device.description.match(/(?:chipset,)(.*)(?:battery)/)?.[1].trim() || 'Unknown';
|
||||
let date = device.description.match(/(?:Announced)(.*)(?:Features)/)?.[1].trim() || 'Unknown';
|
||||
let misc = 'Announced in '+date+' '+display+' display, '+battery+' battery.';
|
||||
|
||||
|
||||
// Change 1024GB to 1TB
|
||||
if (storage === '1024GB') {
|
||||
storage = '1TB';
|
||||
} else {
|
||||
storage = storage
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
const imageUrl = device?.img;
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor('#0099ff')
|
||||
|
@ -55,81 +55,94 @@ function createEmbed(device: any, searchQuery: string, results: number, index: n
|
|||
)
|
||||
.setThumbnail(imageUrl)
|
||||
.setFooter({ text: results+' total results... '+(index + 1)+'/'+devices.length });
|
||||
|
||||
|
||||
return embed;
|
||||
}
|
||||
|
||||
// actual command logic starts here
|
||||
export async function command (message: Message) {
|
||||
try {
|
||||
// reset index and button state
|
||||
let index = 0
|
||||
previous.setDisabled(true);
|
||||
next.setDisabled(false);
|
||||
// split message into array
|
||||
const words = message.content.split(' ');
|
||||
// End listener function to disable buttons and prevent multiple end listeners
|
||||
// from being applied to a single message collector.
|
||||
function addEndListener(collector, previous, next, row, sentMessage) {
|
||||
let endListenerAdded = false;
|
||||
|
||||
// remove the command and pass the rest of the message along to the gsmarena-api
|
||||
const searchQuery = words.slice(1).join(' ');
|
||||
const devices = await fakeapi.search(searchQuery);
|
||||
|
||||
if (devices.length === 0) {
|
||||
message.channel.send('No device(s) found for "'+searchQuery+'"');
|
||||
return;
|
||||
}
|
||||
|
||||
// create the initial embed with the first device information and reset button state
|
||||
const embed = createEmbed(devices[index], searchQuery, devices.length, index, devices);
|
||||
|
||||
const sentMessage = await message.channel.send({embeds: [embed], components: [row] });
|
||||
|
||||
// prevent another end listener from being added everytime a button is pushed
|
||||
// ran when the input collector times out, as set above
|
||||
let endListenerAdded = false;
|
||||
const endListener = () => {
|
||||
// Function to be called when the collector ends to disable buttons and update message.
|
||||
const endListener = async () => {
|
||||
previous.setDisabled(true);
|
||||
next.setDisabled(true);
|
||||
sentMessage.edit({ components: [row] });
|
||||
collector.removeListener('end', endListener); // Remove the listener
|
||||
await sentMessage.edit({ components: [row] }).catch(console.error);
|
||||
collector.stop();
|
||||
};
|
||||
|
||||
|
||||
// create the interaction collector -- runs when a button is pressed
|
||||
const filter = (interaction: Interaction) => interaction.isButton() && (interaction.customId === 'next' || interaction.customId === 'previous');
|
||||
const collector = sentMessage.createMessageComponentCollector({ filter, time: mtime });
|
||||
|
||||
// update everything when cycling pages
|
||||
collector.on('collect', async (interaction: Interaction) => {
|
||||
try {
|
||||
if (interaction.customId === 'next') {
|
||||
index++;
|
||||
} else if (interaction.customId === 'previous') {
|
||||
index--;
|
||||
}
|
||||
index = Math.max(0, Math.min(index, devices.length - 1));
|
||||
|
||||
// update button states
|
||||
previous.setDisabled(index === 0);
|
||||
next.setDisabled(index === devices.length - 1);
|
||||
|
||||
// create a new embed with the updated device information
|
||||
const newEmbed = createEmbed(devices[index], searchQuery, devices.length, index, devices);
|
||||
|
||||
// update the message with the new embed and components
|
||||
await interaction.update({ embeds: [newEmbed], components: [row] });
|
||||
// deal with end listener
|
||||
if (!endListenerAdded) {
|
||||
collector.on('end', endListener);
|
||||
endListenerAdded = true;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
});
|
||||
// Add the end listener only if it hasn't already been added.
|
||||
// I don't think this is needed since I reworked the endlistener,
|
||||
// but i'll keep it just in case, better to have an extra 5 lines of code than a memory leak.
|
||||
if (!endListenerAdded) {
|
||||
collector.on('end', endListener);
|
||||
endListenerAdded = true;
|
||||
}
|
||||
}
|
||||
|
||||
// error handling
|
||||
} catch (error) {
|
||||
console.error('Error:', error);
|
||||
message.channel.send('An error occurred while processing your request. It would appear whatever you just did broke the bot... Congratulations!');
|
||||
}
|
||||
}
|
||||
|
||||
// This runs every time the bot responds to a command.
|
||||
export async function command(message: Message) {
|
||||
try {
|
||||
// Reset index and button state.
|
||||
let index = 0;
|
||||
previous.setDisabled(true);
|
||||
next.setDisabled(false);
|
||||
// Split message into array.
|
||||
const words = message.content.split(' ');
|
||||
|
||||
// Remove the command and pass the rest of the message along to the gsmarena-api.
|
||||
const searchQuery = words.slice(1).join(' ');
|
||||
const devices = await fakeapi.search(searchQuery);
|
||||
|
||||
if (devices.length === 0) {
|
||||
message.channel.send('No device(s) found for "' + searchQuery + '"');
|
||||
return;
|
||||
}
|
||||
|
||||
// Create the initial embed with the first device information and reset button state.
|
||||
const embed = createEmbed(devices[index], searchQuery, devices.length, index, devices);
|
||||
const sentMessage = await message.channel.send({ embeds: [embed], components: [row] });
|
||||
|
||||
// Create the interaction collector -- handles button presses.
|
||||
const filter = (interaction: Interaction) => interaction.isButton() && (interaction.customId === 'next' || interaction.customId === 'previous');
|
||||
const collector = sentMessage.createMessageComponentCollector({ filter, time: mtime });
|
||||
addEndListener(collector, previous, next, row, sentMessage);
|
||||
|
||||
|
||||
// Logic to update the message and components when cycling through pages.
|
||||
collector.on('collect', async (interaction: Interaction) => {
|
||||
try {
|
||||
if (interaction.customId === 'next') {
|
||||
index++;
|
||||
} else if (interaction.customId === 'previous') {
|
||||
index--;
|
||||
}
|
||||
index = Math.max(0, Math.min(index, devices.length - 1));
|
||||
|
||||
// Update button states based on current index.
|
||||
previous.setDisabled(index === 0);
|
||||
next.setDisabled(index === devices.length - 1);
|
||||
|
||||
// Create a new embed with the updated device information.
|
||||
const newEmbed = createEmbed(devices[index], searchQuery, devices.length, index, devices);
|
||||
|
||||
// Update the message with the new embed and components.
|
||||
await interaction.deferUpdate();
|
||||
await interaction.editReply({ embeds: [newEmbed], components: [row] });
|
||||
|
||||
|
||||
} catch (error) {
|
||||
console.error('Collect Error:', error);
|
||||
await interaction.followUp('An error occurred while processing the button click.')
|
||||
} finally {
|
||||
previous.setDisabled();
|
||||
next.setDisabled();
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Command Error:', error);
|
||||
message.channel.send('An error occurred while processing your request. It would appear whatever you just did broke the bot... Congratulations!');
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue