![](/img/trans.png)
[英]discord.js v13 permissions.has() function not working (TypeError: Cannot read properties of undefined (reading 'has'))
[英]Cannot read properties of undefined (reading 'permissions'). | Discord.js v13
我在斜杠命令中創建了一個名為角色信息的命令:然后出現了錯誤。 無法讀取未定義的屬性(讀取“權限”)。
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
}
}
從以前開始,該命令就可以運行,但我將其更改為第二個機器人,然后它給出了錯誤,如果有人知道,請告訴我錯誤:我將不勝感激。 類型錯誤。 無法在 Object 處讀取未定義的屬性(讀取“權限”):在客戶端運行(/home/runner//Slash/Info/roleinfo:js.66:38):(/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);
}
});
您沒有在 function 執行中傳遞 args 參數。 您可以刪除 args 並使用<CommandInteraction>.options.getRole()
獲取角色參數。 現在記住這一點,我們可以提前 go 並定義角色。
const role = interaction.options.getRole("role");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.