From 56a4957ffa015bbb0503f987e0c0e5f02b8e1e8d Mon Sep 17 00:00:00 2001 From: Murali Anand <71955467+muralianand12345@users.noreply.github.com> Date: Mon, 12 Jun 2023 21:24:44 +0530 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=80=20Version=205.1.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Updated handler, streaming and vc create bug fix and improvements. --- commands/slash/vcCreate.js | 23 +++-- config/extras/glog.json | 2 +- config/extras/streamer.json | 11 -- config/extras/streaming.json | 5 + events/application/newvccreate.js | 166 ++++++++++++++++++++++-------- events/application/nickname.js | 79 +++++--------- events/guild/interactionCreate.js | 14 +-- events/guild/messageCreate.js | 58 +++++------ events/models/vcCreateGuild.js | 3 +- events/streamer/streamer.js | 126 ++++++----------------- handlers/commands/commands.js | 31 +++--- 11 files changed, 260 insertions(+), 258 deletions(-) delete mode 100644 config/extras/streamer.json create mode 100644 config/extras/streaming.json diff --git a/commands/slash/vcCreate.js b/commands/slash/vcCreate.js index 9c4d955..01844cd 100644 --- a/commands/slash/vcCreate.js +++ b/commands/slash/vcCreate.js @@ -27,6 +27,11 @@ module.exports = { .setDescription('Category/Parent where VC to be created!') .setRequired(true) ) + .addChannelOption(option => + option.setName('vc-log') + .setDescription('Logs VC create') + .setRequired(true) + ) .addStringOption(option => option.setName('vc-name') .setDescription('VC name to start with') @@ -43,7 +48,7 @@ module.exports = { const vcChan = await interaction.options.getChannel("vc-id"); if (!guildObj.channels.cache.has(vcChan.id)) { const embedReply = new EmbedBuilder() - .setColor('Red') + .setColor('#ED4245') .setDescription(`The Channel does not belong to this server!`) return interaction.reply({ embeds: [embedReply], @@ -52,7 +57,7 @@ module.exports = { } if (vcChan.type !== ChannelType.GuildVoice) { const embedReply = new EmbedBuilder() - .setColor('Red') + .setColor('#ED4245') .setDescription(`Select only voice channels!`) return interaction.reply({ embeds: [embedReply], @@ -63,7 +68,7 @@ module.exports = { const vcParent = await interaction.options.getChannel("vc-category"); if (!guildObj.channels.cache.has(vcParent.id)) { const embedReply = new EmbedBuilder() - .setColor('Red') + .setColor('#ED4245') .setDescription(`The Category does not belong to this server!`) return interaction.reply({ embeds: [embedReply], @@ -72,7 +77,7 @@ module.exports = { } if (vcParent.type !== ChannelType.GuildCategory) { const embedReply = new EmbedBuilder() - .setColor('Red') + .setColor('#ED4245') .setDescription(`Select only Category channels!`) return interaction.reply({ embeds: [embedReply], @@ -90,6 +95,7 @@ module.exports = { }); } + const vcLog = await interaction.options.getChannel("vc-log") || null; const vcName = await interaction.options.getString("vc-name"); if (!vcName) { @@ -97,7 +103,8 @@ module.exports = { guildID: interaction.guild.id, vcID: vcChan.id, parentID: vcParent.id, - name: null + name: null, + logID: vcLog.id }); await newData.save(); } else { @@ -105,14 +112,14 @@ module.exports = { guildID: interaction.guild.id, vcID: vcChan.id, parentID: vcParent.id, - name: vcName + name: vcName, + logID: vcLog.id }); await newData.save(); } - const embed = new EmbedBuilder() - .setColor('Green') + .setColor("#57F287") .setDescription(`VC Setup Successfull!`) .setTimestamp() return interaction.reply({ diff --git a/config/extras/glog.json b/config/extras/glog.json index 0e4735d..aea94a7 100644 --- a/config/extras/glog.json +++ b/config/extras/glog.json @@ -20,4 +20,4 @@ } } -"Note (Delete this after editing the config file)": "Get logs at your own risk. Spying on a specific user or a guild is against discord terms and conditions. You don't want logs, just ignore this update. Memory and CPU usage will be very high (still in beta version). Update will be released to reduced loads and API requests." \ No newline at end of file +"This will make the API request load even worse. Only use if required."" \ No newline at end of file diff --git a/config/extras/streamer.json b/config/extras/streamer.json deleted file mode 100644 index 5afca0d..0000000 --- a/config/extras/streamer.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "CHAN_ID": "", - "STATUS": "STREAMING", - "GUILD": "", - "ROLE": { - "STREAM": "", - "ELITEXPR": "", - "YOUTUBER": "" - }, - "UPDATE_INTERVAL": "20000" -} \ No newline at end of file diff --git a/config/extras/streaming.json b/config/extras/streaming.json new file mode 100644 index 0000000..ead5c29 --- /dev/null +++ b/config/extras/streaming.json @@ -0,0 +1,5 @@ +{ + "ROLEID": "", + "GUILDID": "", + "YTROLEID": "" +} \ No newline at end of file diff --git a/events/application/newvccreate.js b/events/application/newvccreate.js index 530918b..4d6dd06 100644 --- a/events/application/newvccreate.js +++ b/events/application/newvccreate.js @@ -1,3 +1,5 @@ +//Updated version with log system and bug fix + const { EmbedBuilder, Events, @@ -7,18 +9,58 @@ const { const vcCreateModel = require('../../events/models/vcCreate.js'); const vcCreateCount = require('../../events/models/vcCreateGuild.js'); +const mutex = { + _locked: false, + lock: () => { + return new Promise((resolve) => { + const tryLock = () => { + if (!mutex._locked) { + mutex._locked = true; + resolve(); + } else { + setTimeout(tryLock, 10); + } + }; + tryLock(); + }); + }, + unlock: () => { + mutex._locked = false; + }, +}; + +const cooldowns = new Set(); + module.exports = { name: Events.VoiceStateUpdate, execute: async (oldState, newState, client) => { + + async function EmbedLog(Color, Process, UserID, logChan) { + var logembed = new EmbedBuilder() + .setColor(Color) + .setAuthor({ name: `${Process}` }) + .setDescription(`User: <@${UserID}>`); + + if (logChan) { + await logChan.send({ embeds: [logembed] }); + } else { + console.error("logChan is undefined"); + } + } + let User = client.users.cache.get(newState.id); - const vcCreateCounts = await vcCreateCount.findOne({ - guildID: newState.guild.id - }).catch(err => console.log(err)); + const vcCreateCounts = await vcCreateCount + .findOne({ + guildID: newState.guild.id, + }) + .catch((err) => console.error(err)); - var vcCheckold = await vcCreateModel.findOne({ - guildID: oldState.guild.id, - }).catch(err => console.log(err)); + var vcCheckold = await vcCreateModel + .findOne({ + guildID: oldState.guild.id, + }) + .catch((err) => console.error(err)); var targetChannelId; if (vcCheckold) { @@ -27,56 +69,98 @@ module.exports = { if (targetChannelId && oldState.channel?.id === targetChannelId) { const channelToUpdate = oldState.guild.channels.cache.get(targetChannelId); - if (channelToUpdate && channelToUpdate.members.size === 0) { - await channelToUpdate.delete().then(async () => { - await vcCreateModel.findOneAndRemove({ - guildID: oldState.guild.id, - userID: oldState.id - }); - }); + if (vcCreateCounts && vcCreateCounts.logID) { + var logChan = client.channels.cache.get(vcCreateCounts.logID); + + if (channelToUpdate) { + await mutex.lock(); + + try { + const cooldownKey = `${oldState.guild.id}-${oldState.id}`; + if (cooldowns.has(cooldownKey) || channelToUpdate.members.size === 0) { + await vcCreateModel.findOneAndRemove({ + guildID: oldState.guild.id, + vcID: targetChannelId, + }); + + await EmbedLog('Red', 'Channel deleted', oldState.id, logChan); + + await channelToUpdate.delete().catch((err) => { + console.error(`Error deleting channel: ${err}`); + }); + + await vcCreateModel.findOneAndRemove({ + guildID: oldState.guild.id, + userID: oldState.id, + }); + cooldowns.add(cooldownKey); + setTimeout(() => { + cooldowns.delete(cooldownKey); + }, 5000); + } else { + //console.log(`Skipping channel deletion: ${channelToUpdate.name} (${channelToUpdate.id}) due to active members`); + } + } catch (err) { + console.error(err); + } finally { + mutex.unlock(); + } + } } } - if (vcCreateCounts) { + if (vcCreateCounts && vcCreateCounts.logID) { if (newState.channel?.id === vcCreateCounts.vcID) { var vcChecknew = await vcCreateModel.findOne({ guildID: newState.guild.id, userID: newState.id - }).catch(err => console.log(err)); - + }).catch(err => console.error(err)); + const newChannel = await newState.guild.channels.create({ - name: `${vcCreateCounts.name}${User.username}`, //🎤┆ + name: `${vcCreateCounts.name}${User.username}`, parent: vcCreateCounts.parentID, userLimit: 10, bitrate: 64000, type: ChannelType.GuildVoice }); - await newState.setChannel(newChannel).then(async (a) => { - - if (vcChecknew) { - await vcCreateModel.findOneAndRemove({ - guildID: newState.guild.id, - userID: newState.id - }); - - var vcAdd = await new vcCreateModel({ - guildID: newState.guild.id, - vcID: newState.channel.id, - userID: newState.id - }); - await vcAdd.save(); - - } else if (!vcChecknew) { - var vcAdd = await new vcCreateModel({ - guildID: newState.guild.id, - vcID: newState.channel.id, - userID: newState.id - }); - await vcAdd.save(); + if (newChannel) { + try { + var logChan = client.channels.cache.get(vcCreateCounts.logID); + await mutex.lock(); + + await newState.setChannel(newChannel); + + await EmbedLog('Green', 'Channel Created', newState.id, logChan); + + if (vcChecknew) { + await vcCreateModel.findOneAndRemove({ + guildID: newState.guild.id, + userID: newState.id + }); + + var vcAdd = new vcCreateModel({ + guildID: newState.guild.id, + vcID: newState.channel.id, + userID: newState.id + }); + await vcAdd.save(); + } else { + var vcAdd = new vcCreateModel({ + guildID: newState.guild.id, + vcID: newState.channel.id, + userID: newState.id + }); + await vcAdd.save(); + } + } catch (err) { + console.error(err); + await newChannel.delete(); + } finally { + mutex.unlock(); } - }); + } } } } -} +}; diff --git a/events/application/nickname.js b/events/application/nickname.js index d36075d..8395c82 100644 --- a/events/application/nickname.js +++ b/events/application/nickname.js @@ -8,47 +8,15 @@ module.exports = { async execute(client) { const roleId = client.nickname.ROLEID; + const guildId = client.nickname.GUILDID; - const options = [ + const guild = client.guilds.cache.find(guild => guild.id === guildId); + if (!guild) return console.log('No Guild'); - "!", - "\"", - "#", - "$", - "%", - "&", - "`", - "'", - "(", - ")", - "*", - "+", - ",", - "-", - ".", - "/", - ":", - ";", - "<", - "=", - ">", - "?", - "@", - "0", - "1", - "2", - "3", - "4", - "5", - "6", - "7", - "8", - "9", - "[", - "]", - "_" - ]; + const logChan = client.channels.cache.get(client.nickname.LOG); + const options = ["!", "\"", "#", "$", "%", "&", "`", "'", "(", ")", "*", "+", ",", "-", ".", "/", ":", ";", + "<", "=", ">", "?", "@", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "[", "]", "_"]; if (client.config.ENABLE.NICKNAME == true) { setInterval(() => { @@ -74,33 +42,40 @@ module.exports = { { name: 'User Name', value: `${memberInfo.user.username}#${memberInfo.user.discriminator}` }, { name: 'Nickname', value: `${nickName}` }, ); - logChan = client.channels.cache.get(client.nickname.LOG).send({ embeds: [logEmbed] }); + await logChan.send({ embeds: [logEmbed] }); } async function checkNicknames() { - - const guild = client.guilds.cache.find(guild => guild.id === guildId); - if (!guild) return; - - const roles = roleId.map(roleId => guild.roles.cache.find(role => role.id === roleId)); - if (roles.some(role => !role)) return; + const roles = roleId.map(roleId => guild.roles.cache.get(roleId)); + if (roles.filter(role => !role).length) return; for (const [memberId, member] of guild.members.cache) { - if (member.roles.cache.some(role => roles.includes(role))) { + if (!member.roles.cache.some(role => roles.includes(role))) { if (!member.nickname && member.user.username[0] && checkFirstLetterIsOption(member.user.username[0])) { - const newNickname = member.user.username.slice(1); - await member.setNickname(newNickname); + const newNickname = "~" + member.user.username.slice(1); + await member.setNickname(newNickname).catch(err => { + if (err.code === 50013) { + // handle permissions error + } else { + console.error(err); + } + }); await logEmbedFunc(memberId, member, newNickname); } if (member.displayName && checkFirstLetterIsOption(member.displayName[0])) { - - const newNickname = member.displayName.slice(1); - await member.setNickname(newNickname); + const newNickname = "~" + member.displayName.slice(1); + await member.setNickname(newNickname).catch(err => { + if (err.code === 50013) { + // handle permissions error + } else { + console.error(err); + } + }); await logEmbedFunc(memberId, member, newNickname); } } } } } -} +} \ No newline at end of file diff --git a/events/guild/interactionCreate.js b/events/guild/interactionCreate.js index 96697fd..697b9d1 100644 --- a/events/guild/interactionCreate.js +++ b/events/guild/interactionCreate.js @@ -12,7 +12,7 @@ module.exports = { execute: async (interaction, client) => { //if (!interaction.type === InteractionType.ApplicationCommand) return - const command = client.commands.get(interaction.commandName); + const command = client.slashCommands.get(interaction.commandName); if (!command) return; try { @@ -23,7 +23,7 @@ module.exports = { var coolMsg = client.config.MESSAGE["COOLDOWN_MESSAGE"].replace('', ms(cooldown.get(`${command.name}${interaction.user.id}`) - Date.now(), { long: true })); const coolEmbed = new EmbedBuilder() .setDescription(`${coolMsg}`) - .setColor('Red') + .setColor('#ED4245') return interaction.reply({ embeds: [coolEmbed], ephemeral: true }); } @@ -33,14 +33,14 @@ module.exports = { if (!interaction.member.permissions.has(PermissionsBitField.resolve(command.userPerms || []))) { const userPerms = new EmbedBuilder() .setDescription(`🚫 <@${interaction.user.id}>, You don't have \`${command.userPerms}\` permissions to use this command!`) - .setColor('Red') + .setColor('#ED4245') return interaction.reply({ embeds: [userPerms], ephemeral: true }); } if (!interaction.guild.members.cache.get(client.user.id).permissions.has(PermissionsBitField.resolve(command.botPerms || []))) { const botPerms = new EmbedBuilder() .setDescription(`🚫 <@${interaction.user.id}>, I don't have \`${command.botPerms}\` permissions to use this command!`) - .setColor('Red') + .setColor('#ED4245') return interaction.reply({ embeds: [botPerms], ephemeral: true }); } } @@ -66,14 +66,14 @@ module.exports = { if (!interaction.member.permissions.has(PermissionsBitField.resolve(command.userPerms || []))) { const userPerms = new EmbedBuilder() .setDescription(`🚫 <@${interaction.user.id}>, You don't have \`${command.userPerms}\` permissions to use this command!`) - .setColor('Red') + .setColor('#ED4245') return interaction.reply({ embeds: [userPerms], ephemeral: true }); } if (!interaction.guild.members.cache.get(client.user.id).permissions.has(PermissionsBitField.resolve(command.botPerms || []))) { const botPerms = new EmbedBuilder() .setDescription(`🚫 <@${interaction.user.id}>, I don't have \`${command.botPerms}\` permissions to use this command!`) - .setColor('Red') + .setColor('#ED4245') return interaction.reply({ embeds: [botPerms], ephemeral: true }); } } @@ -84,7 +84,7 @@ module.exports = { } catch (error) { global.console.log(error); const botErrorEmbed = new EmbedBuilder() - .setColor('Red') + .setColor('#ED4245') .setDescription('An Internal **Error** Occurred, Kindly Contact The Bot Developers!') return interaction.reply({ embeds: [botErrorEmbed], diff --git a/events/guild/messageCreate.js b/events/guild/messageCreate.js index 377086c..85438dc 100644 --- a/events/guild/messageCreate.js +++ b/events/guild/messageCreate.js @@ -16,8 +16,13 @@ module.exports = { "Raise a ticket, we are happy to help you!", "At your service ❤️", "Yes Sir!", - "Official EliteX Roleplay Discord Bot" - ]; + "How may I assist you?", + "I'm here to help. What can I do for you?", + "Good day! How can I be of service?", + "I'm all ears. What do you need help with?", + "Hello! What can I do for you today?" + ]; + const randomIndex = Math.floor(Math.random() * (botReply.length - 1) + 1); const newActivity = botReply[randomIndex]; @@ -29,7 +34,7 @@ module.exports = { const prefix = client.config.INFO.PREFIX; if (message.author.bot) return; - if (message.channel.type !== 0) return; + //if (message.channel.type !== 0) return; //restricts message commands to text channels only (no annoucement type channels) if (!message.content.startsWith(prefix)) return; const args = message.content.slice(prefix.length).trim().split(/ +/g); @@ -38,20 +43,11 @@ module.exports = { try { - let command = client.commands.get(cmd) - - if (command == null) { - const replyEmbed = new EmbedBuilder() - .setColor('Red') - .setDescription('Invalid Command') - return message.channel.send({ embeds: [replyEmbed] }).then((msg) => { - setTimeout(function () { - msg.delete() - }, 4000); - }); - } + let command = client.messageCommands.get(cmd); - if (!command) command = client.commands.get(client.aliases.get(cmd)); + if (command == null) return; + + if (!command) command = client.messageCommands.get(client.aliases.get(cmd)); if (command) { if (command.cooldown) { @@ -59,10 +55,10 @@ module.exports = { var coolMsg = client.config.MESSAGE["COOLDOWN_MESSAGE"].replace('', ms(cooldown.get(`${command.name}${message.author.id}`) - Date.now(), { long: true })); const coolEmbed = new EmbedBuilder() .setDescription(`${coolMsg}`) - .setColor('Red') + .setColor('#ED4245'); return message.channel.send({ embeds: [coolEmbed] }).then((msg) => { setTimeout(function () { - msg.delete() + msg.delete(); }, 4000); }); } @@ -71,10 +67,10 @@ module.exports = { if (!message.member.permissions.has(PermissionsBitField.resolve(command.userPerms || []))) { const userPerms = new EmbedBuilder() .setDescription(`🚫 ${message.author}, You don't have \`${command.userPerms}\` permissions to use this command!`) - .setColor('Red') + .setColor('#ED4245'); return message.reply({ embeds: [userPerms] }).then((msg) => { setTimeout(function () { - msg.delete() + msg.delete(); }, 4000); }); } @@ -82,19 +78,19 @@ module.exports = { if (!message.guild.members.cache.get(client.user.id).permissions.has(PermissionsBitField.resolve(command.botPerms || []))) { const botPerms = new EmbedBuilder() .setDescription(`🚫 ${message.author}, I don't have \`${command.botPerms}\` permissions to use this command!`) - .setColor('Red') + .setColor('#ED4245'); return message.reply({ embeds: [botPerms] }).then((msg) => { setTimeout(function () { - msg.delete() + msg.delete(); }, 4000); }); } } command.run(client, message, args); - cooldown.set(`${command.name}${message.author.id}`, Date.now() + command.cooldown) + cooldown.set(`${command.name}${message.author.id}`, Date.now() + command.cooldown); setTimeout(() => { - cooldown.delete(`${command.name}${message.author.id}`) + cooldown.delete(`${command.name}${message.author.id}`); }, command.cooldown); } else { @@ -102,10 +98,10 @@ module.exports = { if (!message.member.permissions.has(PermissionsBitField.resolve(command.userPerms || []))) { const userPerms = new EmbedBuilder() .setDescription(`🚫 ${message.author}, You don't have \`${command.userPerms}\` permissions to use this command!`) - .setColor('Red') + .setColor('#ED4245'); return message.reply({ embeds: [userPerms] }).then((msg) => { setTimeout(function () { - msg.delete() + msg.delete(); }, 4000); }); } @@ -113,10 +109,10 @@ module.exports = { if (!message.guild.members.cache.get(client.user.id).permissions.has(PermissionsBitField.resolve(command.botPerms || []))) { const botPerms = new EmbedBuilder() .setDescription(`🚫 ${message.author}, I don't have \`${command.botPerms}\` permissions to use this command!`) - .setColor('Red') + .setColor('#ED4245'); return message.reply({ embeds: [botPerms] }).then((msg) => { setTimeout(function () { - msg.delete() + msg.delete(); }, 4000); }); } @@ -127,14 +123,14 @@ module.exports = { } catch (error) { global.console.log(error); const botErrorEmbed = new EmbedBuilder() - .setColor('Red') - .setDescription('An Internal **Error** Occurred, Kindly Contact The Bot Developers!') + .setColor('#ED4245') + .setDescription('An Internal **Error** Occurred, Kindly Contact The Bot Developers!'); return message.reply({ embeds: [botErrorEmbed], ephemeral: true }).then((msg) => { setTimeout(function () { - msg.delete() + msg.delete(); }, 4000); }); } diff --git a/events/models/vcCreateGuild.js b/events/models/vcCreateGuild.js index 1646d5e..b1e6211 100644 --- a/events/models/vcCreateGuild.js +++ b/events/models/vcCreateGuild.js @@ -5,7 +5,8 @@ const vcCreateGuild = Schema({ vcID: String, parentID: String, name: String, - vcCreateCount: Number + vcCreateCount: Number, + logID: String }); module.exports = model('vccreate-guild', vcCreateGuild); \ No newline at end of file diff --git a/events/streamer/streamer.js b/events/streamer/streamer.js index f2a3e60..5d4d4d2 100644 --- a/events/streamer/streamer.js +++ b/events/streamer/streamer.js @@ -1,101 +1,41 @@ +//Updated version with reduced API load and bug fix + const { - EmbedBuilder, - Events + Events, + ActivityType } = require('discord.js'); -//This is not actually you do it! We must use PresenceUpdate event and it is more efficient than this -//If you know how to do, you are most welcomed! - module.exports = { - name: Events.ClientReady, - async execute(client) { - - if (client.config.ENABLE.STREAMER == true) { - - //Config Files - const YTRoleId = client.streamer.ROLE.YOUTUBER; - const GuildId = client.streamer.GUILD; - const Interval = client.streamer.UPDATE_INTERVAL; - const EliteXRP = client.streamer.ROLE.ELITEXPR; - - //Loggin Channel - const ERROR_CHAN = client.streamer.CHAN_ID; - const err_log = client.channels.cache.get(ERROR_CHAN); - - //auto check after 20 secs - var GuildInfo, YTRole, Role_Total, PRRole, YT_Total, PR_Total, Role_Total; - - GuildInfo = await client.guilds.cache.get(GuildId); - await GuildInfo.members.fetch() - - setInterval(async () => { - YTRole = await GuildInfo.roles.cache.find(role => role.id == YTRoleId); - PRRole = await GuildInfo.roles.cache.find(role => role.id == EliteXRP); - - YT_Total = await YTRole.members.map(m => m); - PR_Total = await PRRole.members.map(m => m); - - return Role_Total = PR_Total.filter((element) => { - return YT_Total.includes(element); - }); - }, Interval); - - //Gives Streamer role when STREAMING - try { - async function YTUsers(Role) { - - await Role.forEach(async (User) => { - if (User.presence !== null) { - - var Bool = false; - await User.presence.activities.forEach(async (activity) => { - if (activity.type == 1) { - return Bool = true; - } - }); - - //Add role or remove - if (Bool == true) { - const StreamRoleId = client.streamer.ROLE.STREAM; - if (User.roles.cache?.has(StreamRoleId)) { - return; - } else { - let streamRole = await GuildInfo.roles.cache.get(StreamRoleId); - await User.roles.add(streamRole).catch(err => { - console.log(`Streamer Add role Error || ${err}`); - }); - var embed_log = new EmbedBuilder() - .setDescription("**STREAMER ROLE ADDED**") - .setFields( - { name: "User Tag", value: `<@${User.id}>` }, - { name: "User Name", value: `\`${User.user.tag}\`` }, - { name: "User ID", value: `\`${User.id}\`` } - ) - err_log.send({ embeds: [embed_log] }); - } - - } else if (Bool == false) { - const StreamRoleId = client.streamer.ROLE.STREAM; - let streamRole = await GuildInfo.roles.cache.get(StreamRoleId); - await User.roles.remove(streamRole).catch(err => { - console.log(`Streamer Remove role Error ${err}`); - }); - - } else { - err_log.send({ content: `**ERROR**\nReason: Bool is neither true nor false` });; - } - } - }); + name: Events.PresenceUpdate, + async execute(oldMember, newMember, client) { + + if (client.config.ENABLE.STREAM == true) { + + const streamingRoleID = client.streaming.ROLEID; + const member = newMember.member; + if (member.user.bot) return; + + if (member.roles.cache.has(client.streaming.YTROLEID)) { + if (newMember.guild.id == client.streaming.GUILDID) { + + const nowStreamingRole = newMember.guild.roles.cache.find(role => role.id === streamingRoleID); + const isStreamingOnYouTube = newMember.activities.some(activity => + activity.type === ActivityType.Streaming && activity.name.toLowerCase().includes('youtube') + ); + + if (isStreamingOnYouTube) { + if (member.roles.cache.has(streamingRoleID)) return; + member.roles.add(nowStreamingRole) + .then(() => console.log(`Assigned "Now Streaming" role to ${member.user.tag}`)) + .catch(console.error); + + } else { + if (!member.roles.cache.has(streamingRoleID)) return; + member.roles.remove(nowStreamingRole) + .then(() => console.log(`Removed "Now Streaming" role from ${member.user.tag}`)) + .catch(console.error); + } } - - //Call function in a loop (every 20 seconds) - setInterval(async () => { - await YTUsers(Role_Total) - }, Interval); - - } catch (err) { - //Error catch - console.log(err); } } } diff --git a/handlers/commands/commands.js b/handlers/commands/commands.js index 2f1b25c..540d35f 100644 --- a/handlers/commands/commands.js +++ b/handlers/commands/commands.js @@ -1,27 +1,32 @@ const fs = require('fs'); const { - Events + Events, + Collection } = require('discord.js'); +const messageCommands = new Collection(); +const slashCommands = new Collection(); + module.exports = { name: Events.ClientReady, execute(client) { - //slashCommands Read - const slashcommandFiles = fs.readdirSync('./commands/slash').filter(file => file.endsWith('.js')); + const messageCommandFiles = fs.readdirSync('./commands/message').filter(file => file.endsWith('.js')); - for (const file of slashcommandFiles) { - const command = require(`../../commands/slash/${file}`); - client.commands.set(command.data.name, command); - }; + for (const file of messageCommandFiles) { + const command = require(`../../commands/message/${file}`); + messageCommands.set(command.name, command); + } - //messageCommands Read - const messagecommandFiles = fs.readdirSync('./commands/message').filter(file => file.endsWith('.js')); + const slashCommandFiles = fs.readdirSync('./commands/slash').filter(file => file.endsWith('.js')); - for (const file of messagecommandFiles) { - const command = require(`../../commands/message/${file}`); - client.commands.set(command.name, command); - }; + for (const file of slashCommandFiles) { + const command = require(`../../commands/slash/${file}`); + slashCommands.set(command.data.name, command); + } + + client.messageCommands = messageCommands; + client.slashCommands = slashCommands; } } \ No newline at end of file