繁体   English   中英

聊天 Discord.js 中的消息 dm

[英]Message dm in chat Discord.js

我似乎无法找到一种方法让某人说 !dm 并让它只发给指定的玩家。 这是一个例子:

CoolGuy 的消息:“!dm Moogstir 你好”

接收者(Moogstir):“-CoolGuy 你好”

这是我的代码:

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}`);
    }

 });

要向消息的作者发送消息,请使用message.reply(``);

如果您希望消息发送给作者以外的其他人,您需要获取收件人 ID/用户名并将其传递给集合以找到他们。 例如: collection.find('username', 'myUsername');

此外, sendMessage已被贬值,如果您要发送到服务器中的频道,则应该只是message.channel.send

您应该通读文档以了解基础知识。 其中一些一开始可能会令人困惑,但随着你玩得越多,它就会开始变得有意义。

编辑:我不确定你为什么命名你的客户端player而不是clientbot ,但我建议更改它以避免以后混淆。

有很多方法可以解决这个问题,但这里是最简单的一种。

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

运行命令!dm后,代码会找到用户提及,然后向用户发送dm message

旁注:为什么还要使用两个不同的客户端? 使用bot或使用player

//dm 命令

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)
  }
})

尝试使用此代码。 请注意,这仅适用于 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!` }); }; });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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