【问题标题】:Discord bot not giving the username of the targetDiscord bot 未提供目标用户名
【发布时间】:2021-10-05 23:15:44
【问题描述】:

所以我一直在尝试为我的 discord 机器人创建一个命令,我的机器人应该在其中嵌入目标(我提到的人)的名称,但不幸的是,没有任何效果。

bot.on("message", (msg) => {
    if (msg.content === "R!kill") {
        const embed = new Discord.MessageEmbed();
        const yoyoyo = msg.author.username;
        const target = msg.mentions.members.first();
        embed.setTitle(`**OKAY, LES KILL ${target}**`);
        embed.setImage("https://i.redd.it/ijh28d8tw0d11.jpg"), embed.setColor("RANDOM"), embed.setFooter(`mission complete`), msg.channel.send(embed);
    }
});

我什至尝试将const target = msg.mentions.members.first(); 更改为const target = msg.mentions.members.first.username();,但仍然无法正常工作。


【问题讨论】:

  • 使用 msg.startsWith(...) 代替 msg.content ==
  • 你的意思是msg.content.startsWith()

标签: javascript node.js discord discord.js


【解决方案1】:

msg.mentions.members.first(); 返回一个<GuildMember> 对象,您可以从中访问其#displayName 属性以获取其各自的公会成员名称。所以将其转换为:msg.mentions.members.first().displayName

【讨论】:

    【解决方案2】:

    如果您想在消息中与命令一起提及某人,则消息的内容 (Message.content) 不会仅等于 R!kill

    类似于R!kill <@!524243315319898133>。数字是您提到的用户的ID。

    您更有可能想要检查内容是否为.startsWith() 命令。 (正如 Tyler2P 在他们的 comment 中所说的那样。)

    要获取用户的用户名,您可以使用GuildMember.user,它具有username 属性。在您的情况下,targetGuildMember 的一个实例。

    if (msg.content.startsWith("R!kill")) {
    
        const embed = new Discord.MessageEmbed();
        const target = msg.mentions.members.first();
    
        // Add check if no one is mentioned, we return from the function
        if (!target) {
            msg.channel.send("Who should I kill?");
            return;
        }
    
        embed.setTitle(`**OKAY, LES KILL ${target.user.username}**`);
        embed.setImage("https://i.redd.it/ijh28d8tw0d11.jpg"), embed.setColor("RANDOM"), embed.setFooter(`mission complete`), msg.channel.send(embed);
    
    }
    

    【讨论】:

    • 嘿,我收到一个错误,我的控制台一直说“非法返回语句”,当我删除返回语句时,它开始给我更多错误,“未定义味精”等。
    • 您是否将代码放入您的bot.on("message", (msg) => ...) 事件回调中?
    【解决方案3】:

    主要问题是你只放target,此时你应该先获取用户,然后是用户名,如下:target.user.username

    
    bot.on("message", (msg) => {
        if (msg.content === "R!kill") {
        
        if (!target) return; // If no mention return.
    
            const embed = new Discord.MessageEmbed();
            const yoyoyo = msg.author.username;
            const target = msg.mentions.members.first();
            embed.setTitle(`**OKAY, LES KILL ${target.user.username}**`);
            embed.setImage("https://i.redd.it/ijh28d8tw0d11.jpg"), embed.setColor("RANDOM"), embed.setFooter(`mission complete`), msg.channel.send(embed);
        }
    });
    

    【讨论】:

    • 这不是主要问题。如果msg.content === "R!kill" 怎么会有target?如果msg.content === "R!kill" 没有提到任何人。
    猜你喜欢
    • 2021-10-17
    • 1970-01-01
    • 2020-07-27
    • 1970-01-01
    • 2018-02-15
    • 2021-02-07
    • 2022-01-13
    • 2021-12-20
    • 1970-01-01
    相关资源
    最近更新 更多