From 8caea0ccd8a6cd54153bd6e641bcd46d97ddd784 Mon Sep 17 00:00:00 2001 From: ArcingFirehawk Date: Mon, 5 Aug 2024 17:59:41 +1000 Subject: [PATCH 01/11] First commit. --- commands/utility/list.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 commands/utility/list.js diff --git a/commands/utility/list.js b/commands/utility/list.js new file mode 100644 index 0000000..343b015 --- /dev/null +++ b/commands/utility/list.js @@ -0,0 +1,21 @@ +const { SlashCommandBuilder } = require('discord.js'); + +module.exports = { + data: new SlashCommandBuilder() + .setName('list') + .setDescription('Lists all of the CN executives.'), + async execute(interaction) { + let memberArray = interaction.guild.roles.cache.get('1267402048580620378').members.map(m=>m.user.tag); + console.log(memberArray); + + let memberPersons = ""; + + memberArray.forEach(element => { + console.log(element.toString()); + memberPersons +=element.toString(); + memberPersons += " "; + }); + + await interaction.reply(memberPersons); + }, +}; \ No newline at end of file From ea7bc647adcc07afddb6f3fb2e033a06f435609e Mon Sep 17 00:00:00 2001 From: ArcingFirehawk Date: Mon, 5 Aug 2024 19:00:56 +1000 Subject: [PATCH 02/11] Added basic error handling. --- commands/utility/list.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/commands/utility/list.js b/commands/utility/list.js index 343b015..697da40 100644 --- a/commands/utility/list.js +++ b/commands/utility/list.js @@ -5,17 +5,23 @@ module.exports = { .setName('list') .setDescription('Lists all of the CN executives.'), async execute(interaction) { - let memberArray = interaction.guild.roles.cache.get('1267402048580620378').members.map(m=>m.user.tag); - console.log(memberArray); + let memberArray = interaction.guild.roles.cache.get('1267402048580620378').members.map(m=>m.displayName); let memberPersons = ""; - memberArray.forEach(element => { - console.log(element.toString()); + memberArray.forEach(element => + { memberPersons +=element.toString(); memberPersons += " "; }); + if (memberArray.length == 0) { + let errorMessage = "ERROR. Server members not found."; + console.log(errorMessage); + await interaction.reply(errorMessage); + return + } + await interaction.reply(memberPersons); }, }; \ No newline at end of file From 85e1dfc8843743982c0bab754f2cadb251528e9c Mon Sep 17 00:00:00 2001 From: ArcingFirehawk Date: Mon, 5 Aug 2024 19:30:02 +1000 Subject: [PATCH 03/11] Added options to the list command. --- commands/utility/list.js | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/commands/utility/list.js b/commands/utility/list.js index 697da40..6bf6ec7 100644 --- a/commands/utility/list.js +++ b/commands/utility/list.js @@ -1,14 +1,33 @@ -const { SlashCommandBuilder } = require('discord.js'); +const { SlashCommandBuilder } = require("discord.js"); module.exports = { data: new SlashCommandBuilder() - .setName('list') - .setDescription('Lists all of the CN executives.'), - async execute(interaction) { - let memberArray = interaction.guild.roles.cache.get('1267402048580620378').members.map(m=>m.displayName); + .setName("list") + .setDescription("Lists members of the CN discord based on their role.") + .addStringOption(option => + option.setName("role") + .setDescription("Options: \"execs\", \"mods\", and \"vets\.") + .setRequired(true)), + async execute(interaction) { + //VARIABLES + let memberArray = []; + let role = interaction.options.get("role"); let memberPersons = ""; + switch (role.value.toString()) + { + case "execs": + memberArray = interaction.guild.roles.cache.get("1267402048580620378").members.map(m=>m.displayName); //CN Veterals Role ID: 349872786241617920 + break; + case "mods": + memberArray = interaction.guild.roles.cache.get("1269948580022452275").members.map(m=>m.displayName); //CN Veterals Role ID: 349872786241617920 + break; + case "vets": + memberArray = interaction.guild.roles.cache.get("1269945736993181780").members.map(m=>m.displayName); //CN Veterals Role ID: 349872786241617920 + break; + } + memberArray.forEach(element => { memberPersons +=element.toString(); From 2b6de510a3faeee08834089c9c12debd1eca8852 Mon Sep 17 00:00:00 2001 From: ArcingFirehawk Date: Mon, 19 Aug 2024 18:29:29 +1000 Subject: [PATCH 04/11] Added basic security features. --- commands/utility/list.js | 21 +++++++++++++-------- index.js | 2 +- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/commands/utility/list.js b/commands/utility/list.js index 6bf6ec7..bad2a83 100644 --- a/commands/utility/list.js +++ b/commands/utility/list.js @@ -3,10 +3,10 @@ const { SlashCommandBuilder } = require("discord.js"); module.exports = { data: new SlashCommandBuilder() .setName("list") - .setDescription("Lists members of the CN discord based on their role.") + .setDescription("Lists some members of the CN discord based on their role.") .addStringOption(option => option.setName("role") - .setDescription("Options: \"execs\", \"mods\", and \"vets\.") + .setDescription("Options: \"execs\", \"mods\", and \"vets\".") .setRequired(true)), async execute(interaction) { @@ -14,28 +14,33 @@ module.exports = { let memberArray = []; let role = interaction.options.get("role"); let memberPersons = ""; - - switch (role.value.toString()) + + //switch statement that decides what role to get based on input. + switch (role.value.toString().toLowerCase()) { case "execs": - memberArray = interaction.guild.roles.cache.get("1267402048580620378").members.map(m=>m.displayName); //CN Veterals Role ID: 349872786241617920 + memberArray = interaction.guild.roles.cache.get("1267402048580620378").members.map(m=>m.displayName); //CN executive role ID: 349872786241617920 break; case "mods": - memberArray = interaction.guild.roles.cache.get("1269948580022452275").members.map(m=>m.displayName); //CN Veterals Role ID: 349872786241617920 + memberArray = interaction.guild.roles.cache.get("1269948580022452275").members.map(m=>m.displayName); //CN moderators role ID: 349872786241617920 break; case "vets": - memberArray = interaction.guild.roles.cache.get("1269945736993181780").members.map(m=>m.displayName); //CN Veterals Role ID: 349872786241617920 + memberArray = interaction.guild.roles.cache.get("1269945736993181780").members.map(m=>m.displayName); //CN veterans role ID: 349872786241617920 + break; + default: break; } + //for-each statement that converts the array of server members into a string. memberArray.forEach(element => { memberPersons +=element.toString(); memberPersons += " "; }); + //if statement for error handling. if (memberArray.length == 0) { - let errorMessage = "ERROR. Server members not found."; + let errorMessage = "ERROR. Please try again."; console.log(errorMessage); await interaction.reply(errorMessage); return diff --git a/index.js b/index.js index d2ff291..c265a0c 100644 --- a/index.js +++ b/index.js @@ -8,7 +8,7 @@ const clientId = process.env.CLIENT_ID; const guildId = process.env.GUILD_ID; // Create a new client instance -const client = new Client({ intents: [GatewayIntentBits.Guilds] }); +const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildPresences] }); client.commands = new Collection(); From 34f26c283a0495783518545684b84be5d68c8ecd Mon Sep 17 00:00:00 2001 From: ArcingFirehawk Date: Mon, 19 Aug 2024 19:59:03 +1000 Subject: [PATCH 05/11] Added command autocomplete. --- commands/utility/list.js | 12 ++++++++++++ index.js | 39 ++++++++++++++++++++++++++------------- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/commands/utility/list.js b/commands/utility/list.js index bad2a83..e97ad4a 100644 --- a/commands/utility/list.js +++ b/commands/utility/list.js @@ -1,3 +1,5 @@ +//*Requires "PRESENCE INTENT" in the Discord Developer Portal to be enabled for full functionality. + const { SlashCommandBuilder } = require("discord.js"); module.exports = { @@ -7,7 +9,17 @@ module.exports = { .addStringOption(option => option.setName("role") .setDescription("Options: \"execs\", \"mods\", and \"vets\".") + .setAutocomplete(true) .setRequired(true)), + + async autocomplete(interaction) { + const focusedValue = interaction.options.getFocused(); + const choices = ["execs", "mods", "vets"]; + const filtered = choices.filter(choice => choice.startsWith(focusedValue)); + await interaction.respond( + filtered.map(choice => ({ name: choice, value: choice })), + ); + }, async execute(interaction) { //VARIABLES diff --git a/index.js b/index.js index c265a0c..189d161 100644 --- a/index.js +++ b/index.js @@ -31,24 +31,37 @@ for (const folder of commandFolders) { } client.on(Events.InteractionCreate, async interaction => { - if (!interaction.isChatInputCommand()) return; const command = interaction.client.commands.get(interaction.commandName); - - if (!command) { - console.error(`No command matching ${interaction.commandName} was found.`); - return; + if (interaction.isChatInputCommand()) { + try { + await command.execute(interaction); + } catch (error) { + console.error(error); + if (interaction.replied || interaction.deferred) { + await interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true }); + } else { + await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true }); + } + } } - try { - await command.execute(interaction); - } catch (error) { - console.error(error); - if (interaction.replied || interaction.deferred) { - await interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true }); - } else { - await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true }); + //Command autocomplete handling. + else if (interaction.isAutocomplete()) { + //const command = client.commands.get(interaction.commandName); + + if (!command) { + console.error(`No command matching ${interaction.commandName} was found.`); + return; } + + await command.autocomplete(interaction); } + + // + + + + }); // When the client is ready, run this code (only once). From 89ba02f31098f6ad6226ebcad5387c25dbd07acc Mon Sep 17 00:00:00 2001 From: ArcingFirehawk Date: Mon, 19 Aug 2024 20:00:58 +1000 Subject: [PATCH 06/11] Post autocomplete cleaning. --- index.js | 8 -------- 1 file changed, 8 deletions(-) diff --git a/index.js b/index.js index 189d161..7d6de6d 100644 --- a/index.js +++ b/index.js @@ -47,8 +47,6 @@ client.on(Events.InteractionCreate, async interaction => { //Command autocomplete handling. else if (interaction.isAutocomplete()) { - //const command = client.commands.get(interaction.commandName); - if (!command) { console.error(`No command matching ${interaction.commandName} was found.`); return; @@ -56,12 +54,6 @@ client.on(Events.InteractionCreate, async interaction => { await command.autocomplete(interaction); } - - // - - - - }); // When the client is ready, run this code (only once). From 099115f852c0776dca50ea75e7e5d8cafd8d0f11 Mon Sep 17 00:00:00 2001 From: ArcingFirehawk Date: Mon, 26 Aug 2024 17:34:18 +1000 Subject: [PATCH 07/11] Updated roles. --- commands/utility/list.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/commands/utility/list.js b/commands/utility/list.js index e97ad4a..478e604 100644 --- a/commands/utility/list.js +++ b/commands/utility/list.js @@ -31,13 +31,13 @@ module.exports = { switch (role.value.toString().toLowerCase()) { case "execs": - memberArray = interaction.guild.roles.cache.get("1267402048580620378").members.map(m=>m.displayName); //CN executive role ID: 349872786241617920 + memberArray = interaction.guild.roles.cache.get("349872786241617920").members.map(m=>m.displayName); break; case "mods": - memberArray = interaction.guild.roles.cache.get("1269948580022452275").members.map(m=>m.displayName); //CN moderators role ID: 349872786241617920 + memberArray = interaction.guild.roles.cache.get("349872786241617920").members.map(m=>m.displayName); break; case "vets": - memberArray = interaction.guild.roles.cache.get("1269945736993181780").members.map(m=>m.displayName); //CN veterans role ID: 349872786241617920 + memberArray = interaction.guild.roles.cache.get("349872786241617920").members.map(m=>m.displayName); break; default: break; From 6ff231dd8411be3dcba4fa19d8880a1c74fd2354 Mon Sep 17 00:00:00 2001 From: ArcingFirehawk Date: Mon, 26 Aug 2024 18:33:52 +1000 Subject: [PATCH 08/11] Added ability to get roles from sever. --- commands/utility/list.js | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/commands/utility/list.js b/commands/utility/list.js index 478e604..73ec8a6 100644 --- a/commands/utility/list.js +++ b/commands/utility/list.js @@ -13,9 +13,13 @@ module.exports = { .setRequired(true)), async autocomplete(interaction) { - const focusedValue = interaction.options.getFocused(); - const choices = ["execs", "mods", "vets"]; - const filtered = choices.filter(choice => choice.startsWith(focusedValue)); + const focusedValue = interaction.options.getFocused().toLowerCase(); + + //Get roles from server. + const choices = Array.from(interaction.guild.roles.cache.map(m=>m.name)); + + //Setup role list for auto-complete. + const filtered = choices.filter(choice => choice.toLowerCase().startsWith(focusedValue)); await interaction.respond( filtered.map(choice => ({ name: choice, value: choice })), ); @@ -26,22 +30,9 @@ module.exports = { let memberArray = []; let role = interaction.options.get("role"); let memberPersons = ""; - - //switch statement that decides what role to get based on input. - switch (role.value.toString().toLowerCase()) - { - case "execs": - memberArray = interaction.guild.roles.cache.get("349872786241617920").members.map(m=>m.displayName); - break; - case "mods": - memberArray = interaction.guild.roles.cache.get("349872786241617920").members.map(m=>m.displayName); - break; - case "vets": - memberArray = interaction.guild.roles.cache.get("349872786241617920").members.map(m=>m.displayName); - break; - default: - break; - } + + //Enables auto-complete of roles in command. + memberArray = interaction.guild.roles.cache.find(r=>r.name == role.value.toString()).members.map(m=>m.displayName); //for-each statement that converts the array of server members into a string. memberArray.forEach(element => From 91d3ef06b5a03b8e54d68817766a01c12e647128 Mon Sep 17 00:00:00 2001 From: ArcingFirehawk Date: Mon, 26 Aug 2024 19:19:53 +1000 Subject: [PATCH 09/11] Added ability to get roles from server. --- commands/utility/list.js | 13 +++++++++---- index.js | 2 +- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/commands/utility/list.js b/commands/utility/list.js index 73ec8a6..dc4fc62 100644 --- a/commands/utility/list.js +++ b/commands/utility/list.js @@ -8,7 +8,7 @@ module.exports = { .setDescription("Lists some members of the CN discord based on their role.") .addStringOption(option => option.setName("role") - .setDescription("Options: \"execs\", \"mods\", and \"vets\".") + .setDescription("Enter the role name.") .setAutocomplete(true) .setRequired(true)), @@ -28,19 +28,24 @@ module.exports = { async execute(interaction) { //VARIABLES let memberArray = []; + let stringLength; let role = interaction.options.get("role"); - let memberPersons = ""; + let memberPersons = "Members with the \"" + role.value + "\" role: "; //Enables auto-complete of roles in command. - memberArray = interaction.guild.roles.cache.find(r=>r.name == role.value.toString()).members.map(m=>m.displayName); + memberArray = interaction.guild.roles.cache.find(r=>r.name.toLowerCase() == role.value.toString().toLowerCase()).members.map(m=>m.displayName); //for-each statement that converts the array of server members into a string. memberArray.forEach(element => { memberPersons +=element.toString(); - memberPersons += " "; + memberPersons += ", "; }); + //End of string formatting. + memberPersons = memberPersons.slice(0, -2); + memberPersons += "."; + //if statement for error handling. if (memberArray.length == 0) { let errorMessage = "ERROR. Please try again."; diff --git a/index.js b/index.js index 2af252b..6491b32 100644 --- a/index.js +++ b/index.js @@ -8,7 +8,7 @@ const clientId = process.env.CLIENT_ID; const guildId = process.env.GUILD_ID; // Create a new client instance -const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildPresences] }); +const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildPresences] }); client.commands = new Collection(); From d5c80a72a5dc3bcbcd743df34b29d147454abce7 Mon Sep 17 00:00:00 2001 From: ArcingFirehawk Date: Mon, 26 Aug 2024 19:33:08 +1000 Subject: [PATCH 10/11] Fixed issue with intent. --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 6491b32..2af252b 100644 --- a/index.js +++ b/index.js @@ -8,7 +8,7 @@ const clientId = process.env.CLIENT_ID; const guildId = process.env.GUILD_ID; // Create a new client instance -const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildPresences] }); +const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildPresences] }); client.commands = new Collection(); From b306d78679eb5aab3a9bff4fd4a906f3115fa71e Mon Sep 17 00:00:00 2001 From: imei kaku Date: Mon, 2 Sep 2024 19:14:07 +1000 Subject: [PATCH 11/11] added member searching function. --- commands/utility/search.js | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 commands/utility/search.js diff --git a/commands/utility/search.js b/commands/utility/search.js new file mode 100644 index 0000000..07cbbed --- /dev/null +++ b/commands/utility/search.js @@ -0,0 +1,36 @@ +const { SlashCommandBuilder } = require('discord.js'); + +module.exports = { + data: new SlashCommandBuilder() + .setName('search') + .setDescription('search for user') + + + .addStringOption(option => + option.setName("id").setDescription("user id").setAutocomplete(true).setRequired(true) + ), + async autocomplete(interaction) { + const focusedValue = interaction.options.getFocused().toLowerCase(); + + //Get roles from server. + const choices = Array.from(interaction.guild.members.cache.map(m => m.user)); + + //Setup role list for auto-complete. + const filtered = choices.filter(choice => choice.displayName.startsWith(focusedValue)); + await interaction.respond( + filtered.map(choice => ({ name: choice.displayName, value: choice.id })), + ); + }, + + async execute(interaction) { + const id = interaction.options.getString("id"); + const member = interaction.guild.members.cache.get(id) + if (!member) { + await interaction.reply("No member found."); + return; + } + const roles = member.roles.cache.map(r => r.name).join(', ') + + await interaction.reply(roles); + }, +};