【问题标题】:Discord guilds.join OAuth2 in JSDiscord guilds.join OAuth2 in JS
【发布时间】:2021-03-29 10:38:13
【问题描述】:
我正在为 Discord (node.js) 创建一个机器人,我希望它是 oauth2,因此当用户将机器人添加到他们的服务器并接受 oauth 应用程序权限时,用户会自动加入我的机器人支持服务器。我见过https://dsc.gg 这样做,我想做类似的事情。我将如何在 Javascript 中执行此操作? (在 replit.com 上托管)谢谢!
【问题讨论】:
标签:
javascript
node.js
oauth-2.0
discord
bots
【解决方案1】:
首先,fetch the access token from Discord 如果您使用的是response_type=code。
其次,使用Discord.JS's addMember function将用户添加到公会:
/*
You'll need to have already defined these variables:
guild : The guild you're adding the member to.
accessToken : The access token.
userId : The user's ID.
client: Your bot's client.
*/
// Fetch the user.
client.users.fetch(userId).then((user) => {
// Add the user to the guild - make sure you pass the access token.
guild.addMember(user, { accessToken });
});
记得get the user's explicit permission before adding them to the server:
您不得以任何方式使用 API 来:
- 未经 Discord 用户的明确许可修改 Discord 用户的帐户。例如,您不得将 Discord 用户添加到 Discord(也称为“服务器”),除非 Discord 用户明确批准加入该 Discord(例如在使用“群组查找器”应用时);