BlueFire02
BlueFire02

Reputation: 116

Getting channel ID from a name

So I want to send a message to a channel that the bot has created! There is no possible other way to get id than to search by name! I did the following:

let channel = guild.channels.cache.find(channel => channel.name === `${v}id`);

And did not work! I searched up on stack overflow and added .id at the end:

let channel = guild.channels.cache.find(channel => channel.name === `${v}id`).id;

Still did not work! And I can't figure out why... Here is the sending part and the error that occured:

Sent message:

channel.send("Created new channel!);

Error:

C:\Users\cedri\Desktop\vibe-main\events\premium.js:45
            channel.send("Created new channel!");
                    ^

TypeError: Cannot read property 'send' of undefined
    at Timeout._onTimeout (C:\Users\cedri\Desktop\vibe-main\events\premium.js:45:21)
    at listOnTimeout (node:internal/timers:556:17)
    at processTimers (node:internal/timers:499:7)

Upvotes: 0

Views: 192

Answers (1)

0xLogN
0xLogN

Reputation: 3805

You are getting a channel that's name is <whatever "v" is>id.

You need to do

let channel = guild.channels.cache.find(channel => channel.name === nameOfChannel).id;

If this errors, you might need to use [...].first().id.

EDIT: For some reason, using ${v} didn't work here. The asker changed it to 1id and it worked.

Upvotes: 2

Related Questions