发布于 2026-01-06 1 阅读
0

Reddit Bot 制作 Reddit 回复机器人 DEV 的全球展示挑战赛,由 Mux 呈现:展示你的项目!

Reddit机器人制作Reddit回复机器人

由 Mux 赞助的 DEV 全球展示挑战赛:展示你的项目!

网上有很多关于用 Python 编写 Reddit 机器人的文档,但我却很难找到 Node 的基础文档——甚至 Reddit 官方维基上列出的一些库也已经失效或有五年历史了(也就是说:对新版 Reddit 的支持不太好)。因此,我想写一篇关于一个简单常见用例的文章:回复标记你的用户

创建 Reddit 应用程序

首先,请访问https://www.reddit.com/prefs/apps并点击“创建应用”——这样做是为了避免 Reddit 使用您的个人用户帐户。您还应该为您的机器人注册一个新的 Reddit 帐户(尤其是在机器人可以被召唤的情况下)。请确保将您的主帐户和机器人帐户都添加为该应用的开发者。

创建应用时,您需要填写类似如下的字段:

您无需担心about uriorredirect uri字段,因为您不会使用它们,所以您可以随意填写任何内容。

进入系统后,你会看到类似这样的屏幕:

记下这个信息,因为你马上就会用到它。

启动 Node 项目

snoostorm对于类似这样的需求,我发现这个库(一个封装库)非常易用snoowrap。它使得获取传入的评论变得异常简单。

首先,你需要创建一个snoowrap对象,然后用它来创建一个CommentStream新对象。为此,你需要你的 Reddit 机器人用户名、密码、应用程序密钥和应用程序 ID。你必须指定一个唯一的用户代理,例如 `user-agent` my-node-js-bot。配置方法如下(我的配置基于上图):

const Snoowrap = require('snoowrap');
const { CommentStream } = require('snoostorm');

const client = new Snoowrap({
    userAgent: 'my-node-js-bot',
    clientId: 'qR6rJnQ7sEJZDw',
    clientSecret: 'OCoo9pYnlC2K6fxQQxbcIPQ5MA4',
    username: 'myusernamebutactuallybot',
    password: 'mypasswordbutactuallybot'
});
Enter fullscreen mode Exit fullscreen mode

有了这个客户端对象,你终于可以开始监听新评论了!前往/r/testingground4bots,加入一个讨论串或创建你自己的讨论串。然后,添加一些代码来开始监听评论:

// pollTime is 10000 because reddit is very strict on posting too frequently
// at first, you'll only be able to post once every 10 minutes, so make sure you get it right!
const comments = new CommentStream(client, { 
    subreddit: 'testingground4bots', 
    limit: 10, 
    pollTime: 10000 
});

comments.on('item', (item) => {
    console.log(item);
});
Enter fullscreen mode Exit fullscreen mode

启动机器人后,你会看到终端里涌现出大量评论。你可能会疑惑为什么会这样——明明还没有新的评论呢!这是因为client机器人启动时总是会显示前 X 条评论(本例中为 10 条),然后才会持续更新。

我们可以轻松解决这个问题:

// reddits api doesn't use millis
const BOT_START = Date.now() / 1000;

const comments = new CommentStream(client, { 
    subreddit: 'testingground4bots', 
    limit: 10, 
    pollTime: 10000 
});

comments.on('item', (item) => {
    if(item.created_utc < BOT_START) return;

    console.log(item);
});
Enter fullscreen mode Exit fullscreen mode

太好了,现在你只会看到最新的评论了。希望你的 Reddit 账号足够活跃,可以在这个子版块发帖。如果你发帖了,很快就会看到终端里显示这些评论。

使其互动

目前,你已经有了一个可以读取评论的机器人——这真是个不错的开始!但你还想让它与你的受众互动,对吧?那么,何不来个经典的“Hello World”呢?来看看吧:

const BOT_START = Date.now() / 1000;

const comments = new CommentStream(client, { 
    subreddit: 'testingground4bots', 
    limit: 10, 
    pollTime: 10000 
});

comments.on('item', (item) => {
    if(item.created_utc < BOT_START) return;

    item.reply('hello world!');
});
Enter fullscreen mode Exit fullscreen mode

在那里,每当有评论出现时,机器人都会回复“你好,世界!”等等……这样会不会有点太频繁了?可能会有点烦人。Reddit建议在机器人被提及时进行专门回复,所以,有一个相当简单的方法可以做到这一点:

const BOT_START = Date.now() / 1000;

const canSummon = (msg) => {
    return msg && msg.toLowerCase().includes('/u/myusernamebutactuallybot');
};

const comments = new CommentStream(client, { 
    subreddit: 'testingground4bots', 
    limit: 10, 
    pollTime: 10000 
});

comments.on('item', (item) => {
    if(item.created_utc < BOT_START) return;
    if(!canSummon(item.body)) return;

    item.reply('hello world!');
});
Enter fullscreen mode Exit fullscreen mode

好了!这个功能的作用是确保你的机器人找到的评论确实指向机器人本身。它canSummon会进行一些基本的检查,以确保你的机器人不会错误地向很多人的评论区发送垃圾信息。现在发表一条评论/u/myusernamebutactuallybot(你应该检查一下你自己的机器人名称),你应该很快就会看到一条回复,内容是“你好,世界!”

你只需要做这些!🎉

文章来源:https://dev.to/seiyria/making-a-reddit-reply-bot-f55