mirror of
https://github.com/ajvpot/meshexplorer.git
synced 2026-03-28 17:42:58 +01:00
discord bot: support threads
This commit is contained in:
@@ -42,6 +42,7 @@ services:
|
||||
|
||||
# Discord Bot Configuration
|
||||
- DISCORD_WEBHOOK_URL=${DISCORD_WEBHOOK_URL}
|
||||
- DISCORD_THREAD_ID=${DISCORD_THREAD_ID:-}
|
||||
- MESH_REGION=${MESH_REGION:-seattle}
|
||||
- POLL_INTERVAL=${POLL_INTERVAL:-1000}
|
||||
- MAX_ROWS_PER_POLL=${MAX_ROWS_PER_POLL:-50}
|
||||
|
||||
@@ -16,6 +16,7 @@ import { DiscordWebhookClient, formatMeshcoreMessageForDiscord } from './lib/dis
|
||||
|
||||
interface BotConfig {
|
||||
webhookUrl: string;
|
||||
threadId?: string;
|
||||
region: string;
|
||||
pollInterval: number;
|
||||
maxRowsPerPoll: number;
|
||||
@@ -30,7 +31,7 @@ class MeshCoreDiscordBot {
|
||||
|
||||
constructor(config: BotConfig) {
|
||||
this.config = config;
|
||||
this.discordClient = new DiscordWebhookClient(config.webhookUrl);
|
||||
this.discordClient = new DiscordWebhookClient(config.webhookUrl, config.threadId);
|
||||
}
|
||||
|
||||
async start() {
|
||||
@@ -44,6 +45,9 @@ class MeshCoreDiscordBot {
|
||||
console.log(`Region: ${this.config.region}`);
|
||||
console.log(`Poll interval: ${this.config.pollInterval}ms`);
|
||||
console.log(`Max rows per poll: ${this.config.maxRowsPerPoll}`);
|
||||
if (this.config.threadId) {
|
||||
console.log(`Thread ID: ${this.config.threadId}`);
|
||||
}
|
||||
|
||||
// Create streaming configuration
|
||||
const streamerConfig = createChatMessagesStreamerConfig(undefined, this.config.region);
|
||||
@@ -130,6 +134,7 @@ class MeshCoreDiscordBot {
|
||||
async function main() {
|
||||
// Get configuration from environment variables
|
||||
const webhookUrl = process.env.DISCORD_WEBHOOK_URL;
|
||||
const threadId = process.env.DISCORD_THREAD_ID;
|
||||
const region = process.env.MESH_REGION || 'seattle';
|
||||
const pollInterval = parseInt(process.env.POLL_INTERVAL || '1000', 10);
|
||||
const maxRowsPerPoll = parseInt(process.env.MAX_ROWS_PER_POLL || '50', 10);
|
||||
@@ -156,6 +161,9 @@ async function main() {
|
||||
|
||||
console.log('Configuration:');
|
||||
console.log(` Webhook URL: ${webhookUrl.substring(0, 50)}...`);
|
||||
if (threadId) {
|
||||
console.log(` Thread ID: ${threadId}`);
|
||||
}
|
||||
console.log(` Region: ${region}`);
|
||||
console.log(` Poll interval: ${pollInterval}ms`);
|
||||
console.log(` Max rows per poll: ${maxRowsPerPoll}`);
|
||||
@@ -164,6 +172,7 @@ async function main() {
|
||||
// Create and start the bot
|
||||
const bot = new MeshCoreDiscordBot({
|
||||
webhookUrl,
|
||||
threadId,
|
||||
region,
|
||||
pollInterval,
|
||||
maxRowsPerPoll,
|
||||
|
||||
@@ -40,10 +40,12 @@ export interface DiscordWebhookResponse {
|
||||
|
||||
export class DiscordWebhookClient {
|
||||
private webhookUrl: string;
|
||||
private threadId?: string;
|
||||
private messageIdMap: Map<string, string> = new Map(); // message_id -> discord_message_id
|
||||
|
||||
constructor(webhookUrl: string) {
|
||||
constructor(webhookUrl: string, threadId?: string) {
|
||||
this.webhookUrl = webhookUrl;
|
||||
this.threadId = threadId;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -54,6 +56,11 @@ export class DiscordWebhookClient {
|
||||
const url = new URL(this.webhookUrl);
|
||||
url.searchParams.set('wait', 'true');
|
||||
|
||||
// Add thread_id if configured
|
||||
if (this.threadId) {
|
||||
url.searchParams.set('thread_id', this.threadId);
|
||||
}
|
||||
|
||||
const response = await fetch(url.toString(), {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
@@ -74,7 +81,14 @@ export class DiscordWebhookClient {
|
||||
*/
|
||||
async updateMessage(discordMessageId: string, message: DiscordWebhookMessage): Promise<DiscordWebhookResponse> {
|
||||
// Use the correct URL format for updating messages
|
||||
const updateUrl = `${this.webhookUrl}/messages/${discordMessageId}`;
|
||||
let updateUrl = `${this.webhookUrl}/messages/${discordMessageId}`;
|
||||
|
||||
// Add thread_id parameter if configured
|
||||
if (this.threadId) {
|
||||
const url = new URL(updateUrl);
|
||||
url.searchParams.set('thread_id', this.threadId);
|
||||
updateUrl = url.toString();
|
||||
}
|
||||
|
||||
const response = await fetch(updateUrl, {
|
||||
method: 'PATCH',
|
||||
|
||||
Reference in New Issue
Block a user