How to reply to any DMs sent to the bot? - discord

I am trying to make my bot replying to any DM sent to it.
So I currently have this:
client.on('msg', () => {
if (msg.channel.DMChannel) {
msg.reply("You are DMing me now!");
}
});
But unfortunately, it does not reply to any DM.
I've tried to replace msg.channel.DMChannel with msg.channel.type == 'dm' but this didn't work.
I also tried to replace msg.reply with msg.author.send and msg.channel.send but they all didn't work.
Any help would be appreciated.
Thanks!

On the official documentation I don't see mentioned the event client.on('msg') only client.on('message').
With that out of the way:
client.on('message', msg => {
if (msg.channel.type == "dm") {
msg.author.send("You are DMing me now!");
return;
}
});
Just tried this and worked without any issues.

Your function isn't receiving the proper 'msg' object, try this:
client.on('message', async message => {
if (message.channel.type == 'dm') {
message.reply("You are DMing me now!");
}
});
Edit: I referenced sample code here to come to this conclusion. Maybe this link will help you in the future. Good luck!

Related

discord.js - bot get kicked out of voice channel event

What I tried to do for my music bot was getting him to report when he gets kicked out of a voice channel, by saying for example "I was forcefully disconnected". I didnt find a useful property or method to check this, so I asked for help to some friends and tried this:
client.on('voiceStateUpdate', (oldMember, newMember) => {
let newUserChannel = newMember.voiceChannel
if (newUserChannel === undefined) return console.log("I was kicked from the voice channel")
})
It didnt work.
So, is there any way to solve my problem?
I had the same proble some time ago.
This is the way i used to solve it:
Client.on('voiceStateUpdate', (oldState, newState) => {
// Represents a mute/deafen update
if(oldState.channelId === newState.chanelId) return console.log('Mute/Deafen Update');
// Some connection
if(!oldState.channelId && newState.channelId) return console.log('Connection Update');
// Disconnection
if(oldState.channelId && !newState.channelId){
console.log('Disconnection Update');
// Bot was disconnected?
if(newState.id === Client.user.id) return console.log(`${Client.user.username} was disconnected!`);
}
});
Since the voiceStateUpdate event is fired in everything related to voice channels, you should add as much scenarios as possible to make things work as intended.

DiscordJs Make reaction Bomb to comment

I want to make a fun little code on the ideia of bombing a comment with reactions.
I know what i need but i dont know how to get it, i've really searched all around internet and i dont seem to find the necessary function.
I probably need: comment ID and channel ID so the command should need to be:
(prefix)command (args0 with channel id) (args1 with comment id) so it can reacti to the exact comment i want.
It should be possible to do this with a if function like
if (find(channel) and find(comment)){
message.react('EMOJI')
}else {return message.reply('need to specify channelID and commentID')} ```
Thanks
Your insight to the problem is absolutely correct, you may achieve this like so:
let prefix = "!";
client.on("message", async (message) => {
if(message.author.bot) return
const args = message.content.slice(prefix.length).trim().split(' ')
if(message.content.toLowerCase() == "!bomb"){
if(!args[0]) return message.channel.send("Provide a channel ID")
if(!args[1]) return message.channel.send("Provide a message ID")
message.guild.channels.cache.get(`${args[0]}`).messages.fetch(`${args[1]}`).then(m => { m.react("😳"); });
message.channel.send("reacted");
}
})

discord.js - Send image on member ban?

Hi so I would like my bot to send an image in the general chat when someone gets banned by for example, dyno, but I do not know how to do that, if anyone could help, I would appreciate it!
You'd create a listener for guildBanAdd, and send a message to the particular channel when a user is banned.
client.on('guildBanAdd', (guild, user) => {
if(guild.id === 'GuildID') {
const notificationChannel = guild.channels.cache.find(c => c.name === 'general');
notificationChannel.send('Message', {files: ['image address/url']});
}
});

how can i make my discord.js bot reply to one specific user?

I'm trying to make a discord.js bot that will reply to one user when they type anything into the chat but I'm having trouble defining that user. Here is the code:
bot.on("message", message => {
if (message.content.includes(" ")) {
//the user im trying to target
if (!message.author.user(fuze_fatal1ty.user)) {
message.reply('No');
}
}
});
This is worded poorly, but I think i get it, you would need to check to their id.
bot.on("message", message => {
if(msg.author.id === "THE_USER_ID") {
msg.reply("No");
}
});
Not sure what fuze_fatal1ty.user is supposed to be

A bot where you can answer dms in the console log or a discord channel

I searched it up and got a code with readline where it looks like this:
const Disc = require('discord.js');
const client = new Disc.Client();
const token = 'token'
const readline = require('readline');
client.login(token);
client.on('message', function(message){
if(message.channel.type === 'dm'){
console.log("[" + message.author.username + "]: " + message.content)
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('REPLY TO ' + message.author.username + ': ', (answer) => {
message.author.send(`${answer}`);
rl.close();
});
}
});
But it doesn't work helpp
This is a topic I just did recently actually, so I'll walk you through it and give you some code to go along with it.
First, I would like to say when your making a post, include a clear question. From what it sounds like, your asking for a bot that logs dms to the console, or responds to them. I will just answer both questions.
The easiest way to check for a DM is to see if the message channel type is DM. Check here for more info on the channel class. You can check if a channel is a certain type by doing this:
if (message.channel.type === 'dm'){ } // change dm to the type you want
This will have to go in your on message function, so right now, if you're following along, the code would look like this:
bot.on('message', async message => {
if (message.channel.type === 'dm'){ }
});
From there it's simply adding code to the inside of the if statement. You will always want a return statement inside of it just incase nothing happens, so it doesn't try to do anything in the channels.
For what you want, this will log the DM to the console and reply to it, if it is equal to a certain message.
bot.on('message', async message => {
if (message.channel.type === 'dm'){
console.log(message.content);
if(message.content === "something"){
return await message.channel.send("Hi!");
}
return;
}
});
This should do what you want, if you have any questions, comment it on here and I'll respond as soon as possible :)
edit:
bot.on('message', async message => {
if (message.channel.type === 'dm'){
console.log(`${message.author.username} says: ${message.content}`);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question(`REPLY TO ${message.author.username}: `, (answer) => {
message.author.send(`${answer}`);
rl.close();
});
}
});

Resources