Reputation: 87
I was creating a lock command where the channel name/id would be passed as the 1st argument. The command use case would be something like this: .lock #[channel-name]/[channel_id]
. This works with a channel id, however, it returns undefined
when I attempt to use a channel name instead (e.g .lock #test
). Would there be a way to achieve this?
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);
});
Upvotes: 1
Views: 1714
Reputation: 8402
discord.js
parses channel mentions (#channel-name
) as <#channelID>
, not #channel-name.
You can use:
bot.channels.cache.get(args[0].match(/<#(\d+)>/)[1])
to get a channel from a mention.
Upvotes: 3