mirror of
https://github.com/DIYgod/RSSHub.git
synced 2026-09-01 14:57:14 +08:00
fix(route): discord join event (#16318)
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
import { Route } from '@/types';
|
||||
import { DataItem, Route } from '@/types';
|
||||
import { getCurrentPath } from '@/utils/helpers';
|
||||
const __dirname = getCurrentPath(import.meta.url);
|
||||
|
||||
import cache from '@/utils/cache';
|
||||
import { config } from '@/config';
|
||||
import { parseDate } from '@/utils/parse-date';
|
||||
import { art } from '@/utils/render';
|
||||
@@ -19,7 +18,7 @@ export const route: Route = {
|
||||
requireConfig: [
|
||||
{
|
||||
name: 'DISCORD_AUTHORIZATION',
|
||||
description: '',
|
||||
description: 'Discord authorization header from the browser',
|
||||
},
|
||||
],
|
||||
requirePuppeteer: false,
|
||||
@@ -45,17 +44,17 @@ async function handler(ctx) {
|
||||
const { authorization } = config.discord;
|
||||
const channelId = ctx.req.param('channelId');
|
||||
|
||||
const channelInfo = await getChannel(channelId, authorization, cache.tryGet);
|
||||
const messagesRaw = await getChannelMessages(channelId, authorization, cache.tryGet, ctx.req.query('limit') ?? 100);
|
||||
const channelInfo = await getChannel(channelId, authorization);
|
||||
const messagesRaw = await getChannelMessages(channelId, authorization, ctx.req.query('limit') ?? 100);
|
||||
const { name: channelName, topic: channelTopic, guild_id: guildId } = channelInfo;
|
||||
|
||||
const guildInfo = await getGuild(guildId, authorization, cache.tryGet);
|
||||
const guildInfo = await getGuild(guildId, authorization);
|
||||
const { name: guildName, icon: guidIcon } = guildInfo;
|
||||
|
||||
const messages = messagesRaw.map((message) => ({
|
||||
title: message.content,
|
||||
description: art(path.join(__dirname, 'templates/message.art'), { message }),
|
||||
author: `${message.author.username}#${message.author.discriminator}`,
|
||||
title: message.content.split('\n')[0],
|
||||
description: art(path.join(__dirname, 'templates/message.art'), { message, guildInfo }),
|
||||
author: `${message.author.global_name ?? message.author.username}(${message.author.username})`,
|
||||
pubDate: parseDate(message.timestamp),
|
||||
updated: message.edited_timestamp ? parseDate(message.edited_timestamp) : undefined,
|
||||
category: `#${channelName}`,
|
||||
@@ -67,6 +66,7 @@ async function handler(ctx) {
|
||||
description: channelTopic,
|
||||
link: `${baseUrl}/channels/${guildId}/${channelId}`,
|
||||
image: `https://cdn.discordapp.com/icons/${guildId}/${guidIcon}.webp`,
|
||||
item: messages,
|
||||
item: messages as unknown as DataItem[],
|
||||
allowEmpty: true,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,55 +1,50 @@
|
||||
import got from '@/utils/got';
|
||||
import cache from '@/utils/cache';
|
||||
import ofetch from '@/utils/ofetch';
|
||||
import { config } from '@/config';
|
||||
import { RESTGetAPIGuildResult, RESTGetAPIGuildChannelsResult, RESTGetAPIChannelResult, RESTGetAPIChannelMessagesQuery, RESTGetAPIChannelMessagesResult } from 'discord-api-types/rest/v10';
|
||||
|
||||
const baseUrl = 'https://discord.com';
|
||||
export const baseUrl = 'https://discord.com';
|
||||
const apiUrl = `${baseUrl}/api/v10`;
|
||||
|
||||
const getGuild = (guildId, authorization, tryGet) =>
|
||||
tryGet(`discord:guilds:${guildId}`, async () => {
|
||||
const response = await got(`${apiUrl}/guilds/${guildId}`, {
|
||||
export const getGuild = (guildId, authorization) =>
|
||||
cache.tryGet(`discord:guilds:${guildId}`, () =>
|
||||
ofetch(`${apiUrl}/guilds/${guildId}`, {
|
||||
headers: {
|
||||
authorization,
|
||||
},
|
||||
});
|
||||
return response.data;
|
||||
});
|
||||
})
|
||||
) as Promise<RESTGetAPIGuildResult>;
|
||||
|
||||
const getGuildChannels = (guildId, authorization, tryGet) =>
|
||||
tryGet(`discord:guilds:${guildId}:channels`, async () => {
|
||||
const response = await got(`${apiUrl}/guilds/${guildId}/channels`, {
|
||||
export const getGuildChannels = (guildId, authorization) =>
|
||||
cache.tryGet(`discord:guilds:${guildId}:channels`, () =>
|
||||
ofetch(`${apiUrl}/guilds/${guildId}/channels`, {
|
||||
headers: {
|
||||
authorization,
|
||||
},
|
||||
});
|
||||
return response.data;
|
||||
});
|
||||
})
|
||||
) as Promise<RESTGetAPIGuildChannelsResult>;
|
||||
|
||||
const getChannel = (channelId, authorization, tryGet) =>
|
||||
tryGet(`discord:channels:${channelId}`, async () => {
|
||||
const response = await got(`${apiUrl}/channels/${channelId}`, {
|
||||
export const getChannel = (channelId, authorization) =>
|
||||
cache.tryGet(`discord:channels:${channelId}`, () =>
|
||||
ofetch(`${apiUrl}/channels/${channelId}`, {
|
||||
headers: {
|
||||
authorization,
|
||||
},
|
||||
});
|
||||
return response.data;
|
||||
});
|
||||
})
|
||||
) as Promise<RESTGetAPIChannelResult>;
|
||||
|
||||
const getChannelMessages = (channelId, authorization, tryGet, limit = 100) =>
|
||||
tryGet(
|
||||
export const getChannelMessages = (channelId, authorization, limit = 100) =>
|
||||
cache.tryGet(
|
||||
`discord:channels:${channelId}:messages`,
|
||||
async () => {
|
||||
const response = await got(`${apiUrl}/channels/${channelId}/messages`, {
|
||||
() =>
|
||||
ofetch(`${apiUrl}/channels/${channelId}/messages`, {
|
||||
headers: {
|
||||
authorization,
|
||||
},
|
||||
searchParams: {
|
||||
query: {
|
||||
limit,
|
||||
},
|
||||
});
|
||||
return response.data;
|
||||
},
|
||||
} as RESTGetAPIChannelMessagesQuery,
|
||||
}),
|
||||
config.cache.routeExpire,
|
||||
false
|
||||
);
|
||||
|
||||
export { baseUrl, getGuild, getGuildChannels, getChannel, getChannelMessages };
|
||||
) as Promise<RESTGetAPIChannelMessagesResult>;
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
{{ if message.type === 7 }}
|
||||
{{ message.author.global_name ?? message.author.username }} joined {{ guildInfo.name }}.<br>
|
||||
{{ /if }}
|
||||
|
||||
{{ if message.content }}
|
||||
{{@ message.content.replace(/\n/g, '<br>') }}<br>
|
||||
{{ /if }}
|
||||
@@ -8,6 +12,17 @@
|
||||
{{ /each }}
|
||||
{{ /if }}
|
||||
|
||||
{{ if message.sticker_items }}
|
||||
{{ each message.sticker_items sticker }}
|
||||
{{ if sticker.format_type < 3 }}
|
||||
<img src="https://cdn.discordapp.com/stickers/{{ sticker.id }}.png"
|
||||
{{ else if sticker.format_type === 4 }}
|
||||
<img src="https://media.discordapp.net/stickers/{{ sticker.id }}.gif"
|
||||
{{ /if }}
|
||||
alt="{{ sticker.name }}"><br>
|
||||
{{ /each }}
|
||||
{{ /if }}
|
||||
|
||||
{{ if message.embeds }}
|
||||
{{ each message.embeds e }}
|
||||
{{ if e.type === 'article' }}
|
||||
|
||||
@@ -167,6 +167,7 @@
|
||||
"@typescript-eslint/parser": "7.18.0",
|
||||
"@vercel/nft": "0.27.3",
|
||||
"@vitest/coverage-v8": "2.0.4",
|
||||
"discord-api-types": "0.37.93",
|
||||
"eslint": "9.8.0",
|
||||
"eslint-config-prettier": "9.1.0",
|
||||
"eslint-nibble": "8.1.0",
|
||||
|
||||
Generated
+8
@@ -354,6 +354,9 @@ importers:
|
||||
'@vitest/coverage-v8':
|
||||
specifier: 2.0.4
|
||||
version: 2.0.4(vitest@2.0.4(@types/node@22.0.0)(jsdom@24.1.1(bufferutil@4.0.8)(utf-8-validate@5.0.10)))
|
||||
discord-api-types:
|
||||
specifier: 0.37.93
|
||||
version: 0.37.93
|
||||
eslint:
|
||||
specifier: 9.8.0
|
||||
version: 9.8.0
|
||||
@@ -3235,6 +3238,9 @@ packages:
|
||||
resolution: {integrity: sha512-d9paCbverdqmuwR+B40phSqiHhgPKiP8dpsMz5WT9U6ug2VVQ3tqXNCedpa6iGHg6mgv9lHaoq5DJUu2IXMjsQ==}
|
||||
engines: {node: '>=18.17.0'}
|
||||
|
||||
discord-api-types@0.37.93:
|
||||
resolution: {integrity: sha512-M5jn0x3bcXk8EI2c6F6V6LeOWq10B/cJf+YJSyqNmg7z4bdXK+Z7g9zGJwHS0h9Bfgs0nun2LQISFOzwck7G9A==}
|
||||
|
||||
dlv@1.1.3:
|
||||
resolution: {integrity: sha512-+HlytyjlPKnIG8XuRG8WvmBP8xs8P71y+SKKS6ZXWoEgLuePxtDoUEiH7WkdePWrQ5JBpE6aoVqfZfJUQkjXwA==}
|
||||
|
||||
@@ -10558,6 +10564,8 @@ snapshots:
|
||||
|
||||
directory-import@3.3.1: {}
|
||||
|
||||
discord-api-types@0.37.93: {}
|
||||
|
||||
dlv@1.1.3: {}
|
||||
|
||||
doctrine@3.0.0:
|
||||
|
||||
Reference in New Issue
Block a user