diff --git a/.gitignore b/.gitignore index ce2ce65..16b52f6 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ config.json templates.file node_modules package-lock.json -guilds-*.txt \ No newline at end of file +guilds-*.txt +guilds/*.json \ No newline at end of file diff --git a/commands/exitMsg.js b/commands/exitMsg.js new file mode 100644 index 0000000..5019978 --- /dev/null +++ b/commands/exitMsg.js @@ -0,0 +1,95 @@ +const fs = require('fs'); +module.exports = { + name: 'exitmsg', + description: 'Enable or disable the exit message and edit the exit message. To mention the user, use {{USER}}.', + help: 'Function to edit the message that is sent when a user leaves the server, use {{USER}} to mention the user.', + options: [ + { + name: 'enabled', + description: 'Enable or disable the exit message', + type: 5, + required: true, + }, + { + name: 'title', + description: 'Title of the exit message', + type: 3, + required: false, + }, + { + name: 'description', + description: 'Description of the exit message, use {{USER}} to mention the user', + type: 3, + required: false, + }, + { + name: 'channel', + description: 'Channel to send the exit message', + type: 7, + required: false, + } + ], + slash: true, + text: true, + admin: true, + requireKick: false, + requireBan: false, + canBeUsedInDm: true, + contexts: ['GUILD_TEXT'], + integration_types: [0], + execute(message, args) { + message.channel.send('This command can only be used as a slash command.'); + }, + executeSlash(interaction) { + //check if the user has the required permissions + if (!interaction.member.permissions.has('ADMINISTRATOR')) { + interaction.reply('You need to have the administrator permission to use this command.'); + return; + } + //check if all the required arguments are provided + if(!interaction.options.getBoolean('enabled') || !interaction.options.getString('title') || !interaction.options.getString('description') || !interaction.options.getChannel('channel')) { + interaction.reply('Please provide all the required information.'); + return; + } + const enabled = interaction.options.getBoolean('enabled'); + const title = interaction.options.getString('title'); + const description = interaction.options.getString('description'); + const channel = interaction.options.getChannel('channel'); + const filePath = `./guilds/guilds-${interaction.guild.id}.json`; + + if(enabled === null || title === null || description === null || channel === null) { + interaction.reply('Please provide all the required information.'); + return; + } + + if(channel.type !== 0) { + interaction.reply('Please provide a text channel.'); + return; + } + + + if(enabled === false) { + //get data from file + const data = JSON.parse(fs.readFileSync(filePath)); + data.goodbyeMessage.title = null; + data.goodbyeMessage.content = null; + data.goodbyeChannel = null; + data.goodbye = false; + fs.writeFileSync(filePath, JSON.stringify(data)); + interaction.reply('Exit message disabled.'); + return; + } + else + { + const data = JSON.parse(fs.readFileSync(filePath)); + data.goodbyeMessage.title = title; + data.goodbyeMessage.content = description; + data.goodbyeChannel = channel.id; + data.goodbye = true; + + fs.writeFileSync(filePath, JSON.stringify(data)); + interaction.reply('Exit message updated successfully.'); + return; + } + }, +}; diff --git a/commands/newUserMsg.js b/commands/newUserMsg.js new file mode 100644 index 0000000..1f5b819 --- /dev/null +++ b/commands/newUserMsg.js @@ -0,0 +1,101 @@ +const fs = require('fs'); +module.exports = { + name: 'newusermsg', + description: 'Edit the message that is sent when a new user joins the server, use {{USER}} to mention the user.', + help: 'Function to edit the message that is sent when a new user joins the server, use {{USER}} to mention the user.', + //enabled/disabled, title, description, channel + options: [ + { + name: 'enabled', + description: 'Enable or disable the welcome message', + type: 5, + required: true, + }, + { + name: 'title', + description: 'Title of the welcome message', + type: 3, + required: false, + }, + { + name: 'description', + description: 'Description of the welcome message, use {{USER}} to mention the user', + type: 3, + required: false, + }, + { + name: 'channel', + description: 'Channel to send the welcome message', + type: 7, + required: false, + } + ], + slash: true, + text: true, + admin: true, + requireKick: true, + requireBan: true, + canBeUsedInDm: false, + contexts: ['GUILD_TEXT'], + integration_types: [0, 1], + execute(message, args) { + message.channel.send('This command can only be used as a slash command.'); + }, + executeSlash(interaction) { + //check if the user has the required permissions + if (!interaction.member.permissions.has('ADMINISTRATOR')) { + interaction.reply('You need to have the administrator permission to use this command.'); + return; + } + //check if all the required arguments are provided + if(!interaction.options.getBoolean('enabled') || !interaction.options.getString('title') || !interaction.options.getString('description') || !interaction.options.getChannel('channel')) { + interaction.reply('Please provide all the required information.'); + return; + } + const enabled = interaction.options.getBoolean('enabled'); + const title = interaction.options.getString('title'); + const description = interaction.options.getString('description'); + const channel = interaction.options.getChannel('channel'); + const filePath = `./guilds/guilds-${interaction.guild.id}.json`; + + if(enabled === null || title === null || description === null || channel === null) { + interaction.reply('Please provide all the required information.'); + return; + } + //check if the channel is a text channel + if(channel.type !== 0) { + interaction.reply('Please provide a text channel.'); + return; + } + + if(enabled === false) { + //get data from file + if (fs.existsSync(filePath)) { + const data = JSON.parse(fs.readFileSync(filePath)); + data.hello = false; + data.helloChannel = null; + data.welcomeMessage.title = null; + data.welcomeMessage.content = null; + fs.writeFileSync(filePath, JSON.stringify(data)); + } + interaction.reply('Welcome message disabled.'); + return; + } + else + { + //get data from file + if (fs.existsSync(filePath)) { + const data = JSON.parse(fs.readFileSync(filePath)); + data.hello = true; + data.helloChannel = channel.id; + data.welcomeMessage.title = title; + data.welcomeMessage.content = description; + fs.writeFileSync(filePath, JSON.stringify(data)); + } + interaction.reply('Welcome message enabled.'); + return; + } + + + }, +}; diff --git a/commands/rulesMsg.js b/commands/rulesMsg.js new file mode 100644 index 0000000..f6d3401 --- /dev/null +++ b/commands/rulesMsg.js @@ -0,0 +1,76 @@ +const fs = require('fs'); +module.exports = { + name: 'rulesmsg', + description: 'Enable or disable the rules message and edit the rules message.', + help: 'Use {{RULES}} to mention the rules channel', + options: [ + { + name: 'enabled', + description: 'Enable or disable the rules message', + type: 5, + required: true, + }, + { + name: 'ruleline', + description: 'Description of the rules message, use {{RULES}} to mention the rules channel', + type: 3, + required: false, + }, + { + name: 'channel', + description: 'Channel to send the rules message', + type: 7, + required: false, + } + ], + slash: true, + text: true, + admin: true, + requireKick: false, + requireBan: false, + canBeUsedInDm: false, + contexts: ['GUILD_TEXT'], + integration_types: [0], + execute(message, args) { + message.channel.send('This command can only be used as a slash command.'); + }, + executeSlash(interaction) { + //check if the user has the required permissions + if (!interaction.member.permissions.has('ADMINISTRATOR')) { + interaction.reply('You need to have the administrator permission to use this command.'); + return; + } + //check if all the required arguments are provided + if(!interaction.options.getBoolean('enabled') || !interaction.options.getString('ruleline') || !interaction.options.getChannel('channel')) { + interaction.reply('Please provide all the required information.'); + return; + } + const enabled = interaction.options.getBoolean('enabled'); + const ruleline = interaction.options.getString('ruleline'); + const channel = interaction.options.getChannel('channel'); + const filePath = `./guilds/guilds-${interaction.guild.id}.json`; + + if(enabled === null || ruleline === null || channel === null) { + interaction.reply('Please provide all the required information.'); + return; + } + + if(channel.type !== 0) { + interaction.reply('Please provide a text channel.'); + return; + } + if (enabled) { + const data = JSON.parse(fs.readFileSync(filePath)); + data.rulesMessage = ruleline; + data.rulesChannel = channel.id; + fs.writeFileSync(filePath, JSON.stringify(data, null, 4)); + } + else { + const data = JSON.parse(fs.readFileSync(filePath)); + data.rulesMessage = null; + data.rulesChannel = null; + fs.writeFileSync(filePath, JSON.stringify(data, null, 4)); + } + interaction.reply('Rules message updated successfully.'); + }, +}; diff --git a/events/newGuild.js b/events/newGuild.js index 5ab6c43..fbaf097 100644 --- a/events/newGuild.js +++ b/events/newGuild.js @@ -9,5 +9,18 @@ module.exports = { const guildPath = `./guilds-${new Date().toISOString().split('T')[0]}.txt`; //save name - id - time of joining fs.appendFileSync(guildPath, `JOIN: \`${guild.name}\` - ${guild.id} - ${new Date().toLocaleString()}\n`); + const guildPath2 = `./guilds/guilds-${guild.id}.json`; + if (!fs.existsSync(guildPath2)) { + fs.writeFileSync(guildPath2, JSON.stringify({ id: guild.id, name: [guild.name], premium: false, prefix: prefix, premium:false, prefix:"^", hello: false, goodbye: true, helloChannel: null, goodbyeChannel: null, rulesChannel: null, welcomeMessage: { title: "Say hello", content: "Hello {{USER}}"}, goodbyeMessage: { title: "Say goodbye", content: "Goodbye {{USER}}"}, rulesMessage: null})); + } + else + { + const data = JSON.parse(fs.readFileSync(guildPath2)); + if (!data.name.includes(guild.name)) + { + data.name.push(guild.name); + fs.writeFileSync(guildPath2, JSON.stringify(data)); + } + } } }; \ No newline at end of file diff --git a/events/newMember.js b/events/newMember.js index 61554be..384b18c 100644 --- a/events/newMember.js +++ b/events/newMember.js @@ -1,28 +1,57 @@ const { Events, EmbedBuilder } = require('discord.js'); +const fs = require('fs'); function randomColor() { const r = Math.floor(Math.random() * 200) + 50; const g = Math.floor(Math.random() * 200) + 50; const b = Math.floor(Math.random() * 200) + 50; return `#${r.toString(16)}${g.toString(16)}${b.toString(16)}`; } -module.exports = { - name: Events.GuildMemberAdd, - once: false, - execute(args) { - const { guild, user } = args; - const channel = guild.channels.cache.find(channel => ['new-member', 'new-users', 'hello', 'welcome'].includes(channel.name)); - //find rules channel - const rulesChannel = guild.channels.cache.find(channel => ['rules', 'regulations', 'regulamin'].includes(channel.name)); - if (!channel) return; - if(!rulesChannel) var text = 'Please read the rules in rules channel.'; - else var text = `Please read the rules in ${rulesChannel} channel.`; - const embed = new EmbedBuilder() - .setTitle(`Welcome ${user.username}!`) - .setDescription(`Welcome to ${guild.name}! \n${text}`) - .setColor(randomColor()) - .setThumbnail(user.avatarURL()) - .setTimestamp(); - channel.send({ embeds: [embed] }); +module.exports = { + name: Events.GuildMemberAdd, + once: false, + execute(member) { + const { guild, user } = member; + const filePath = `./guilds/guilds-${guild.id}.json`; + + if (fs.existsSync(filePath)) { + const data = JSON.parse(fs.readFileSync(filePath)); + + if (!data.hello || !data.helloChannel) return; + + let title = data.welcomeMessage && data.welcomeMessage.title ? data.welcomeMessage.title : "👋 Welcome!"; + let description = data.welcomeMessage && data.welcomeMessage.content ? data.welcomeMessage.content.replace("{{USER}}", user.username) : `Welcome to the server, ${user.username}!`; + if(data.rulesChannel == null || data.rulesMessage == null) + { + + } + else + { + //data.rulesMessage + #data.rulesChannel + description += "\n" + data.rulesMessage; + //replace {{RULES}} with the rules channel + description = description.replace("{{RULES}}", `<#${data.rulesChannel}>`); + } + + const embed = new EmbedBuilder() + .setTitle(title) + .setDescription(description) + .setColor(randomColor()) + .setThumbnail(user.avatarURL()) + .setTimestamp(); + + guild.channels.fetch(data.helloChannel) + .then(channel => { + if (channel) { + channel.send({ embeds: [embed] }) + .catch(console.error); + } else { + console.error("Welcome channel not found."); + } + }) + .catch(console.error); + } else { + return; + } } -}; \ No newline at end of file +}; diff --git a/events/ready.js b/events/ready.js index c90b91e..2655bfb 100644 --- a/events/ready.js +++ b/events/ready.js @@ -1,11 +1,66 @@ const { Events } = require('discord.js'); const { ActivityType } = require('discord.js'); - +const { prefix } = require('../config.json'); +const fs = require('fs'); module.exports = { name: Events.ClientReady, once: true, execute(client) { console.log(`Ready! Logged in as ${client.user.tag}`); - client.user.setActivity('kry008.xyz/bot/i | To see how to support type /supportbot', { type: ActivityType.PLAYING }); + client.user.setActivity('kry008.xyz/bot/i | To see how to support type /supportbot | Type /help to get all commands', { type: ActivityType.PLAYING }); + //read all guilds, create files with guildid.json if not exists + client.guilds.cache.forEach(guild => { + const guildPath = `./guilds/guilds-${guild.id}.json`; + if (!fs.existsSync(guildPath)) { + fs.writeFileSync(guildPath, JSON.stringify(({ id: guild.id, name: [guild.name], premium: false, prefix: prefix, premium:false, prefix:"^", modChannel: null, hello: false, goodbye: true, helloChannel: null, goodbyeChannel: null, rulesChannel: null, welcomeMessage: { title: "Say hello", content: "Hello {{USER}}"}, goodbyeMessage: { title: "Say goodbye", content: "Goodbye {{USER}}"}, rulesMessage: null}))); + } + else + { + const data = JSON.parse(fs.readFileSync(guildPath)); + if (!data.name.includes(guild.name)) + { + data.name.push(guild.name); + fs.writeFileSync(guildPath, JSON.stringify(data)); + } + if(!data.hello) + { + data.hello = false; + } + if(!data.goodbye) + { + data.goodbye = false; + } + if(!data.helloChannel) + { + data.helloChannel = null; + } + if(!data.goodbyeChannel) + { + data.goodbyeChannel = null; + } + if(!data.rulesChannel) + { + data.rulesChannel = null; + } + if(!data.welcomeMessage) + { + data.welcomeMessage = null; + } + if(!data.goodbyeMessage) + { + data.goodbyeMessage = null; + } + if(!data.rulesMessage) + { + data.rulesMessage = null; + } + if(!data.modChannel) + { + data.modChannel = null; + } + fs.writeFileSync(guildPath, JSON.stringify(data)); + } + }); + }, }; diff --git a/events/removeMember.js b/events/removeMember.js index a500d29..5cc9056 100644 --- a/events/removeMember.js +++ b/events/removeMember.js @@ -1,19 +1,42 @@ -//guildMemberRemove const { Events, EmbedBuilder } = require('discord.js'); +const fs = require('fs'); module.exports = { - name: Events.GuildMemberRemove, + name: Events.GuildMemberRemove, once: false, - execute(args) { - const { guild, user } = args; - const channel = guild.channels.cache.find(channel => ['goodbye', 'bye', 'left', 'leaving', 'wyjścia'].includes(channel.name)); - if (!channel) return; - const embed = new EmbedBuilder() - .setTitle(`Goodbye ${user.username}!`) - .setDescription(`Goodbye ${user.username}!`) - .setColor('#ff0000') - .setThumbnail(user.avatarURL()) - .setTimestamp(); - channel.send({ embeds: [embed] }); + execute(member) { + const { guild, user } = member; + const filePath = `./guilds/guilds-${guild.id}.json`; + + if (fs.existsSync(filePath)) { + const data = JSON.parse(fs.readFileSync(filePath)); + + if (!data.goodbye || !data.goodbyeChannel) return; + + let title = data.goodbyeMessage.title; + let description = data.goodbyeMessage.content ? data.goodbyeMessage.content.replace("{{USER}}", user.username) : `Goodbye ${user.username}!`; + + const embed = new EmbedBuilder() + .setTitle(title) + .setDescription(description) + .setColor("#ff0000") + .setThumbnail(user.avatarURL()) + .setTimestamp(); + + guild.channels.fetch(data.goodbyeChannel) + .then(channel => { + if (channel) { + channel.send({ embeds: [embed] }) + .catch(console.error); + } else { + console.error("Goodbye channel not found."); + } + }) + .catch(console.error); + } + else + { + return; + } } }; \ No newline at end of file diff --git a/guilds/HERE-GUILDS-DB b/guilds/HERE-GUILDS-DB new file mode 100644 index 0000000..e69de29