我正在创建一个锁定命令,其中通道名称/ id将作为第一个参数传递。命令用例将是这样的:.lock #[channel-name]/[channel_id]
。这适用于频道ID,但是当我尝试使用频道名称(例如undefined
)时,它将返回.lock #test
。有办法实现吗?
const channel =
bot.channels.cache.find(
(channel) => channel.name == `#${args.slice(0).join('-')}`
) || bot.channels.cache.get(args[0]);
if (!channel) {
console.log(channel);
return message.reply('Please provide a channel name/id!');
}
if (!args[1]) {
return message.reply('Please set the lock type!');
}
if (!channel.permissionsFor(message.guild.roles.everyone).has('VIEW_CHANNEL')) {
const errorEmbed = new Discord.MessageEmbed()
.setDescription(
`❌ \`VIEW_CHANNEL\` for \`${channel.name}\` is already disabled.`
)
.setColor('RED');
return message.channel.send(errorEmbed);
}
channel
.updateOverwrite(channel.guild.roles.everyone, { VIEW_CHANNEL: false })
.then(() => {
const msgEmbed = new Discord.MessageEmbed()
.setDescription(`✅ The channel\`${channel.name}\` has been locked.`)
.setColor('GREEN');
message.channel.send(msgEmbed);
})
.catch((error) => {
console.log(error);
const errorEmbed = new Discord.MessageEmbed()
.setDescription(`❌ Unable to lock \`${channel.name}\`.`)
.setColor('RED');
message.channel.send(errorEmbed);
});
答案 0 :(得分:3)
discord.js
将频道提及(#channel-name
)解析为<#channelID>
,而不是#channel-name.
您可以使用:
bot.channels.cache.get(args[0].match(/<#(\d+)>/)[1])
从提及中获取频道。