dms not registering as a dm

hey, ive been trying to make this code from like a year ago work, but the channeltype.dm doesnt seem to be working -
24 Replies
d.js toolkit
d.js toolkit4d ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
Galactic
GalacticOP4d ago
module.exports = (client) => {
client.handleDM = async (message) => {
const {
EmbedBuilder,
ChannelType,
WebhookClient,
} = require("discord.js");

const webhookClient = new WebhookClient({
url: "hidden",
});

if (message.channel.type === ChannelType.DM) {
console.log('is a dm')
if (message.author === client) return;
const channel = await client.channels
.fetch("hidden")
.catch((err) => console.error(err));
if (
!channel.threads.cache.find(
(x) => x.name === message.author.id
)
) {
const thread = await channel.threads.create({
name: message.author.id,
autoArchiveDuration: 60,
});
await channel.send(`:promotion: ${message.author.displayName} :promotion:`)

webhookClient.send({
content: `${message}`,
username: `${message.author.displayName} (${message.author.username})`,
avatarURL: `${message.author.avatarURL({ dynamic: true })}`,
threadId: thread.id,
});
} else {
const thread = channel.threads.cache.find(
(x) => x.name === message.author.id
);
webhookClient.send({
content: `${message}`,
username: `${message.author.displayName} (${message.author.username})`,
avatarURL: `${message.author.avatarURL({ dynamic: true })}`,
threadId: thread.id,
});
}
}
};
};
module.exports = (client) => {
client.handleDM = async (message) => {
const {
EmbedBuilder,
ChannelType,
WebhookClient,
} = require("discord.js");

const webhookClient = new WebhookClient({
url: "hidden",
});

if (message.channel.type === ChannelType.DM) {
console.log('is a dm')
if (message.author === client) return;
const channel = await client.channels
.fetch("hidden")
.catch((err) => console.error(err));
if (
!channel.threads.cache.find(
(x) => x.name === message.author.id
)
) {
const thread = await channel.threads.create({
name: message.author.id,
autoArchiveDuration: 60,
});
await channel.send(`:promotion: ${message.author.displayName} :promotion:`)

webhookClient.send({
content: `${message}`,
username: `${message.author.displayName} (${message.author.username})`,
avatarURL: `${message.author.avatarURL({ dynamic: true })}`,
threadId: thread.id,
});
} else {
const thread = channel.threads.cache.find(
(x) => x.name === message.author.id
);
webhookClient.send({
content: `${message}`,
username: `${message.author.displayName} (${message.author.username})`,
avatarURL: `${message.author.avatarURL({ dynamic: true })}`,
threadId: thread.id,
});
}
}
};
};
Glaze
Glaze4d ago
What do you mean by "does not seem to work"? Does it not pass your if statement? Do you get an error?
Galactic
GalacticOP4d ago
my discord.js is v14.14.1 nothing at all doesnt log, doenst error well i guess the logging part is my fault cus no debug but im kinda rusty at djs rn
Glaze
Glaze4d ago
does your client.handleDM event fire at all? if you log outside your if statement do you get anything?
Galactic
GalacticOP4d ago
yes, if i do !=== channeltype.dm it sends the debug message
Glaze
Glaze4d ago
can you console.log(message.channel.type)?
Galactic
GalacticOP4d ago
it doesnt log anything when in dms but logs 0 in text channels and im pretty sure i have partials this is my bot.js
require("dotenv").config();
const { token } = process.env;
const {
Client,
Collection,
GatewayIntentBits,
ChannelType,
Partials,
EmbedBuilder,
WebhookClient,
} = require("discord.js");
const fs = require("fs");

const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
],
partials: [Partials.Message, Partials.Channel, Partials.Reaction],
});
client.commands = new Collection();
client.modals = new Collection();
client.commandArray = [];
const functionFolders = fs.readdirSync(`./src/functions`);
for (const folder of functionFolders) {
const functionFiles = fs
.readdirSync(`./src/functions/${folder}`)
.filter((file) => file.endsWith(".js"));
for (const file of functionFiles)
require(`./functions/${folder}/${file}`)(client);
}

client.on("messageCreate", (message) => {
client.handleDM(message);
});
client.handleEvents();
client.handleCommands();
client.handleComponents();
client.login(token);
require("dotenv").config();
const { token } = process.env;
const {
Client,
Collection,
GatewayIntentBits,
ChannelType,
Partials,
EmbedBuilder,
WebhookClient,
} = require("discord.js");
const fs = require("fs");

const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
],
partials: [Partials.Message, Partials.Channel, Partials.Reaction],
});
client.commands = new Collection();
client.modals = new Collection();
client.commandArray = [];
const functionFolders = fs.readdirSync(`./src/functions`);
for (const folder of functionFolders) {
const functionFiles = fs
.readdirSync(`./src/functions/${folder}`)
.filter((file) => file.endsWith(".js"));
for (const file of functionFiles)
require(`./functions/${folder}/${file}`)(client);
}

client.on("messageCreate", (message) => {
client.handleDM(message);
});
client.handleEvents();
client.handleCommands();
client.handleComponents();
client.login(token);
Glaze
Glaze4d ago
it doesnt log anything? did you put the console log outside the if statement?
Galactic
GalacticOP4d ago
wait is there a partial for dms? yeah beneath the webhook client but outside the if statement
Galactic
GalacticOP4d ago
ok, so thats not the problem
Glaze
Glaze4d ago
You could try updating to latest 14.17.3. couldn't hurt. in case its a bug or something w/ djs
Galactic
GalacticOP4d ago
sure, lemme try ok im on 14.17.3 lemme try still nothing
Glaze
Glaze4d ago
also can you try logging just message for dm messages? curious if that logs anything. since it doesnt error on message.channel.type, that would mean message and message.channel are defined
Galactic
GalacticOP4d ago
nothing when dm but the full json when msg hmm
Glaze
Glaze4d ago
what happens if you replace
if (message.channel.type === ChannelType.DM) {
if (message.channel.type === ChannelType.DM) {
with
if (!message.guild) {
if (!message.guild) {
makes me think its not even reaching the logging point. strange
Galactic
GalacticOP4d ago
yeah STILL nothing with !message.guild
Glaze
Glaze4d ago
then its just not running that code it seems
Galactic
GalacticOP4d ago
exactly but when its a normal message everything is fine its strange in theory there should be nothing wrong its like it cant even see the dms
treble/luna
treble/luna4d ago
missing DirectMessages intent
Galactic
GalacticOP4d ago
WHY- BRUHHH
Glaze
Glaze4d ago
oh the intent 😭 😭 😭 why didnt i think of that
Galactic
GalacticOP4d ago
why is there an intent for it 😭 when i coded this there wasnt one bru- well thanks last yea r but it worked before so idk

Did you find this page helpful?