This is my code:
const {
EmbedBuilder,
ChannelType,
} = require("discord.js");
const serverId = "1110927555693924659";
const categoryId = "1114273709084191765";
client.on("messageCreate", async (message) => {
if (message.channel.type === ChannelType.DM && !message.author.bot) {
const guild = client.guilds.cache.get(serverId);
const category = guild.channels.cache.get(categoryId);
const channel = guild.channels.cache.find(
(channel) =>
channel.name === message.author.id &&
channel.parentID === category.id
);
const embed = new EmbedBuilder()
.setColor("#0099ff")
.setTitle("New Message")
.setDescription(message.content)
.setAuthor({
name: message.author.tag,
iconURL: message.author.displayAvatarURL(),
});
if (channel) {
await channel.send({ embeds: [embed] });
} else {
const channel = await guild.channels.create({
name: message.author.id,
type: ChannelType.GuildText,
parent: category,
});
await channel.send({ embeds: [embed] });
}
}
});
I try to log messages that are sent to my bot via DMs to a channel. This code creates a channel with the ID of the user as a name when someone sends a message to my bot's DM and sends the message there as an Embed Message. The bot must search in the categoryId
if there is a channel with the same User ID as the user who sent the message then the bot must send the new User message there. If the bot doesn't have a channel created with the User's ID then the bot must create a new channel and send the User's message there. The problem is when I send a message a second time it creates a second channel with my ID instead of sending my message in the channel that was previously created! Any answer is accepted.
My Intents & Partials:
Intents:
Guilds
GuildMembers
GuildMessages
MessageContent
DirectMessages
GuildVoiceStates
Partials:
User
Message
GuildMember
ThreadMember
Channel
I already checked both of these articles 1) & 2), but they are outdated and doesn't resolve my problem.