简体   繁体   中英

How to find channel by name DiscordJS 12

I know they changed some things and I've been very slowly attempting to grasp these changes, but something is escaping me and it's breaking other functions as well. So the simplest way I can solve this is by solving one of the simpler commands that are broken, which is a.sayto command that used to send a message to a specified channel by name. You could do for example !sayto testing Hello! This is a test! !sayto testing Hello! This is a test! and it would say "Hello. This is a test." in #testing, Now this doesn't work and I can't for the life of me find a method for accomplishing this anymore. I'll provide my code below, and hopefully someone can help me make sense of this.

let saytochan = bot.channels.cache.get(args[0]);
message.delete().catch();
    saytochan.send(botmessage);

It's important to me to be able to specify a channel by name when using the command. My only fear is that I'll end up having to define a variable for every channel by ID and handling it the hard way with ten million IF statements... Appreciate any and all assistance with this!

You could use find channel

const ch = bot.channels.cache.find(c => c.name === `${args[0]}`);

then

ch.send(botmessage)

if the channel doesnt exist...

if (!ch) return message.channel.send("channel doesn't exist")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM