如何解决有没有一种方法可以按名称获取渠道? Discord.js v12
我正在创建一个锁定命令,其中通道名称/ 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);
});
解决方法
discord.js
将频道提及(#channel-name
)解析为<#channelID>
,而不是#channel-name.
您可以使用:
bot.channels.cache.get(args[0].match(/<#(\d+)>/)[1])
从提及中获取频道。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。