diff --git a/src/commands/rank/buyer.ts b/src/commands/rank/buyer.ts index a9ed1ad..d221dc3 100644 --- a/src/commands/rank/buyer.ts +++ b/src/commands/rank/buyer.ts @@ -1,5 +1,6 @@ -import { EmbedBuilder, MessageFlags, SlashCommandBuilder } from 'discord.js'; +import { EmbedBuilder, userMention, MessageFlags, SlashCommandBuilder } from 'discord.js'; import { prisma } from '../../lib/prisma.ts'; +import emoji from '../../../assets/emoji.json' assert { type: "json" }; export default { data: new SlashCommandBuilder() @@ -38,13 +39,33 @@ export default { }); } catch (err) { console.error(`\t⚠️ | Buyer => Cannot get the database connection!\n\t\t(${err}).`); + await interaction.reply({ + content: `${emoji.answer.error} | Cannot connect to the database`, + flags: MessageFlags.Ephemeral + }); + return; + } + let guildData: Guild; + try { + guildData = await prisma.guild.findUnique({ + where: { + id: interaction.guild.id + } + }); + } catch (err) { + console.error(`\t⚠️ | INFO => Cannot get the database connection!\n\t\t(${err}).`); + await interaction.reply({ + content: `${emoji.answer.error} | Cannot connect to the database`, + flags: MessageFlags.Ephemeral + }); + return; } const target: GuildMember = interaction.options.getUser('target') switch (subcommand) { case 'add': if (!userData.isDev) { await interaction.reply({ - content: ` | This command is only for the developper of the bot`, + content: `${emoji.answer.no} | This command is only for the developper of the bot`, flags: MessageFlags.Ephemeral }); return; @@ -82,24 +103,28 @@ export default { } }); } catch (err) { - console.error(`⚠️ | Error when adding ${target.username} to the username`); + console.error(`⚠️ | Error when adding ${target.username} to the buyer list`); + await interaction.reply({ + content: `${emoji.answer.error} | Error when adding ${target.username} to the owner list`, + flags: MessageFlags.Ephemeral + }); return; } await interaction.reply({ - content: ` | ${target.username} has been added to the buyer list`, + content: `${emoji.answer.yes} | ${target.username} has been added to the buyer list`, flags: MessageFlags.Ephemeral }); return; case 'delete': if (!userData.isDev) { await interaction.reply({ - content: ` | This command is only for buyer`, + content: `${emoji.answer.no} | This command is only for buyer`, flags: MessageFlags.Ephemeral }); return; } else if (interaction.user.id === target.id) { await interaction.reply({ - content: ` | You cannot removing yourself form the buyer list`, + content: `${emoji.answer.no} | You cannot removing yourself from the buyer list`, flags: MessageFlags.Ephemeral }); return; @@ -141,14 +166,14 @@ export default { return; } await interaction.reply({ - content: ` | ${target.username} has been removing to the buyer list`, + content: `${emoji.answer.yes} | ${target.username} has been removing to the buyer list`, flags: MessageFlags.Ephemeral }); return; case 'list': if (!userData.isBuyer) { await interaction.reply({ - content: ` | This command is only for buyer`, + content: `${emoji.answer.no} | This command is only for buyer`, flags: MessageFlags.Ephemeral }); return; @@ -165,19 +190,31 @@ export default { if (!bot || bot.buyers.length === 0) { await interaction.reply({ - content: ' | There is no buyer registered.', + content: `${emoji.answer.error} | There is no buyer registered.`, flags: MessageFlags.Ephemeral }); break; } - const buyerList = bot.buyers - .map(buyer => `- <@${buyer.id}>`) - .join('\n'); + const buyerList = await Promise.all( + bot.buyers.map(async (buyer) => { + try { + const user = await interaction.client.users.fetch(buyer.id); + return `- ${user.username} (\`${user.id}\`)\n`; + } catch (err) { + console.warn(`⚠️ | ${buyer.id} : ${err}`); + return null; + } + }) + ); const toSend: EmbedBuilder = new EmbedBuilder() - .setTitle(`Buyer list`) - .setDescription(`${buyerList}`) + .setTitle(`${emoji.badge.buyer} | Buyer list`) + .setColor(guildData.color) + .setFooter({ + text: guildData.footer + }) + .setDescription(buyerList.filter(Boolean).join('')) await interaction.reply({ embeds: [ toSend @@ -185,11 +222,12 @@ export default { flags: MessageFlags.Ephemeral }); } catch (err) { - console.error(`⚠️ | Buyer => error when fetching information from the database: ${err}`); + console.error(`⚠️ | Buyer => error when fetching infromation from the database: ${err}`); await interaction.reply({ - content: ' | Cannot fetch the information of the database.', + content: `${emoji.answer.error} | Cannot fetch the infromation of the database.`, flags: MessageFlags.Ephemeral }); + return; } break; return; diff --git a/src/commands/rank/owner.ts b/src/commands/rank/owner.ts index 63063e6..349e03e 100644 --- a/src/commands/rank/owner.ts +++ b/src/commands/rank/owner.ts @@ -1,5 +1,6 @@ -import { EmbedBuilder, MessageFlags, SlashCommandBuilder } from 'discord.js'; +import { EmbedBuilder, userMention, MessageFlags, SlashCommandBuilder } from 'discord.js'; import { prisma } from '../../lib/prisma.ts'; +import emoji from '../../../assets/emoji.json' assert { type: "json" }; export default { data: new SlashCommandBuilder() @@ -38,13 +39,33 @@ export default { }); } catch (err) { console.error(`\t⚠️ | Owner => Cannot get the database connection!\n\t\t(${err}).`); + await interaction.reply({ + content: `${emoji.answer.error} | Cannot connect to the database`, + flags: MessageFlags.Ephemeral + }); + return; + } + let guildData: Guild; + try { + guildData = await prisma.guild.findUnique({ + where: { + id: interaction.guild.id + } + }); + } catch (err) { + console.error(`\t⚠️ | INFO => Cannot get the database connection!\n\t\t(${err}).`); + await interaction.reply({ + content: `${emoji.answer.error} | Cannot connect to the database`, + flags: MessageFlags.Ephemeral + }); + return; } const target: GuildMember = interaction.options.getUser('target') switch (subcommand) { case 'add': if (!userData.isBuyer) { await interaction.reply({ - content: ` | This command is only for buyer`, + content: `${emoji.answer.no} | This command is only for buyer`, flags: MessageFlags.Ephemeral }); return; @@ -76,23 +97,27 @@ export default { }); } catch (err) { console.error(`⚠️ | Error when adding ${target.username} to the username`); + await interaction.reply({ + content: `${emoji.answer.error} | Error when adding ${target.username} to the owner list`, + flags: MessageFlags.Ephemeral + }); return; } await interaction.reply({ - content: ` | ${target.username} has been added to the owner list`, + content: `${emoji.answer.yes} | ${target.username} has been added to the owner list`, flags: MessageFlags.Ephemeral }); return; case 'delete': if (!userData.isBuyer) { await interaction.reply({ - content: ` | This command is only for buyer`, + content: `${emoji.answer.no} | This command is only for buyer`, flags: MessageFlags.Ephemeral }); return; } else if (interaction.user.id === target.id) { await interaction.reply({ - content: ` | You cannot removing yourself form the owner list`, + content: `${emoji.answer.no} | You cannot removing yourself from the owner list`, flags: MessageFlags.Ephemeral }); return; @@ -124,17 +149,21 @@ export default { }); } catch (err) { console.error(`⚠️ | Error when removing ${target.username} to the username`); + await interaction.reply({ + content: `${emoji.answer.error} | Cannot removing the user from the owner list`, + flags: MessageFlags.Ephemeral + }); return; } await interaction.reply({ - content: ` | ${target.username} has been removing to the owner list`, + content: `${emoji.answer.yes} | ${target.username} has been removing to the owner list`, flags: MessageFlags.Ephemeral }); return; case 'list': if (!userData.isOwner) { await interaction.reply({ - content: ` | This command is only for owner`, + content: `${emoji.answer.no} | This command is only for owner`, flags: MessageFlags.Ephemeral }); return; @@ -151,19 +180,31 @@ export default { if (!bot || bot.owners.length === 0) { await interaction.reply({ - content: ' | There is no owner registered.', + content: `${emoji.answer.error} | There is no owner registered.`, flags: MessageFlags.Ephemeral }); break; } - const ownerList = bot.owners - .map(owner => `- <@${owner.id}>`) - .join('\n'); + const ownerList = await Promise.all( + bot.owners.map(async (owner) => { + try { + const user = await interaction.client.users.fetch(owner.id); + return `- ${user.username} (\`${user.id}\`)\n`; + } catch (err) { + console.warn(`⚠️ | ${owner.id} : ${err}`); + return null; + } + }) + ); const toSend: EmbedBuilder = new EmbedBuilder() - .setTitle(`Owner list`) - .setDescription(`${ownerList}`) + .setTitle(`${emoji.badge.owner} | Owner list`) + .setColor(guildData.color) + .setFooter({ + text: guildData.footer + }) + .setDescription(ownerList.filter(Boolean).join('')) await interaction.reply({ embeds: [ toSend @@ -171,9 +212,9 @@ export default { flags: MessageFlags.Ephemeral }); } catch (err) { - console.error(`⚠️ | Buyer => error when fetching information from the database: ${err}`); + console.error(`⚠️ | Buyer => error when fetching infromation from the database: ${err}`); await interaction.reply({ - content: ' | Cannot fetch the information of the database.', + content: `${emoji.answer.error} | Cannot fetch the infromation of the database.`, flags: MessageFlags.Ephemeral }); } diff --git a/src/commands/utils/info.ts b/src/commands/utils/info.ts index e8b0a19..4303e14 100644 --- a/src/commands/utils/info.ts +++ b/src/commands/utils/info.ts @@ -1,5 +1,15 @@ import { prisma } from '../../lib/prisma.ts'; import { userMention, roleMention, MessageFlags, SlashCommandBuilder, EmbedBuilder } from 'discord.js'; +import emoji from '../../../assets/emoji.json' assert { type: "json" }; + +function getGuildRoles(guild: Guild): string { + const roles = guild.roles.cache + .filter(role => role.id !== guild.id) + .sort((a, b) => b.position - a.position) + .map(role => roleMention(role.id)); + + return roles.length > 0 ? roles.join(', ') : 'No role'; +} function getUserRoles(target: GuildMember): string { const roles = target.roles.cache @@ -7,7 +17,7 @@ function getUserRoles(target: GuildMember): string { .sort((a, b) => b.position - a.position) .map(role => `${roleMention(role.id)}`); - return roles.length > 0 ? roles.join(', ') : 'Aucun rôle'; + return roles.length > 0 ? roles.join(', ') : 'No role'; } function getUserBadges(userData: { @@ -19,15 +29,15 @@ function getUserBadges(userData: { const badges: string[] = []; if (userData.isDev) - badges.push("<:dev:1398755085441564772>"); + badges.push(`${emoji.badge.dev}`); if (userData.isEnium) - badges.push("<:enium_staff:1398755055930179586>"); + badges.push(`${emoji.badge.enium}`); if (userData.isPwn) - badges.push("<:dash:1398755072317325403>"); + badges.push(`${emoji.badge.dash}`); if (userData.isBuyer) - badges.push(""); + badges.push(`${emoji.badge.buyer}`); if (userData.isOwner) - badges.push(""); + badges.push(`${emoji.badge.owner}`); return badges.length > 0 ? badges.join(" ") : "Aucun badge"; } @@ -35,18 +45,18 @@ function getUserBadges(userData: { export default { data: new SlashCommandBuilder() .setName('info') - .setDescription('Show the information of one of these cathegories (user, server, bot)') + .setDescription('Show the infromation of one of these cathegories (user, server, bot)') .addSubcommand(subcommand => subcommand .setName('user') - .setDescription('Show the information of one user') + .setDescription('Show the infromation of one user') .addUserOption(option => option.setName('target') - .setDescription('The user to show the information') + .setDescription('The user to show the infromation') ) ) .addSubcommand(subcommand => subcommand .setName('server') - .setDescription('Show the information of the server') + .setDescription('Show the infromation of the server') ), async execute(interaction: CommandInteraction) { let guildData: Guild; @@ -58,6 +68,11 @@ export default { }); } catch (err) { console.error(`\t⚠️ | INFO => Cannot get the database connection!\n\t\t(${err}).`); + await interaction.reply({ + content: `${emoji.answer.error} | Cannot connect to the database`, + flags: MessageFlags.Ephemeral + }); + return; } const subcommand: string = interaction.options.getSubcommand(); switch (subcommand) { @@ -73,6 +88,10 @@ export default { }); } catch (err) { console.error(`\t⚠️ | USERINFO => Cannot get the database connection!\n\t\t(${err}).`); + await interaction.reply({ + content: `${emoji.answer.error} | Cannot connect to the database`, + flags: MessageFlags.Ephemeral + }); } let targetServer: GuildMember; @@ -80,11 +99,15 @@ export default { targetServer = await interaction.guild.members.fetch(targetGlobal.id); } catch (err) { console.error(`\t⚠️ | USERINFO => Cannot get the targetServer!\n\t\t(${err}).`); - return ; + await interaction.reply({ + content: `${emoji.answer.error} | Cannot get the guild profile of the user`, + flags: MessageFlags.Ephemeral + }); + return; } const userResult: EmbedBuilder = new EmbedBuilder() .setTitle(`${targetGlobal.displayName}'s information`) - .setColor(`${targetServer.displayHexColor}`) + .setColor(`${guildData.color}`) .setThumbnail(`${targetGlobal.displayAvatarURL()}`) .setFooter({ text: guildData.footer @@ -125,7 +148,7 @@ export default { .setFooter({ text: guildData.footer }) - .setImage(guild.bannerURL({dynamic: false, size: 2048})) + .setImage(guild.bannerURL({dynamic: true, size: 2048})) .setDescription(` **🆔 | ID:** ${guild.id} @@ -146,6 +169,10 @@ export default { ${guild.members.cache.filter((m) => !m.user.bot).size} **🤖 | Bots:** ${guild.members.cache.filter((m) => m.user.bot).size} + + **🏅 | Roles:** + There is ${guild.roles.cache.size - 1} on __${guild.name}__ + ${getGuildRoles(guild)} `) await interaction.reply({ embeds: [ diff --git a/src/events/client/ready.ts b/src/events/client/ready.ts index 302dac8..a7fe00a 100644 --- a/src/events/client/ready.ts +++ b/src/events/client/ready.ts @@ -1,9 +1,79 @@ -import { Events } from 'discord.js' +import { ActivityType, PresenceUpdateStatus, Events } from 'discord.js' +import { prisma } from '../../lib/prisma.ts'; export default { name: Events.ClientReady, once: true, - execute(client) { + async execute(client) { + try { + let botData: Bot; + botData = await prisma.bot.findUnique({ + where: { + id: 1 + } + }); + const newStatus: string = botData.status; + const tmpType: string = botData.type; + let newType: ActivityType; + switch (tmpType) { + case 'play': + newType = ActivityType.Playing; + break; + case 'listen': + newType = ActivityType.Listening; + break; + case 'watch': + newType = ActivityType.Watching; + break; + case 'stream': + newType = ActivityType.Streaming; + break; + case 'comptet': + newType = ActivityType.Competing; + break; + } + const tmpPresence: string = botData.presence + let newPresence: PresenceUpdateStatus; + switch (tmpPresence) { + case 'online': + newPresence = PresenceUpdateStatus.Online; + break; + case 'idle': + newPresence = PresenceUpdateStatus.Idle; + break; + case 'dnd': + newPresence = PresenceUpdateStatus.DoNotDisturb; + break; + case 'invisible': + newPresence = PresenceUpdateStatus.Invisible; + break; + } + if (botData.type === 'steam') { + client.user.setPresence({ + status: newPresence, + activities: [{ + name: newStatus, + type: newType, + url: 'https://twich.tv/EniumRaphael' + }] + }); + } else { + client.user.setPresence({ + status: newPresence, + activities: [{ + name: newStatus, + type: newType, + }] + }); + } + } catch (err) { + console.error(`\t⚠️ | INFO => Cannot get the database connection!\n\t\t(${err}).`); + await interaction.reply({ + content: `${emoji.answer.error} | Cannot connect to the database`, + flags: MessageFlags.Ephemeral + }); + return; + } console.log(`✅ | ${client.user.username} is now running under TTS bot`); }, }; diff --git a/src/events/interaction/interaction.ts b/src/events/interaction/interaction.ts index 206a329..fa103e3 100644 --- a/src/events/interaction/interaction.ts +++ b/src/events/interaction/interaction.ts @@ -8,6 +8,10 @@ export default { const command = interaction.client.commands.get(interaction.commandName); if (!command) { console.error(`⚠️ | Can't execute ${interaction.commandName}`); + await interaction.reply({ + content: `${emoji.answer.error} | Cannot execute the command ${interaction.commandName}`, + flags: MessageFlags.Ephemeral + }); return; } try { @@ -16,12 +20,12 @@ export default { console.error(`⚠️ | Error when occured this command ${interaction.commandName}\n\t${error}`); if (interaction.replied || interaction.deferred) { await interaction.followUp({ - content: ` | ${interaction.commandName} seems have a problem, thanks report that to the support (After Print)`, + content: `${emoji.answer.error} | ${interaction.commandName} seems have a problem, thanks report that to the support (After Print)`, flags: MessageFlags.Ephemeral }); } else { await interaction.reply({ - content: ` | ${interaction.commandName} seems have a problem, thanks report that to the support (Before Print)`, + content: `${emoji.answer.error} | ${interaction.commandName} seems have a problem, thanks report that to the support (Before Print)`, flags: MessageFlags.Ephemeral }); } diff --git a/src/index.ts b/src/index.ts index efa278e..55728ea 100644 --- a/src/index.ts +++ b/src/index.ts @@ -68,7 +68,7 @@ for (const folder of eventFolders) { console.log(`\t✅ | ${event.name}`); } } catch (err) { - console.error(`\t⚠️ | event at ${file}\n\t\t(${err}).`); + console.error(`\t⚠️ | Event at ${file}\n\t\t(${err}).`); } } } @@ -77,6 +77,13 @@ console.log('\n\n'); client.once('ready', async () => { console.log(`🤖 | Connecté en tant que ${client.user?.tag}`); + await prisma.bot.upsert({ + where: { + id: 1 + }, + update: {}, + create: {} + }); for (const [guildId, guild] of client.guilds.cache) { await prisma.guild.upsert({ where: {