Skip to content

Commit 944a82f

Browse files
committed
Show Discord channel names and events in startup config
- Fetch channel names from Discord at startup for better readability - Display each channel on its own line: #channel-name (id) = events - Refactor platform logging into separate helper functions - Bump dependencies: discord.js to 14.25.1, ethers to 6.16.0
1 parent 0db32f7 commit 944a82f

File tree

2 files changed

+80
-26
lines changed

2 files changed

+80
-26
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "opensea-activity-bot",
3-
"version": "3.5.1",
3+
"version": "3.5.3",
44
"description": "A bot that shares new OpenSea events for a collection to Discord and Twitter.",
55
"author": "Ryan Ghods <ryan@ryanio.com>",
66
"license": "MIT",
@@ -19,8 +19,8 @@
1919
"lint": "ultracite lint && npx tsc --noEmit -p tsconfig.build.json"
2020
},
2121
"dependencies": {
22-
"discord.js": "^14.14.1",
23-
"ethers": "^6.9.0",
22+
"discord.js": "^14.25.1",
23+
"ethers": "^6.16.0",
2424
"sharp": "^0.33.5",
2525
"timeago.js": "^4.0.2",
2626
"twitter-api-v2": "^1.25.0"

src/index.ts

Lines changed: 77 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import { Client, type TextBasedChannel } from "discord.js";
12
import {
23
type EventTimestampSource,
34
fetchCollectionSlug,
45
fetchEvents,
56
resolveLastEventTimestamp,
67
} from "./opensea";
7-
import { messageEvents } from "./platforms/discord";
8+
import { channelsWithEvents, messageEvents } from "./platforms/discord";
89
import { tweetEvents } from "./platforms/twitter";
910
import type { OpenSeaAssetEvent } from "./types";
1011
import { MS_PER_SECOND } from "./utils/constants";
@@ -19,7 +20,78 @@ import {
1920
minOfferETH,
2021
} from "./utils/utils";
2122

22-
const logPlatformConfig = (
23+
const fetchDiscordChannelNames = async (): Promise<Map<string, string>> => {
24+
const channelNames = new Map<string, string>();
25+
26+
if (!(process.env.DISCORD_TOKEN && process.env.DISCORD_EVENTS)) {
27+
return channelNames;
28+
}
29+
30+
const client = new Client({ intents: [] });
31+
32+
try {
33+
await new Promise<void>((resolve) => {
34+
client.on("ready", () => resolve());
35+
client.login(process.env.DISCORD_TOKEN);
36+
});
37+
38+
const channelEvents = channelsWithEvents();
39+
for (const [channelId] of channelEvents) {
40+
try {
41+
const channel = await client.channels.fetch(channelId);
42+
const name = (channel as TextBasedChannel & { name?: string }).name;
43+
if (name) {
44+
channelNames.set(channelId, name);
45+
}
46+
} catch {
47+
// Channel might not be accessible
48+
}
49+
}
50+
} catch {
51+
// Discord connection failed
52+
} finally {
53+
client.destroy();
54+
}
55+
56+
return channelNames;
57+
};
58+
59+
const logTwitterConfig = () => {
60+
const twitterEvents = process.env.TWITTER_EVENTS?.replace(/,/g, ", ") ?? "";
61+
logger.info(`│ ├─ Events: ${twitterEvents}`);
62+
if (process.env.TWITTER_PREPEND_TWEET) {
63+
logger.info(`│ ├─ Prepend: "${process.env.TWITTER_PREPEND_TWEET}"`);
64+
}
65+
if (process.env.TWITTER_APPEND_TWEET) {
66+
logger.info(`│ ├─ Append: "${process.env.TWITTER_APPEND_TWEET}"`);
67+
}
68+
const config = getDefaultEventGroupConfig("TWITTER");
69+
const hasPrependOrAppend =
70+
process.env.TWITTER_PREPEND_TWEET || process.env.TWITTER_APPEND_TWEET;
71+
logger.info(`│ ${hasPrependOrAppend ? "├─" : "└─"} Grouping`);
72+
logger.info(`│ ├─ Min Group Size: ${config.minGroupSize} items`);
73+
logger.info(`│ └─ Settle Time: ${config.settleMs / MS_PER_SECOND}s`);
74+
};
75+
76+
const logDiscordConfig = async () => {
77+
const channelNames = await fetchDiscordChannelNames();
78+
const channelEvents = channelsWithEvents();
79+
const config = getDefaultEventGroupConfig("DISCORD");
80+
81+
for (const [channelId, events] of channelEvents) {
82+
const channelName = channelNames.get(channelId);
83+
const channelDisplay = channelName
84+
? `#${channelName} (${channelId})`
85+
: channelId;
86+
logger.info(`│ ├─ ${channelDisplay} = ${events.join(", ")}`);
87+
}
88+
89+
logger.info("│ └─ Grouping");
90+
logger.info(`│ ├─ Min Group Size: ${config.minGroupSize} items`);
91+
logger.info(`│ └─ Settle Time: ${config.settleMs / MS_PER_SECOND}s`);
92+
};
93+
94+
const logPlatformConfig = async (
2395
twitterEnabled: boolean,
2496
discordEnabled: boolean
2597
) => {
@@ -29,32 +101,14 @@ const logPlatformConfig = (
29101
`│ 🐦 Twitter: ${twitterEnabled ? "✅ ENABLED" : "⭕ DISABLED"}`
30102
);
31103
if (twitterEnabled) {
32-
const twitterEvents = process.env.TWITTER_EVENTS?.replace(/,/g, ", ") ?? "";
33-
logger.info(`│ ├─ Events: ${twitterEvents}`);
34-
if (process.env.TWITTER_PREPEND_TWEET) {
35-
logger.info(`│ ├─ Prepend: "${process.env.TWITTER_PREPEND_TWEET}"`);
36-
}
37-
if (process.env.TWITTER_APPEND_TWEET) {
38-
logger.info(`│ ├─ Append: "${process.env.TWITTER_APPEND_TWEET}"`);
39-
}
40-
const config = getDefaultEventGroupConfig("TWITTER");
41-
const hasPrependOrAppend =
42-
process.env.TWITTER_PREPEND_TWEET || process.env.TWITTER_APPEND_TWEET;
43-
logger.info(`│ ${hasPrependOrAppend ? "├─" : "└─"} Grouping`);
44-
logger.info(`│ ├─ Min Group Size: ${config.minGroupSize} items`);
45-
logger.info(`│ └─ Settle Time: ${config.settleMs / MS_PER_SECOND}s`);
104+
logTwitterConfig();
46105
}
47106
logger.info("│");
48107
logger.info(
49108
`│ 💬 Discord: ${discordEnabled ? "✅ ENABLED" : "⭕ DISABLED"}`
50109
);
51110
if (discordEnabled) {
52-
const discordEvents = process.env.DISCORD_EVENTS?.replace(/,/g, ", ") ?? "";
53-
logger.info(`│ ├─ Events: ${discordEvents}`);
54-
const config = getDefaultEventGroupConfig("DISCORD");
55-
logger.info("│ └─ Grouping");
56-
logger.info(`│ ├─ Min Group Size: ${config.minGroupSize} items`);
57-
logger.info(`│ └─ Settle Time: ${config.settleMs / MS_PER_SECOND}s`);
111+
await logDiscordConfig();
58112
}
59113
logger.info("│");
60114
};
@@ -134,7 +188,7 @@ const logStartupConfiguration = async () => {
134188
const twitterEnabled = Boolean(process.env.TWITTER_EVENTS);
135189
const discordEnabled = Boolean(process.env.DISCORD_EVENTS);
136190

137-
logPlatformConfig(twitterEnabled, discordEnabled);
191+
await logPlatformConfig(twitterEnabled, discordEnabled);
138192

139193
logger.info("└─");
140194
logger.info("");

0 commit comments

Comments
 (0)