没想好标题,但是搭建了RSS服务器和Discord Bot

发布于 2024-06-25  413 次阅读


如题所示,近期闲的没事,在自己的服务器上搭建了个RSS服务器

通过项目RSSHub,以及教程,成功搭建了出来,并根据文档的食用指南,部署了这五个RSS源(使用软件 Fluent Reader
- 【碧蓝航线-百度贴吧】


- 【日経(日本経済新聞)】


- 【NHK-NEWS WEB EASY】


- 【旋转音律Rotaeno的bilibili动态】


- 【我自己的bilibili动态】

然后我寻思,既然RSS源都搞出来了,为什么不整一个能往Discord频道上发的呢?
然后就开始 Google [Discord RSS],搜出来基本上只有三种解决方案
1. 使用 RSS.app
但是它的 Free Plan 一个账号只能绑定两个RSS源,而且每个RSS源只能发送5条信息,想要持久用还是得交钱,所以就PASS了
2. 使用 IFTTT 的 RSS to Discord
(其实网上的教程还有一个 RSS to Webhook 然后再在Discord创建Webhook来发送信息,我看的文章显示这个服务是免费的,但是我在实际使用的时候发现 to Webhook 这点是需要付费 Plan 的,故而放弃)
我实际上也绑定它到服务器上了,下图是效果图

看起来也很好,对吧?
但是再仔细看一下这条动态发送的时间(at 06:06PM)以及bot消息转发的时间(昨天19:29),就不难看出,这个bot抓取RSS源的频率很低,而我嫌它转发的太慢,故而也PASS
3. 使用 Readybot.io
这个也是免费的,也实际使用了一下,抓取RSS源的频率也很高(每5min抓取一次),但是有时会发生抓取不到bilibili动态的情况,而且它发送的消息也不是很美观,想要修改发送信息的格式也是需要它的付费 Plan

其实一开始到这里就结束了,但是我在午睡后又突然想起来“对啊,反正我也有服务器,那我为什么不自己搭建一个Discord Bot”,然后去网上搜了各式各样的教程,发现它是用Python写的 但是我不会写Python
所以就请教了BING AI,得到了以下代码

import discord
import feedparser
import asyncio
from discord.ext import commands

token = 'your-bot-token'
channel_id = 1234567890  # 将此处替换为您的Discord群组ID
rss_url = 'http://hezhi.anitsuri.top:1200/bilibili/user/dynamic/103243330/useAvid=1'
prev_entry_link = ''

intents = discord.Intents.default()
bot = commands.Bot(command_prefix='!', intents=intents)

@bot.event
async def on_ready():
    print(f'Bot logged in as {bot.user}')
    while True:
        feed = feedparser.parse(rss_url)
        entry = feed.entries[0]
        global prev_entry_link
        if entry.link != prev_entry_link:
            prev_entry_link = entry.link
            channel = bot.get_channel(channel_id)
            await channel.send(f'更新了:{entry.link}')
        await asyncio.sleep(60)  # 每60秒检查一次更新

bot.run(token)

而这样的代码出来的结果是这样的

不知道你们怎么想,反正我的第一反应是 小小的也很可爱
那这这么短这也不行啊
于是就又改来改去,最终改出了想要的样子(太长了,PC上截不全)

import discord
import feedparser
import asyncio
from discord.ext import commands
from lxml import html

token = 'your-bot-token'
channel_id = 1234567890  # 将此处替换为您的Discord群组ID
rss_urls = [
    'http://hezhi.anitsuri.top:1200/bilibili/user/dynamic/103243330/useAvid=1',
    'http://hezhi.anitsuri.top:1200/bilibili/user/dynamic/247871319'
]
prev_entry_links = [''] * len(rss_urls)

intents = discord.Intents.default()
bot = commands.Bot(command_prefix='!', intents=intents)

@bot.event
async def on_ready():
    print(f'Bot logged in as {bot.user}')
    while True:
        for i, rss_url in enumerate(rss_urls):
            feed = feedparser.parse(rss_url)
            channel_title = feed.feed.title if 'title' in feed.feed else 'RSS feed'
            entry = feed.entries[0]
            if entry.link != prev_entry_links[i]:
                prev_entry_links[i] = entry.link
                channel = bot.get_channel(channel_id)
                description_html = html.fromstring(entry.description)
                description_text = '\n'.join(description_html.itertext())
                image_url = description_html.xpath('//img/@src')[0] if description_html.xpath('//img/@src') else ''
                message = f'{channel_title} 更新了:\n\n{description_text}\n\n{entry.link}\n\n{image_url}'
                await channel.send(message)
        await asyncio.sleep(60)  # 每60秒检查一次更新

bot.run(token)

到这里其实就结束了

届ける言葉を今は育ててる
最后更新于 2024-07-02