discord.js - 尝试在我的不和谐机器人上实现仅 Patreon 的命令/功能,我将如何实现这一点?
问题描述
我的不和谐机器人将“Patreon”的角色赋予了我的 patreon 支持者。这个角色在我的主要不和谐机器人服务器上给出。所以现在我正在尝试编写一些仅对在 BOTS 不和谐服务器中具有“Patreon”角色的用户可用的命令,我该如何完成呢?
有没有办法让我像 -
message.member.has('Patreon Role').in('我的 Discord 服务器)?
解决方案
让我们回顾一下完成此任务所需的任务。
与您的用户和相应的 Patreon 角色一起获得“家庭公会”。
见Client.guilds
和Map.get()
。在公会中找到用户。
见Guild.member()
。检查用户是否具有 Patreon 角色。
见GuildMember.roles
和Collection.find()
。
您可以定义一个函数来帮助您解决此问题,将其导出并在您需要的地方使用它(或在相关范围内定义它),然后调用它来检查用户是否是您的 Patreon 支持者之一。
这是这个函数的样子......
// Assuming 'client' is the instance of your Discord Client.
function isSupporter(user) {
const homeGuild = client.guilds.get('idHere');
if (!homeGuild) return console.error('Couldn\'t find the bots guild!');
const member = homeGuild.member(user);
if (!member) return false;
const role = member.roles.find(role => role.name === 'Patreon');
if (!role) return false;
return true;
}
然后,作为一个例子,在命令中使用这个函数......
// Assuming 'message' is a Message.
if (!isSupporter(message.author)) {
return message.channel.send(':x: This command is restricted to Patreon supporters.')
.catch(console.error);
}
推荐阅读
- xamarin.forms - Xamarin iOS 搜索栏自定义
- python - imread 在 Spyder 中有效,但在 VS Code 中无效
- android - app-release 版本与 app-debug 版本有何不同?
- python - Super:超出最大递归深度
- python - 执行代码时如何打开或关闭 Visual Studio python (Jupyter) 交互式窗口 (2019)?
- jenkins - 如何通过外部 groovy 脚本 CURL 语句访问 jenkins 凭证
- python - 如何使用 pandas 将 1M OHLC 转换为 5M OHLC
- javascript - 等待但从未解决/拒绝的承诺内存使用情况
- javascript - 从数组中以名称和值的形式创建一个带有键的对象并返回它
- python - 如何将数据框的所有列与另一个数据框列进行比较并获得增量