首页 > 解决方案 > 机器人询问用户他们希望加入什么角色,机器人根据回复将他们添加到角色中

问题描述

我在业余时间慢慢学习 javascript,但我还没有完全做到这一点。但是一位朋友让我制作一个简单的机器人,它可以给每个加入服务器的新用户发私信,询问他们希望他们的用户名是哪种颜色,并将他们添加到他们回复的角色中。服务器上的角色没有权限意义。它纯粹是用户名的颜色(discord 服务器是基于 DeviantArt 的社区聊天)。

在下面的摘录中,它工作得很好,直到用户回复颜色“蓝色”并回复“请选择颜色或确保您输入的颜色与显示的完全一致”。发送而不是将用户添加到正确的角色并回复颜色是什么。我相当确定这部分的问题:

user.addTo(server.roles.get("name", `${collected.first().content}`));
newmsg.channel.send(`Your name is now: **${collected.first().content}** Thank you and enjoy the server!`)

我只是不确定我缺少什么或应该如何构建它以使其工作。列出的颜色确实在角色区域中以小写形式显示,并且角色存在。

bot.on('guildMemberAdd', member => {
   member.send("Welcome to RebornWings! Please select a color for your username! **Choices: orange-red, red, blue, blue-green, or purple** Please type exactly how they appear in the list of choices.")
   .then((newmsg) => { //Now newmsg is the message you sent
     newmsg.channel.awaitMessages(response => response.content, {
       max: 1,
       time: 300000,
       errors: ['time'],
    }).then((collected) => {
     user.addTo(server.roles.get("name", `${collected.first().content}`))
     newmsg.channel.send(`Your name is now: **${collected.first().content}** Thank you and enjoy the server!`)
}).catch(() => {
newmsg.channel.send('Please choose a color or be sure you typed the color exactly as shown.');
      });
    });
});

bot.login(config.token);

我想我已经很接近通过实验来获得它了,只是不确定我错过了什么。

在我自己慢慢学习时提前感谢您的帮助。

标签: javascriptnode.jsdiscordroleprivate-messaging

解决方案


角色添加过程之后的 Catch 语句被触发,因为您试图不正确地获取角色并将其添加到不存在的变量中。

如果您将 user 替换为 member 并将 addTo 替换为 addRole 将是正确的。
也 get 不用于此目的。您将 Collection#get 与雪花 ID 一起使用(服务器的角色保存在集合中)。
出于您的目的,您只需将 get 替换为 find 即可。但正如Stock Overflaw已经说过的,我会首先检查用户是否输入正确。您永远不要盲目相信用户输入。


推荐阅读