I'm making a command called role info in slash command! And came the error called: Cannot read properties of undefined (reading 'permissions').
const { MessageEmbed, MessageActionRow, MessageButton } = require("discord.js");
const moment = require("moment");
moment.locale("pt-BR");
module.exports = {
name: "role",
description: "「🙋│ Utilidades」 Obtenha informações de um cargo",
options: [
{
name: 'info',
description: "「🙋│ Utilidades」 Obtenha informações de um cargo",
type: "SUB_COMMAND",
options: [
{
name: "role",
type: "ROLE",
description: "O cargo que você deseja obter as informações!",
required: true,
}]
},
],
run: async (client, interaction, args) => {
const role = interaction.guild.roles.cache.get(args[0]);
const permissions = {
"ADMINISTRATOR": "Administrador",
"VIEW_AUDIT_LOG": "Ver Registro de Auditoria",
"VIEW_GUILD_INSIGHTS": "Exibir insights do servidor",
"MANAGE_GUILD": "Gerenciar Servidor",
"MANAGE_ROLES": "Gerenciar Cargos",
"MANAGE_CHANNELS": "Gerenciar Canais",
"KICK_MEMBERS": "Expulsar Membros",
"BAN_MEMBERS": "Banir Membros",
"CREATE_INSTANT_INVITE": "Criar convite",
"CHANGE_NICKNAME": "Mudar apelido",
"MANAGE_NICKNAMES": "Gerenciar apelidos",
"MANAGE_EMOJIS": "Gerenciar Emojis",
"MANAGE_WEBHOOKS": "Gerenciar webhooks",
"VIEW_CHANNEL": "Ler canais de texto e ver canais de voz",
"SEND_MESSAGES": "Enviar mensagens",
"SEND_TTS_MESSAGES": "Enviar mensagens TTS",
"MANAGE_MESSAGES": "Gerenciar mensagens",
"EMBED_LINKS": "Embed Links",
"ATTACH_FILES": "Anexar arquivos",
"READ_MESSAGE_HISTORY": "Leia o histórico da mensagem",
"MENTION_EVERYONE": "Mencione @everyone, @here e Todos os cargos",
"USE_EXTERNAL_EMOJIS": "Usar Emojis Externos",
"ADD_REACTIONS": "Adicionar Reações",
"CONNECT": "Conectar",
"SPEAK": "Falar",
"STREAM": "Video",
"MUTE_MEMBERS": "Mutar Membros",
"DEAFEN_MEMBERS": "Ensurdecer membros",
"MOVE_MEMBERS": "Mover membros",
"USE_VAD": "Usar atividade de voz",
"PRIORITY_SPEAKER": "Orador prioritário"
}
const yesno = {
true: '`Sim`',
false: '`Não`'
}
const rolePermissions = role.permissions.toArray();
const finalPermissions = [];
for (const permission in permissions) {
if (rolePermissions.includes(permission)) finalPermissions.push(`${client.emoji.success} ${permissions[permission]}`);
else finalPermissions.push(`${client.emoji.fail} ${permissions[permission]}`);
}
const position = `\`${interaction.guild.roles.cache.size - role.position}°\``;
const embed = new MessageEmbed()
.setTitle(`${role.name}`)
.addField(`${client.emoji.id} ID`, `\`${role.id}\``, true)
.addField(`${client.emoji.trophy} Posição`, `${position}`, true)
.addField(`${client.emoji.ping} Mencionável`, yesno[role.mentionable], true)
.addField(`${client.emoji.bot} Cargo de bot`, yesno[role.managed], true)
.addField(`${client.emoji.on} Visível`, yesno[role.hoist], true)
.addField(`${client.emoji.rcolor} Cor`, `\`${role.hexColor.toUpperCase()}\``, true)
.addField(`${client.emoji.calendar} Data de criação`, `${moment(role.createdAt).format('LLL')}(${moment(role.createdAt).startOf('day').fromNow()})`, true)
.setColor(`${role.hexColor.toUpperCase()}`)
const row = new MessageActionRow()
.addComponents(
new MessageButton()
.setLabel(`Permissões`)
.setEmoji(`${client.emoji.perms}`)
.setCustomId('perms') .setStyle('SECONDARY')
);
const m = await interaction.followUp({ embeds: [embed], components: [row] })
const iFilter = i => i.user.id === interaction.user.id;
const collector = m.createMessageComponentCollector({ filter: iFilter, time: 10 * 60000 });
collector.on('collect', async(i) => {
switch (i.customId) { //começo da mensagem
case `perms`:
i.reply({
embeds: [
new MessageEmbed()
.setTitle(`${client.emoji.perms} Permissões`)
.setDescription(`${finalPermissions.join('\n')}
`)
.setColor(`${role.hexColor.toUpperCase()}`)
],
ephemeral: true })
}
}) //Final da mensagem
}
}
Since before, the command was working, but I changed it to the second bot and then it gave the error! If anyone knows, tell me the error, I would be very grateful! TypeError: Cannot read properties of undefine d (reading 'permissions') at Object.run (/home/runner//Slash/Info/roleinfo.js:66:38) at Client. (/home/runner//events/interactionCreate:24:13
InteractionCreate.js
const client = require("../index");
client.on("interactionCreate", async (interaction) => {
// Slash Command Handling
if (interaction.isCommand()) {
await interaction.deferReply({ ephemeral: false }).catch(() => {});
const cmd = client.slash.get(interaction.commandName);
if (!cmd)
return interaction.followUp({ content: "An error has occured " });
const args = [];
for (let option of interaction.options.data) {
if (option.type === "SUB_COMMAND") {
if (option.name) args.push(option.name);
option.options?.forEach((x) => {
if (x.value) args.push(x.value);
});
} else if (option.value) args.push(option.value);
}
interaction.member = interaction.guild.members.cache.get(interaction.user.id);
cmd.run(client, interaction, args);
}
// Context Menu Handling
if (interaction.isContextMenu()) {
// await interaction.deferReply({ ephemeral: true });
const command = client.slash.get(interaction.commandName);
if (command) command.run(client, interaction);
}
});
You aren't passing in the args parameter in your function execution. You can remove args and use <CommandInteraction>.options.getRole()
to get the role argument. Now keeping that in mind we can go ahead and define role as that.
const role = interaction.options.getRole("role");