简体   繁体   English

如何将参数作为用户 ID?

[英]How do I make an argument expected as a user ID?

My question is about these lines for a Bot on Discord:我的问题是关于 Discord 上的机器人的这些行:

client.users.get("idhere")
  .send("message")

How do I turn it into a script where it takes an argument and expects it to be a user ID?我如何将它变成一个脚本,它接受一个参数并期望它是一个用户 ID? (Can you please send a script?) (可以发个脚本吗?)

So I can type [$dm (userid) (msg)]所以我可以输入[$dm (userid) (msg)]

NOTE: I want the bot to DM a user by his ID注意:我希望机器人通过他的 ID DM 用户

This is my current code:这是我当前的代码:

const Discord = require('discord.js');

const client = new Discord.Client();

client.on("ready", () => {
  console.log("the bot is ready...");
  client.user.setGame("prefix is $");
});

const prefix = "$";
client.on("message", (message) => {
  const args = message.content.slice(prefix.length).trim().split(/ +/);
  const cmdname = args.shift().toLowerCase();
  mention = message.mentions.users.first();
  if (cmdname == 'send') {
    if (mention == null) return;
    message.delete();
    args.shift();
    mention.send(args.join(' '));
    message.channel.send("done");
  }
});

client.login('token');

You should definitely learn the basics of js.你绝对应该学习js的基础知识。

So basically you could first check if there is a mention and if there are none try to get the user by id.所以基本上你可以先检查是否有提及,如果没有,请尝试通过 id 获取用户。

(Updated to Discord.js v12) (更新到 Discord.js v12)

const { Client, MessageEmbed } = require("discord.js");
const client = new Client();

client.on("ready", () => {
  console.log("the bot is ready...");

  client.user.setActivity("prefix is $");
});

client.on("message", (message) => {
  const args = message.content.slice(prefix.length).trim().split(/ +/);
  const cmdName = args.shift().toLowerCase();
  const prefix = "$";

  if (cmdName == "send") {
    message.delete();

    let targetChannel;
    let mention = message.mentions.members.first();
    if (!mention) targetChannel = client.users.fetch(args[0]);
    // if there is no mention we tring to get user from id
    else targetChannel = mention;

    if (!targetChannel) {
      const embed = new MessageEmbed()
        .setColor("RANDOM")
        .setDescription("You need to mention user or provide his id.");
      message.channel.send(embed).then((msg) => msg.delete(5000));
      return;
    }

    let text = args.slice(1).join(" "); // removing id or mention

    if (!text) {
      const embed = new MessageEmbed()
        .setColor("RANDOM")
        .setDescription("You need to provide text to send.");
      message.channel.send(embed).then((msg) => msg.delete(5000));
      return;
    }

    targetChannel.send(text);
    const embed = new MessageEmbed()
      .setColor("RANDOM")
      .setDescription("Successufully sended message.");
    message.channel.send(embed).then((msg) => msg.delete(5000));
  }
});

client.login("token");

As message.mentions.users.first();作为message.mentions.users.first(); returns undefined we can just make a simple and quick change to the mention variable:返回 undefined 我们可以对提及变量进行简单快速的更改:

const mention = message.mentions.users.first() || client.users.get(args[0]);

If no user is mentioned you it will try to use the ID of the User.如果没有提到用户,它会尝试使用用户的 ID。

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

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