From f062c0e5c2e1fa685d0f6d82769c773f73a482c8 Mon Sep 17 00:00:00 2001 From: Lukian LEIZOUR Date: Fri, 27 Dec 2024 00:45:23 +0100 Subject: [PATCH] fixed bye message --- src/commands/guilds/disablefeature.ts | 2 +- src/commands/guilds/enablefeature.ts | 2 +- src/commands/guilds/setAdminRole.ts | 2 +- src/events/guildMemberAdd.ts | 14 ++++----- src/events/guildMemberRemove.ts | 45 +++++++++++++++++++++++++++ src/main.ts | 10 +++--- 6 files changed, 59 insertions(+), 16 deletions(-) create mode 100644 src/events/guildMemberRemove.ts diff --git a/src/commands/guilds/disablefeature.ts b/src/commands/guilds/disablefeature.ts index d1853bd..83014a9 100644 --- a/src/commands/guilds/disablefeature.ts +++ b/src/commands/guilds/disablefeature.ts @@ -38,7 +38,7 @@ export default { setFeature(connection, guild_id, feature ? feature : "", "false"); - connection.end() + await connection.end() const embed = successEmbed("The feature has been successfully disabled.", interaction.client.user.displayAvatarURL()) diff --git a/src/commands/guilds/enablefeature.ts b/src/commands/guilds/enablefeature.ts index ebc2ca5..01f7600 100644 --- a/src/commands/guilds/enablefeature.ts +++ b/src/commands/guilds/enablefeature.ts @@ -37,7 +37,7 @@ export default { const feature: string | null = interaction.options.getString("feature") - setFeature(connection, guild_id, feature ? feature : "", "true"); + await setFeature(connection, guild_id, feature ? feature : "", "true"); connection.end() diff --git a/src/commands/guilds/setAdminRole.ts b/src/commands/guilds/setAdminRole.ts index 96459a4..afa1ecd 100644 --- a/src/commands/guilds/setAdminRole.ts +++ b/src/commands/guilds/setAdminRole.ts @@ -20,7 +20,7 @@ export default { const connection = await connectToDb(); - const guild: any[] = await getGuild(connection, guild_id); + const guild = await getGuild(connection, guild_id); if (!guild[0]) { await addGuild(connection, guild_id); diff --git a/src/events/guildMemberAdd.ts b/src/events/guildMemberAdd.ts index ae56ae0..5e2e437 100644 --- a/src/events/guildMemberAdd.ts +++ b/src/events/guildMemberAdd.ts @@ -4,7 +4,7 @@ import { createWelcomeImage } from "../libs/imageGeneration.ts" export default { name: Events.GuildMemberAdd, - async execute(member: GuildMember ) { + async execute(member: GuildMember) { const connection = await connectToDb(); const guild = await getGuild(connection, member.guild.id); const config = await getWelcomeConfig(connection, member.guild.id); @@ -19,20 +19,18 @@ export default { } if (config[0].description_format) { - embed.setDescription(config[0].description_format.replace("{user}", `<@${member.user.id}>`).replace("{welcomer}", `<@&${config[0].welcomer_role_id ? config[0].welcomer_role_id : ""}>`)); + embed.setDescription(config[0].description_format.replace("{user}", `<@${member.user.id}>`).replace("{role}", `<@&${config[0].role_id ? config[0].role_id : ""}>`)); } if (config[0].vignette_url) { embed.setThumbnail(config[0].vignette_url); } - - var attachement; - var buffer; + var files = []; if (config[0].background_url) { - buffer = await createWelcomeImage(config[0].background_url, `https://cdn.discordapp.com/avatars/${member.user.id}/${member.user.avatar}.jpeg`, member.user.id); - attachement = new AttachmentBuilder(buffer, { name: `${member.user.id}.png` }) + const buffer = await createWelcomeImage(config[0].background_url, `https://cdn.discordapp.com/avatars/${member.user.id}/${member.user.avatar}.jpeg`, member.user.id); + const attachement = new AttachmentBuilder(buffer, { name: `${member.user.id}.png` }) files.push(attachement) embed.setImage(`attachment://${member.user.id}.png`) } @@ -40,7 +38,7 @@ export default { const channel = member.guild.channels.cache.get(config[0].channel_id); if (channel?.isTextBased()) { - channel.send({content: config[0].message_format ? config[0].message_format.replace("{user}", `<@${member.user.id}>`).replace("{welcomer}", `<@&${config[0].welcomer_role_id ? config[0].welcomer_role_id : ""}>`) : "", embeds: [embed], files: files}); + channel.send({content: config[0].message_format ? config[0].message_format.replace("{user}", `<@${member.user.id}>`).replace("{role}", `<@&${config[0].role_id ? config[0].role_id : ""}>`) : "", embeds: [embed], files: files}); } } }, diff --git a/src/events/guildMemberRemove.ts b/src/events/guildMemberRemove.ts new file mode 100644 index 0000000..0f8614f --- /dev/null +++ b/src/events/guildMemberRemove.ts @@ -0,0 +1,45 @@ +import { Events, GuildMember, EmbedBuilder, AttachmentBuilder } from "npm:discord.js"; +import { connectToDb, getGuild, getByeConfig } from "../libs/mysql.ts"; +import { createWelcomeImage } from "../libs/imageGeneration.ts" + +export default { + name: Events.GuildMemberRemove, + async execute(member: GuildMember) { + const connection = await connectToDb(); + const guild = await getGuild(connection, member.guild.id); + const config = await getByeConfig(connection, member.guild.id); + + connection.end(); + + if (guild[0]?.welcome_message && config[0]) { + const embed = new EmbedBuilder(); + + if (config[0].title) { + embed.setTitle(config[0].title); + } + + if (config[0].description_format) { + embed.setDescription(config[0].description_format.replace("{user}", `<@${member.user.id}>`).replace("{role}", `<@&${config[0].role_id ? config[0].role_id : ""}>`)); + } + + if (config[0].vignette_url) { + embed.setThumbnail(config[0].vignette_url); + } + + var files = []; + + if (config[0].background_url) { + const buffer = await createWelcomeImage(config[0].background_url, `https://cdn.discordapp.com/avatars/${member.user.id}/${member.user.avatar}.jpeg`, member.user.id); + const attachement = new AttachmentBuilder(buffer, { name: `${member.user.id}.png` }) + files.push(attachement) + embed.setImage(`attachment://${member.user.id}.png`) + } + + const channel = member.guild.channels.cache.get(config[0].channel_id); + + if (channel?.isTextBased()) { + channel.send({content: config[0].message_format ? config[0].message_format.replace("{user}", `<@${member.user.id}>`).replace("{role}", `<@&${config[0].role_id ? config[0].role_id : ""}>`) : "", embeds: [embed], files: files}); + } + } + }, +}; diff --git a/src/main.ts b/src/main.ts index 87eed77..c3e00b1 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,8 +1,7 @@ import * as fs from 'node:fs'; -import * as path from 'node:path'; import "dotenv/config"; -import { Client, Collection, REST, Routes, RESTPutAPIApplicationCommandsResult, GatewayIntentBits, Partials } from 'npm:discord.js'; -import { sendLog } from './libs/discord.ts'; +import { Client, Collection, REST, Routes, GatewayIntentBits, Partials } from 'npm:discord.js'; +import process from 'node:process'; const client: Client = new Client({ intents: [ @@ -10,7 +9,8 @@ const client: Client = new Client({ GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent, GatewayIntentBits.DirectMessages, - GatewayIntentBits.GuildMembers + GatewayIntentBits.GuildMembers, + GatewayIntentBits.GuildPresences ], partials: [ Partials.Channel, @@ -72,7 +72,7 @@ const rest = new REST().setToken(process.env.DISCORD_TOKEN ? process.env.DISCORD try { console.log(`Started refreshing ${commands.length} application (/) commands.`); - const data = await rest.put( + await rest.put( Routes.applicationCommands(process.env.BOT_ID ? process.env.BOT_ID : ""), { body: commands } );