Discord.js move messages in a different Channel

Viewed 622

I'm trying to make the t!suggestion command work on different Servers. My problem is that i used the Channel ID in the following Code, which is obviously not working on a second Server. So my thought was to exchange the Channel ID and use the Channel Name. Unfortunatley i have no idea how to do that. And therefore are my question. Is it possible to just change the Code and use the Channel Name instead of the Channel ID and would that work?

bot.on("message", async (message) => {
    if (message.content.startsWith("t!suggestion")) {
        var str = message.content.slice(" t!suggestion".length);

        const channelToSendId = "8786607442820137466";

        const embed = new Discord.MessageEmbed()
            .setThumbnail("Picture")
            .setAuthor("TestBot", "small picture")
            .setTitle("Suggestion")
            .setColor("#151515")
            .addFields({ name: "Person, that suggested somenthing:", value: `<@!${message.author.id}>` }, { name: "suggestion:", value: `${str}` }, { name: "Channel:", value: `${message.channel}` })
            .setTimestamp();

        await bot.channels.cache
            .get(channelToSendId)
            .send(embed)
            .then((embedMessage) => {
                embedMessage.react("✅");
                embedMessage.react("❌");
            });
        message.delete();
    }
});
1 Answers

Using the find method will work.

Change this

bot.channels.cache.get(channelToSendId)

To this

bot.channels.cache.find(c => c.name === 'suggestions')

The get method always finds by id (that’s how discord.js sets it), but you can put a condition in find. Note that it is a function. Another problem is that this will still only work on one channel, even if the command is run in a different guild. To fix that, you should only check the current guild's channels.

message.guild.channels.cache.find(c => c.name === 'suggestions')

This will find the channel with the name suggestions (we don’t put the # in the code) and send the message there. Keep in mind you may receive an error if the channel is not found.

Related