rewrite /number to use threads

This commit is contained in:
Jonny_Bro (Nikita) 2024-04-26 21:35:34 +05:00
parent 5308d19d56
commit dbfc178bac
No known key found for this signature in database
GPG key ID: 3F1ECC04147E9BD8
19 changed files with 242 additions and 189 deletions

View file

@ -206,7 +206,7 @@ class JaBaClient extends Client {
* @param {Object[]} [data.fields] - An array of field objects for the embed.
* @param {string} [data.image] - The URL of the image for the embed.
* @param {string} [data.url] - The URL to be used as the embed's hyperlink.
* @param {number} [data.color] - The color of the embed's border. If not provided, the default color from the client's configuration will be used.
* @param {string} [data.color] - The HEX color of the embed's border. If not provided, the default color from the client's configuration will be used.
* @param {string} [data.footer] - The text to be displayed as the embed's footer. If not provided, the default footer from the client's configuration will be used.
* @param {Date} [data.timestamp] - The timestamp to be displayed in the embed's footer. If not provided, the current timestamp will be used.
* @param {string|Object} [data.author] - The author information for the embed. Can be a string (name) or an object with `name` and/or `iconURL` properties.
@ -316,7 +316,7 @@ class JaBaClient extends Client {
/**
* Finds or creates a user in the database based on the provided user ID.
* @param {string} userID - The ID of the user to find or create.
* @returns {import("./User")} The user data object, either retrieved from the database or newly created.
* @returns {Promise<import("./User")>} The user data object, either retrieved from the database or newly created.
*/
async findOrCreateUser(userID) {
let userData = await this.usersData.findOne({ id: userID });
@ -341,7 +341,7 @@ class JaBaClient extends Client {
* @param {Object} options - The options for finding or creating the member.
* @param {string} options.id - The ID of the member to find or create.
* @param {string} options.guildId - The ID of the guild the member belongs to.
* @returns {import("./Member")} The member data object, either retrieved from the database or newly created.
* @returns {Promise<import("./Member")>} The member data object, either retrieved from the database or newly created.
*/
async findOrCreateMember({ id: memberID, guildId }) {
let memberData = await this.membersData.findOne({ guildID: guildId, id: memberID });
@ -372,7 +372,7 @@ class JaBaClient extends Client {
/**
* Finds or creates a guild in the database based on the provided guild ID.
* @param {string} guildId - The ID of the guild to find or create.
* @returns {import("./Guild")} The guild data object, either retrieved from the database or newly created.
* @returns {Promise<import("./Guild")>} The guild data object, either retrieved from the database or newly created.
*/
async findOrCreateGuild(guildId) {
let guildData = await this.guildsData.findOne({ id: guildId }).populate("members");

View file

@ -1,4 +1,4 @@
const { SlashCommandBuilder, MessageCollector } = require("discord.js");
const { SlashCommandBuilder, MessageCollector, ButtonBuilder, ActionRowBuilder, ButtonStyle, ThreadAutoArchiveDuration } = require("discord.js");
const BaseCommand = require("../../base/BaseCommand"),
currentGames = {};
@ -31,43 +31,51 @@ class Number extends BaseCommand {
if (currentGames[interaction.guildId]) return interaction.error("fun/number:GAME_RUNNING");
const participants = [],
number = client.functions.randomNum(1000, 5000);
number = client.functions.randomNum(100, 101);
await interaction.replyT("fun/number:GAME_START");
const gameCreatedAt = Date.now();
let channel;
if (interaction.channel.isThread()) channel = interaction.channel;
else
channel = await interaction.channel.threads.create({
name: `number-guessing-${client.functions.randomNum(10000, 20000)}`,
autoArchiveDuration: ThreadAutoArchiveDuration.OneHour,
});
const gameCreatedAt = Date.now();
const filter = m => !m.author.bot;
const collector = new MessageCollector(interaction.channel, {
const collector = new MessageCollector(channel, {
filter,
time: 5 * 60 * 1000,
});
currentGames[interaction.guildId] = true;
collector.on("collect", async msg => {
if (!participants.includes(msg.author.id)) participants.push(msg.author.id);
if (!participants.includes(msg.author)) participants.push(msg.author);
if (msg.content === "STOP") return collector.stop("force");
if (isNaN(msg.content)) return;
const parsedNumber = parseInt(msg.content, 10);
if (parsedNumber === number) {
interaction.channel.send({
channel.send({
content: interaction.translate("fun/number:GAME_STATS", {
winner: msg.author.toString(),
number,
time: `<t:${Math.floor(gameCreatedAt / 1000)}:R>`,
participantCount: participants.length,
participants: participants.map(p => `<@${p}>`).join(", "),
participants: participants.map(p => p.toString()).join(", "),
}),
});
if (participants.length > 1) {
const won = 100 * (participants.length * 0.5);
interaction.channel.send({
channel.send({
content: interaction.translate("fun/number:WON", {
winner: msg.author.username,
winner: msg.author.toString(),
credits: `**${won}** ${client.functions.getNoun(won, interaction.translate("misc:NOUNS:CREDIT:1"), interaction.translate("misc:NOUNS:CREDIT:2"), interaction.translate("misc:NOUNS:CREDIT:5"))}`,
}),
});
@ -91,6 +99,33 @@ class Number extends BaseCommand {
await memberData.save();
}
interaction.editReply({
content: interaction.translate("fun/number:GAME_STATS", {
winner: msg.author.toString(),
number,
time: `<t:${Math.floor(gameCreatedAt / 1000)}:R>`,
participantCount: participants.length,
participants: participants.map(p => p.toString()).join(", "),
}),
});
channel.setArchived(true);
const deleteYes = new ButtonBuilder()
.setCustomId("number_delete_yes")
.setLabel(interaction.translate("common:YES"))
.setStyle(ButtonStyle.Danger);
const deleteNo = new ButtonBuilder()
.setCustomId("number_delete_no")
.setLabel(interaction.translate("common:NO"))
.setStyle(ButtonStyle.Secondary);
const row = new ActionRowBuilder().addComponents(deleteYes, deleteNo);
channel.send({
content: interaction.translate("fun/number:DELETE_CHANNEL"),
components: [row],
});
collector.stop();
}
@ -110,6 +145,23 @@ class Number extends BaseCommand {
else if (reason === "force") return interaction.editReply({ content: interaction.translate("misc:FORCE_STOP", { user: interaction.member.toString(), number }) });
});
}
/**
*
* @param {import("../../base/Client")} client
*/
async onLoad(client) {
client.on("interactionCreate", async interaction => {
if (!interaction.isButton()) return;
await interaction.deferUpdate();
if (interaction.customId === "number_delete_yes")
interaction.channel.delete();
else if (interaction.customId === "number_delete_no")
interaction.message.delete();
});
}
}
module.exports = Number;

View file

@ -30,7 +30,6 @@ class CreateTicketEmbed extends BaseCommand {
client.on("interactionCreate", async interaction => {
if (!interaction.isButton()) return;
if (interaction.isButton()) {
interaction.data = [];
interaction.data.guild = await client.findOrCreateGuild(interaction.guildId);
@ -200,7 +199,6 @@ class CreateTicketEmbed extends BaseCommand {
}
} else return;
}
}
});
}

View file

@ -19,20 +19,20 @@ BaseInteraction.prototype.replyT = async function (key, args, options = {}) {
const translated = this.translate(key, args, this.getLocale());
const string = options.prefixEmoji ? `${this.client.customEmojis[options.prefixEmoji]} | ${translated}` : translated;
if (options.edit) return await this.editReply({ content: string, ephemeral: options.ephemeral || false });
else return await this.reply({ content: string, ephemeral: options.ephemeral || false });
if (options.edit) return this.editReply({ content: string, ephemeral: options.ephemeral || false });
else return this.reply({ content: string, ephemeral: options.ephemeral || false });
};
BaseInteraction.prototype.success = async function (key, args, options = {}) {
options.prefixEmoji = "success";
return await this.replyT(key, args, options);
return this.replyT(key, args, options);
};
BaseInteraction.prototype.error = async function (key, args, options = {}) {
options.prefixEmoji = "error";
return await this.replyT(key, args, options);
return this.replyT(key, args, options);
};
Message.prototype.getLocale = function () {
@ -50,18 +50,18 @@ Message.prototype.replyT = async function (key, args, options = {}) {
const translated = this.translate(key, args, this.getLocale());
const string = options.prefixEmoji ? `${this.client.customEmojis[options.prefixEmoji]} | ${translated}` : translated;
if (options.edit) return await this.edit({ content: string, allowedMentions: { repliedUser: options.mention ? true : false } });
else return await this.reply({ content: string, allowedMentions: { repliedUser: options.mention ? true : false } });
if (options.edit) return this.edit({ content: string, allowedMentions: { repliedUser: options.mention ? true : false } });
else return this.reply({ content: string, allowedMentions: { repliedUser: options.mention ? true : false } });
};
Message.prototype.success = async function (key, args, options = {}) {
options.prefixEmoji = "success";
return await this.replyT(key, args, options);
return this.replyT(key, args, options);
};
Message.prototype.error = async function (key, args, options = {}) {
options.prefixEmoji = "error";
return await this.replyT(key, args, options);
return this.replyT(key, args, options);
};

View file

@ -8,5 +8,6 @@
"WON": "{{winner}} has won {{credits}}",
"DEFEAT": "No one guessed the number! It was **{{number}}**",
"GAME_STATS": "🎉 | {{winner}} has guessed the number! It was **{{number}}**!\n\n**Statistics:**\n*-* __**Duration**__: {{time}}\n*-* __**Participants ({{participantCount}})**__: {{participants}}",
"GAME_RUNNING": "The game is already in progress"
"GAME_RUNNING": "The game is already in progress",
"DELETE_CHANNEL": "Game has ended. Do you want to delete this channel?"
}

View file

@ -8,5 +8,6 @@
"WON": "{{winner}} выиграл {{credits}}",
"DEFEAT": "Никто не угадал число! Им было **{{number}}**",
"GAME_STATS": "🎉 | {{winner}} угадал число! Им было **{{number}}**!\n\n**Статистика:**\n*-* __**Длительность**__: {{time}}\n*-* __**Участники ({{participantCount}})**__: {{participants}}",
"GAME_RUNNING": "Игра уже идёт"
"GAME_RUNNING": "Игра уже идёт",
"DELETE_CHANNEL": "Игра окончена. Вы хотите удалить этот канал?"
}

View file

@ -8,5 +8,6 @@
"WON": "{{winner}} виграв {{credits}}",
"DEFEAT": "Ніхто не вгадав число! Їм було **{{number}}**",
"GAME_STATS": "🎉 | {{winner}} вгадав число! Їм було **{{number}}**!\n\n**Статистика:**\n*-* __**Тривалість**__: {{time}}\n*-* __**Учасники ({{participantCount}})**__: {{participants}}",
"GAME_RUNNING": "Гра вже йде"
"GAME_RUNNING": "Гра вже йде",
"DELETE_CHANNEL": "Гру закінчено. Ви хочете видалити цей канал?"
}