Reputation: 13
I am trying to make a bot which sends an embedded message to a channel and then reacts to it. The channel that it sends to is created by the discord bot so I don't have the ID of the channel, just the name which is simply island-info-\<user ID>
. This channel is created when you run command /channel
but will soon be changed to when you join the server and deleted when you leave. When i run this code:
else if (cmd === `${prefix}channel`){
const name = "island-info-" + message.author.username.toLowerCase();
message.guild.channels.create(name, {
type: 'text',
permissionOverwrites: [
{
id: message.guild.id,
deny: ["VIEW_CHANNEL", "SEND_MESSAGES"]
},
{
id: message.author.id,
allow: ["VIEW_CHANNEL", "ADD_REACTIONS"]
},
],
parent: "734170209107051074"
})
.catch(console.error);
const Embed = new Discord.MessageEmbed()
.setTitle('ISLAND INFO')
message.guild.channels.cache.find(r => r.name === name).send(Embed)
message.guild.channels.cache.find(r => r.name === name).messages.fetch({ limit: 1 }).then(messages => {
messages.first().react("👍")
}).catch(err => {
console.error(err)
})
}
It throws the error: Cannot read property 'send' of undefined
which is because of the line message.guild.channels.cache.find(r => r.name === name).send(Embed)
. Is there a better way of doing this because when i take the cache
part out it says find
isn't a command. Thank you!
(EDIT) I believe this is happening because it is sending the message to the channel at the same time or before it creates the channel, for a reason i dont know of, does anyone know a way around this because when I access the channel after the last }
it works all fine
Upvotes: 0
Views: 105
Reputation: 11080
The channel does not exist at the time that you're trying to send a message to it.
You're using .then()
and .catch()
so you must have some awareness of promises. Remember that the action that a promise represents is not finished anywhere except inside the promise callback (or after if you use await
).
Basically you're writing this:
//send a request to Discord to make a channel
message.guild.channels.create(name, {...}).catch(console.error);
...
//immediately, without waiting for Discord to make the channel, send a message to it
message.guild.channels.cache.find(r => r.name === name).send(Embed);
Your code to send a message depends on the channel having already been created. Thus, it needs to be in the .then()
callback of the channels.create(...)
promise. This has the additional advantage that the promise will actually resolve with the channel object, so you can call .send()
on it instead of needing to search the cache.
message.guild.channels.create(name, {...}).then(chan => {
//make embed
chan.send(Embed);
}).catch(console.error);
You will need to similarly attach a .then()
to the .send()
call to react to the message that was just sent. Because you need to wait for Discord to actually make the message before you can react to it.
Upvotes: 1
Reputation: 123
If it is undefined then it means the channel you need with that name does not exist. I don't know how in your situation you would handle this, but this is an option:
const Embed = new Discord.MessageEmbed()
.setTitle('ISLAND INFO');
const channel = message.guild.channels.cache.find(r => r.name === name);
if (!channel) message.channel.send("Your channel does not exist!");
else {
channel.send(embed)
}
Another thing to watch out for when storing data by username is that usernames can be changed. I suggest you name your channels by user id, as these do not change ever
Upvotes: 0