mirror of
https://github.com/citra-emu/discord-bot.git
synced 2025-06-02 05:50:22 +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';
|
import { search as fakeapi } from 'gsmarena-api';
|
||||||
|
|
||||||
// Variables you might want to change:
|
// Variables you might want to change:
|
||||||
// allowed roles
|
// Allowed roles:
|
||||||
export const roles = ["@everyone"];
|
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
|
const mtime = 200000
|
||||||
|
|
||||||
|
|
||||||
// button config
|
// Button config:
|
||||||
const next = new ButtonBuilder()
|
const next = new ButtonBuilder()
|
||||||
.setCustomId('next')
|
.setCustomId('next')
|
||||||
.setLabel('Next Result >')
|
.setLabel('Next Result >')
|
||||||
|
@ -22,7 +22,7 @@ const previous = new ButtonBuilder()
|
||||||
const row = new ActionRowBuilder()
|
const row = new ActionRowBuilder()
|
||||||
.addComponents(previous, next);
|
.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) {
|
function createEmbed(device: any, searchQuery: string, results: number, index: number, devices: array) {
|
||||||
let name = device.name;
|
let name = device.name;
|
||||||
let chipset = device.description.match(/(?:display,)(.*)(?:chipset)/)?.[1].trim() || 'Unknown'; // parsing json data
|
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 battery = device.description.match(/(?:chipset,)(.*)(?:battery)/)?.[1].trim() || 'Unknown';
|
||||||
let date = device.description.match(/(?:Announced)(.*)(?:Features)/)?.[1].trim() || 'Unknown';
|
let date = device.description.match(/(?:Announced)(.*)(?:Features)/)?.[1].trim() || 'Unknown';
|
||||||
let misc = 'Announced in '+date+' '+display+' display, '+battery+' battery.';
|
let misc = 'Announced in '+date+' '+display+' display, '+battery+' battery.';
|
||||||
|
|
||||||
// Change 1024GB to 1TB
|
// Change 1024GB to 1TB
|
||||||
if (storage === '1024GB') {
|
if (storage === '1024GB') {
|
||||||
storage = '1TB';
|
storage = '1TB';
|
||||||
} else {
|
} else {
|
||||||
storage = storage
|
storage = storage
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const imageUrl = device?.img;
|
const imageUrl = device?.img;
|
||||||
const embed = new EmbedBuilder()
|
const embed = new EmbedBuilder()
|
||||||
.setColor('#0099ff')
|
.setColor('#0099ff')
|
||||||
|
@ -55,81 +55,94 @@ function createEmbed(device: any, searchQuery: string, results: number, index: n
|
||||||
)
|
)
|
||||||
.setThumbnail(imageUrl)
|
.setThumbnail(imageUrl)
|
||||||
.setFooter({ text: results+' total results... '+(index + 1)+'/'+devices.length });
|
.setFooter({ text: results+' total results... '+(index + 1)+'/'+devices.length });
|
||||||
|
|
||||||
return embed;
|
return embed;
|
||||||
}
|
}
|
||||||
|
|
||||||
// actual command logic starts here
|
// End listener function to disable buttons and prevent multiple end listeners
|
||||||
export async function command (message: Message) {
|
// from being applied to a single message collector.
|
||||||
try {
|
function addEndListener(collector, previous, next, row, sentMessage) {
|
||||||
// reset index and button state
|
let endListenerAdded = false;
|
||||||
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
|
// Function to be called when the collector ends to disable buttons and update message.
|
||||||
const searchQuery = words.slice(1).join(' ');
|
const endListener = async () => {
|
||||||
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 = () => {
|
|
||||||
previous.setDisabled(true);
|
previous.setDisabled(true);
|
||||||
next.setDisabled(true);
|
next.setDisabled(true);
|
||||||
sentMessage.edit({ components: [row] });
|
await sentMessage.edit({ components: [row] }).catch(console.error);
|
||||||
collector.removeListener('end', endListener); // Remove the listener
|
collector.stop();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// create the interaction collector -- runs when a button is pressed
|
// Add the end listener only if it hasn't already been added.
|
||||||
const filter = (interaction: Interaction) => interaction.isButton() && (interaction.customId === 'next' || interaction.customId === 'previous');
|
// I don't think this is needed since I reworked the endlistener,
|
||||||
const collector = sentMessage.createMessageComponentCollector({ filter, time: mtime });
|
// but i'll keep it just in case, better to have an extra 5 lines of code than a memory leak.
|
||||||
|
if (!endListenerAdded) {
|
||||||
// update everything when cycling pages
|
collector.on('end', endListener);
|
||||||
collector.on('collect', async (interaction: Interaction) => {
|
endListenerAdded = true;
|
||||||
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);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// error handling
|
|
||||||
} catch (error) {
|
// This runs every time the bot responds to a command.
|
||||||
console.error('Error:', error);
|
export async function command(message: Message) {
|
||||||
message.channel.send('An error occurred while processing your request. It would appear whatever you just did broke the bot... Congratulations!');
|
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