【问题标题】:How do I invite someone to a guild in discord.js?如何在 discord.js 中邀请某人加入公会?
【发布时间】:2021-11-02 16:58:05
【问题描述】:

我正在尝试让我的 discord 机器人向用户发送加入公会的链接,但我无法确定将其发送给谁。由于他们不在公会中,因此您无法向他们发送消息。

if (!args[0]) return message.reply("Who would you like to invite? For example: funny#6942");
    message.channel.createInvite({
            unique: true,
            maxUses: 1,
            maxAge: 8 * 60 * 1000
        })
        .then(invite => {
            var invited = args[0];
            message.invited.send("https://discord.gg/" + invite.code);
            //dm person with the invite code
        });

我对 Discord 的 JavaScript API 还是有点陌生​​。

【问题讨论】:

    标签: javascript discord.js


    【解决方案1】:

    您可以在所有缓存的用户中查找该用户,如

    client.users.cache.find((x) => {
     x.tag == `${args[0]}` 
    }).send("https://discord.gg/" + invite.code); // This would return a GuildMember object and send the code to that person 
    

    可能存在用户未缓存的情况,在这种情况下,最好向用户请求 Id 并使用 <Client>#user#fetch() 在他们身上,尽管如果你的机器人很小,则无需担心! ?

    【讨论】:

    • 我很确定你必须用 ${args[0]} 这样的花括号括起来 args[0]
    • 是的,如果它们在 q 字符串中,你必须添加占位符
    • 哦,等一下,你可以完全放弃模板文字,直接做x.tag == args[0]
    • 是的,尽管x.tag 返回一个字符串,我想你也可以这样做,所以将字符串等同于字符串是更好的做法:)
    • 它只是给我错误“无法读取未定义的属性'发送'”
    猜你喜欢
    • 2020-12-11
    • 2021-09-05
    • 2021-05-12
    • 2019-08-28
    • 2021-03-29
    • 2020-10-18
    • 2012-12-25
    • 2020-09-23
    • 1970-01-01
    相关资源
    最近更新 更多