简体   繁体   中英

Message dm in chat Discord.js

I can't seem to find out how to make a way to have someone say !dm and have it only go to the specified player. Here's an example:

CoolGuy's message: "!dm Moogstir Hello"

Reciever (Moogstir): "-CoolGuy Hello"

Here's my code:

const Discord = require('discord.js');
const bot = new Discord.Client();
const Player = new Discord.Client();

bot.on('ready', () => {
    console.log(`It's an owl! It's a Bagel! NO It's a ${bot.user.tag}`) 
 });

Player.on('message', (message) => {



    if (message.author.bot) return;
    const args = message.content.split(/ +/g);
    const command = args.shift().toLowerCase();

    if(command === `!dm ` + `${player.user.tag}` + `${message.content}`) {
        message.Player.sendMessage(`-${message.author}` + "\n" + 
`${message.content}`);
    }

 });

To send a message to the author of the message, use message.reply(``);

If you want the message to go to someone other than the author, you need to get the recipients ID/username and pass that through a collection to find them. ex: collection.find('username', 'myUsername');

Also, sendMessage is depreciated and should just be message.channel.send if you are sending to a channel in your server.

You should read through the documentation to get the basics down. Some of it might be confusing at first, but it will start making sense the more you play with it.

EDIT: I'm not sure why you named your client player instead of client or bot , but I would recommend changing that to avoid confusion later on.

There are many ways you could go about this, but here is one of the most simple.

//command: !dm @user <message>
let user = message.mentions.users.first(); //grabbing the user mention
user.send(<message here>);

After running your command !dm , the code finds the user mention, and then sends the user a dm message .

Sidenote: Why use two different clients as well? Either use bot or use player .

//dm command

bot.on('message', function(message){
  if(message.content.startsWith("!ddm")){

    var user = message.mentions.users.first();
    var text = message.content.split(' ').slice(2).join(' ');

    message.delete();

    if(message.author.bot) return;
    if(!message.member.hasPermission("ADMINISTRATOR")){
      message.channel.send("Du hast leider keine Rechte dafür!")
      return;
    }

    if(message.author.id == "ID hier"){
      message.reply("Tja da hast du wohl keine Rechte für! Du stehst für diesen Command leider auf der Blacklist!")
      return;
    }

    if(!user) return message.channel.send("Du hast keinen User angegeben.");
    if(!text) return message.channel.send("Du hast keine Nachricht angegeben.")

    user.send(`**${message.guild.name}:**`)
    user.send(text)
  }
})

Try using this code. Note that this only works on discord.js

 const prefix = "REPLACE THIS WITH YOUR BOTS PREFIX"; client.on("messageCreate", async (message, args) => { if (message.author.bot || !message.guild) return; // DM Command if (message.content.toLowerCase().startsWith(`${prefix}dm`)) { let userDmed = message.mentions.users.first(); let msgToUser = args.join(" ").split(", ")[1]; if (!userDmed) return message.reply({ content: `Who you wanna DM lol? (eg \`${prefix}dm @user, Hey wanna go to park?\`)` }); if (userDmed.id == message.author.id) return message.reply({ content: "Are you an idiot? You can't dm yourself lmao."}) if (userDmed.id == client.user.id) return message.reply({ content: "Ya know I can't send embed to myself idiot." }); if (!msgToUser) return message.reply({ content: `Provide some message bro. (eg \`${prefix}dm @user, Hey wanna go to park?\`)` }); let dmEmbed = new MessageEmbed() .setTitle("Someone has DM'ed you!") .setDescription(`**From:** ${message.author}\n**Their Message:** ${msgToUser}`) .setColor("#525254") .setFooter({ text: `From: ${message.author.tag}`, iconURL: message.author.displayAvatarURL() }); userDmed.send({ embeds: [dmEmbed] }); await message.reply({ content: `Your message has been send to **${userDmed.tag}**'s DM!` }); }; });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM