【问题标题】:Discord.js open DM stream with certain user by IDDiscord.js 通过 ID 使用特定用户打开 DM 流
【发布时间】:2020-04-23 18:07:41
【问题描述】:

是否有任何选项可以通过 ID 为特定用户打开 DM stream 或类似的东西。

什么意思?

这就像 ssh 终端功能,当我输入命令给机器人时:

me: DM@{user.id}
bot: {connection established message}
/**--------------------------------------------
@ and for now, every message from me, bot will deliver to {user.id} until I 
@ don't send `{message_stop}` command, for example:
----------------------------------------------*/
me: "Hi, how are you?" -> client.fetchUser('user_id')
me: "Are you ok?"
me: -stop command

不每次在每条消息的开头都使用-prefix?

或者可能像这样实现这个功能:

在服务器(公会)上创建一个频道,我在那里写的每条消息都会发送给某个用户?

实际的问题是,如果可能的话,我应该如何操纵with client.on('trigger') state 来达到这样的结果?

就目前而言,我已经编写了命令,该命令根据每条消息的请求中的前缀向用户发送消息,例如:

me: dm@user_id: Hello!
me: dm@user_id: How are you?

也许我应该将会话数据 (user.id) 存储在某处,以帮助 bot 了解我只想与特定用户通信?

【问题讨论】:

  • 没有 DM 流这样的东西。您必须检查机器人看到消息时发出的 message 事件。使用属性guild,查找它是否没有公会(所以它必须是 DM)。使用存储的用户 ID,检查所述消息是否来自该存储的用户 ID。我希望这会有所帮助。
  • 正如@Steamgamer 所说,您不能像创建 Opus 音频流一样创建 DM 流。为了与其他用户创建 DM 频道,您需要针对用户对象运行函数 createDM()。您可以通过客户端发出的message 事件获取用户对象,也可以通过您在其中标记用户的命令手动调用它。正如here 所记录的那样,在guildMemberAdd 事件上,您将返回一个GuildMember 类型,其中你可以做member.createDM()。请记住,这是一个承诺。
  • @Steamgamer,@Brxxn,看来你明白这是错误的人。我在询问文本直接消息流。没有语音音频。 MessageCollector 在这里是一个完美的解决方案。
  • 我们理解正确,我们不是在评论音频流,而是从直接消息中获得的文本。 MessageCollectors 非常适合如果消息是短期的且具有预期的持续时间,但这不是 message 事件的限制。
  • 好吧,但是您可以limit 以时间或消息数量计算如此巨大的价值,您将收到源源不断的信息流。也可以随时停止它,只需向机器人发送任何命令即可。

标签: javascript node.js discord discord.js


【解决方案1】:

我在Discord.js discord 中收到了这个答案。所以MessageCollector 是可能的。我正在使用this guide 构建我的机器人with dynamic commands。所以这是我的代码:

module.exports = {
    name: 'direct',
    description: 'Ping!',
    args: true,
    execute(message, args, client) {
        const u = message.content.split(/(?<=^\S+)@/)[1];
        console.log(message.content);
        message.channel.send(`Connection established: ${message.author.id}`);
        const filter = m => m.content.includes('discord');
        const collector = message.channel.createMessageCollector(filter, {
            maxMatches: 10,
            time: 30000,
            errors: ['time']
        });

        collector.on('collect', m => {
            console.log(`${Date.now()} Collected ${m.content}`);
            client.fetchUser('user_id').then((user) => {
                return user.send(`${m.content}`);
            });
        });

        collector.on('end', collected => {
            message.channel.send(`Connection cancelled with ${message.author.id}. You send ${collected.size} messages`);
            console.log(`${Date.now()} Collected ${collected.size} items`);
            if (collected.size === 0) {
                console.log('here');
            }
        });
    }
};

在这种情况下,您只需输入命令名称(直接),然后将包含discord DM 的每条消息输入到 user_id 10 次或 30 秒。如果你想改变它,你可以使用参数。希望它可以帮助任何人。

另外,我非常感谢 Discord.js 社区的 Monbrey#4502 提供此信息。

【讨论】:

    猜你喜欢
    • 2021-10-26
    • 1970-01-01
    • 2021-12-24
    • 2021-05-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-27
    • 2020-12-25
    • 2021-05-17
    相关资源
    最近更新 更多