Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slash Command - List Members in a Role #8

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
59 changes: 59 additions & 0 deletions commands/utility/list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//*Requires "PRESENCE INTENT" in the Discord Developer Portal to be enabled for full functionality.

const { SlashCommandBuilder } = require("discord.js");

module.exports = {
data: new SlashCommandBuilder()
.setName("list")
.setDescription("Lists some members of the CN discord based on their role.")
.addStringOption(option =>
option.setName("role")
.setDescription("Enter the role name.")
.setAutocomplete(true)
.setRequired(true)),

async autocomplete(interaction) {
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 })),
);
},

async execute(interaction) {
//VARIABLES
let memberArray = [];
let stringLength;
let role = interaction.options.get("role");
let memberPersons = "Members with the \"" + role.value + "\" role: ";

//Enables auto-complete of roles in command.
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 += ", ";
});

//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.";
console.log(errorMessage);
await interaction.reply(errorMessage);
return
}

await interaction.reply(memberPersons);
},
};
36 changes: 36 additions & 0 deletions commands/utility/search.js
Original file line number Diff line number Diff line change
@@ -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);
},
};
33 changes: 19 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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] });
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildPresences] });

client.commands = new Collection();

Expand All @@ -31,23 +31,28 @@ 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()) {
if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}

await command.autocomplete(interaction);
}
});

Expand Down