Skip to content

Commit 9d009c0

Browse files
authored
Merge pull request #1 from mfiscus/bug/unhandled-exception
Fix unhandled exceptions and add Docker publish workflow
2 parents 3eaa5b9 + 2fc8c45 commit 9d009c0

11 files changed

Lines changed: 401 additions & 275 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
name: Build and Publish Docker Image
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
release:
8+
types: [published]
9+
schedule:
10+
- cron: '0 0 1 * *'
11+
12+
jobs:
13+
build-and-push:
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- name: Checkout
18+
uses: actions/checkout@v4
19+
20+
- name: Set up QEMU
21+
uses: docker/setup-qemu-action@v3
22+
23+
- name: Set up Docker Buildx
24+
uses: docker/setup-buildx-action@v3
25+
26+
- name: Log in to Docker Hub
27+
uses: docker/login-action@v3
28+
with:
29+
username: ${{ secrets.DOCKERHUB_USERNAME }}
30+
password: ${{ secrets.DOCKERHUB_TOKEN }}
31+
32+
- name: Extract metadata
33+
id: meta
34+
uses: docker/metadata-action@v5
35+
with:
36+
images: mfiscus/ref-bot
37+
tags: |
38+
type=raw,value=latest,enable=${{ github.ref == 'refs/heads/main' }}
39+
type=semver,pattern={{version}}
40+
type=semver,pattern={{major}}.{{minor}}
41+
42+
- name: Build and push
43+
uses: docker/build-push-action@v5
44+
with:
45+
context: .
46+
platforms: linux/amd64,linux/arm64
47+
push: true
48+
tags: ${{ steps.meta.outputs.tags }}
49+
labels: ${{ steps.meta.outputs.labels }}
50+
cache-from: type=gha
51+
cache-to: type=gha,mode=max

‎.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.DS_Store
2+
images/
13
node_modules
24
.env
35
config.json

‎CLAUDE.md‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# CLAUDE.md
2+
3+
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
4+
5+
## Project Overview
6+
7+
**ref-bot** is a Discord bot (Node.js + discord.js v14) that queries amateur radio reflector APIs (URF/MREF) and displays status information as formatted Discord embeds via slash commands.
8+
9+
## Commands
10+
11+
```bash
12+
# Install dependencies
13+
npm install
14+
15+
# Run locally
16+
node index.js
17+
18+
# Register/update slash commands with Discord API (required after adding/changing commands)
19+
node deploy-commands.js
20+
21+
# Lint
22+
npx eslint .
23+
24+
# Docker
25+
docker-compose up -d
26+
```
27+
28+
There are no automated tests.
29+
30+
## Architecture
31+
32+
**Entry point**: `index.js` — loads all commands from `commands/` dynamically, handles Discord interactions, enforces a cooldown system (3s default, 10s for reflector commands).
33+
34+
**Command structure** (`commands/reflector/`): Each command exports a `data` property (SlashCommandBuilder) and an `execute(interaction)` function. Commands use `undici.request()` to call the configured reflector's REST API endpoints (`/json/status`, `/json/metadata`, `/json/stations`, `/json/peers`, `/json/links`) and reply with EmbedBuilder formatted messages. All replies are ephemeral.
35+
36+
**Data flow**: Discord slash command → `index.js` interaction handler → command lookup → HTTP request to reflector API → parse JSON → format Discord embed → ephemeral reply.
37+
38+
**Events** (`events/`): `ready.js` and `interactionCreate.js` exist but contain duplicate/minimal logic — the primary interaction handling lives in `index.js`.
39+
40+
## Configuration
41+
42+
Copy `config.json-example` to `config.json` (gitignored):
43+
44+
```json
45+
{
46+
"token": "", // Discord bot token
47+
"clientId": "", // Discord application ID
48+
"guildId": "", // Discord server/guild ID
49+
"icon": "", // Embed icon URL
50+
"url": "" // Reflector API base URL (e.g. https://reflector.example.com)
51+
}
52+
```
53+
54+
For Docker, `config.json` is stored at `/opt/ref-bot/config.json` on the host and symlinked into the container.
55+
56+
## Adding a New Command
57+
58+
1. Create a file in `commands/reflector/` exporting `data` (SlashCommandBuilder) and `execute`.
59+
2. Run `node deploy-commands.js` to register it with Discord.
60+
3. The command is auto-loaded by `index.js` on startup — no other wiring needed.
61+
62+
## Code Style
63+
64+
ESLint enforces: tabs for indentation, single quotes, stroustrup brace style, max 4 nested callbacks. Run `npx eslint .` before committing.

‎commands/reflector/lastheard.js‎

Lines changed: 53 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -6,58 +6,60 @@ const limit = 5;
66
const pluralize = (count, noun, suffix = 's') => `${count} ${noun}${count !== 1 ? suffix : ''}`;
77

88
function TimeSince(date) {
9-
var seconds = Math.floor((new Date() - date) / 1000);
10-
var interval = seconds / 31536000;
11-
if (interval > 1) return pluralize(Math.floor(interval), 'year') + ' ago';
12-
interval = seconds / 2592000;
13-
if (interval > 1) return pluralize(Math.floor(interval), 'month') + ' ago';
14-
interval = seconds / 86400;
15-
if (interval > 1) return pluralize(Math.floor(interval), 'day') + ' ago';
16-
interval = seconds / 3600;
17-
if (interval > 1) return pluralize(Math.floor(interval), 'hour') + ' ago';
18-
interval = seconds / 60;
19-
if (interval > 1) return pluralize(Math.floor(interval), 'minute') + ' ago';
20-
return pluralize(Math.floor(interval), 'second') + ' ago';
9+
const seconds = Math.floor((new Date() - date) / 1000);
10+
let interval = seconds / 31536000;
11+
if (interval > 1) return pluralize(Math.floor(interval), 'year') + ' ago';
12+
interval = seconds / 2592000;
13+
if (interval > 1) return pluralize(Math.floor(interval), 'month') + ' ago';
14+
interval = seconds / 86400;
15+
if (interval > 1) return pluralize(Math.floor(interval), 'day') + ' ago';
16+
interval = seconds / 3600;
17+
if (interval > 1) return pluralize(Math.floor(interval), 'hour') + ' ago';
18+
interval = seconds / 60;
19+
if (interval > 1) return pluralize(Math.floor(interval), 'minute') + ' ago';
20+
return pluralize(Math.floor(interval), 'second') + ' ago';
2121
}
2222

2323
module.exports = {
24-
cooldown: 10,
25-
data: new SlashCommandBuilder()
26-
.setName('lastheard')
27-
.setDescription('Provides information about the last 5 stations heard on the reflector.'),
28-
29-
async execute(interaction) {
30-
31-
// retrieve last heard stations
32-
var {statusCode, headers, body} = await request(url + '/json/stations');
33-
if (statusCode != 200) return interaction.editReply(`stations api returned error ${statusCode}.`);
34-
const stations = await body.json();
35-
36-
// retrieve reflector metadata
37-
var {statusCode, headers, body} = await request(url + '/json/metadata');
38-
if (statusCode != 200) return interaction.editReply(`metadata api returned error ${statusCode}.`);
39-
const metadata = await body.json();
40-
41-
const embed = new EmbedBuilder()
42-
.setColor(0x2e8b57)
43-
.setTitle('Last 5 Stations Heard')
44-
.setAuthor({ name: `${metadata.reflector_callsign}`, iconURL: `${icon}`, url: `${url}` })
45-
.addFields(
46-
{ name: 'Callsign', value: ' ', inline: true },
47-
{ name: 'On Module', value: ' ', inline: true },
48-
{ name: 'When', value: ' ', inline: true },
49-
);
50-
51-
for (var i in stations.stations) {
52-
if (i >= limit) break; // limit to 5
53-
console.log(stations.stations[i].callsign, stations.stations[i].vianode, stations.stations[i].callsignsuffix, stations.stations[i].onmodule, stations.stations[i].lastheard)
54-
embed.addFields(
55-
{ name: ' ', value: `${stations.stations[i].callsign} `, inline: true },
56-
{ name: ' ', value: `${stations.stations[i].onmodule} `, inline: true },
57-
{ name: ' ', value: TimeSince(new Date(stations.stations[i].lastheard)), inline: true },
58-
);
59-
}
60-
interaction.editReply({ embeds: [embed] });
61-
62-
},
24+
cooldown: 10,
25+
data: new SlashCommandBuilder()
26+
.setName('lastheard')
27+
.setDescription('Provides information about the last 5 stations heard on the reflector.'),
28+
29+
async execute(interaction) {
30+
31+
// retrieve last heard stations
32+
const { statusCode, body } = await request(url + '/json/stations');
33+
if (statusCode != 200) return interaction.editReply(`stations api returned error ${statusCode}.`);
34+
const stations = await body.json();
35+
if (!stations || !stations.stations) return interaction.editReply('No stations heard recently.');
36+
37+
// retrieve reflector metadata
38+
const { statusCode: metaStatusCode, body: metaBody } = await request(url + '/json/metadata');
39+
if (metaStatusCode != 200) return interaction.editReply(`metadata api returned error ${metaStatusCode}.`);
40+
const metadata = await metaBody.json();
41+
42+
const embed = new EmbedBuilder()
43+
.setColor(0x2e8b57)
44+
.setTitle('Last 5 Stations Heard')
45+
.setAuthor({ name: `${metadata.reflector_callsign}`, iconURL: `${icon}`, url: `${url}` })
46+
.addFields(
47+
{ name: 'Callsign', value: ' ', inline: true },
48+
{ name: 'On Module', value: ' ', inline: true },
49+
{ name: 'When', value: ' ', inline: true },
50+
);
51+
52+
for (const i in stations.stations) {
53+
// limit to 5
54+
if (i >= limit) break;
55+
console.log(stations.stations[i].callsign, stations.stations[i].vianode, stations.stations[i].callsignsuffix, stations.stations[i].onmodule, stations.stations[i].lastheard);
56+
embed.addFields(
57+
{ name: ' ', value: `${stations.stations[i].callsign} `, inline: true },
58+
{ name: ' ', value: `${stations.stations[i].onmodule} `, inline: true },
59+
{ name: ' ', value: TimeSince(new Date(stations.stations[i].lastheard)), inline: true },
60+
);
61+
}
62+
interaction.editReply({ embeds: [embed] });
63+
64+
},
6365
};

‎commands/reflector/links.js‎

Lines changed: 51 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,65 +5,66 @@ const { url, icon } = require('../../config.json');
55
const pluralize = (count, noun, suffix = 's') => `${count} ${noun}${count !== 1 ? suffix : ''}`;
66

77
function TimeSince(date) {
8-
var seconds = Math.floor((new Date() - date) / 1000);
9-
var interval = seconds / 31536000;
10-
if (interval > 1) return pluralize(Math.floor(interval), 'year') + ' ago';
11-
interval = seconds / 2592000;
12-
if (interval > 1) return pluralize(Math.floor(interval), 'month') + ' ago';
13-
interval = seconds / 86400;
14-
if (interval > 1) return pluralize(Math.floor(interval), 'day') + ' ago';
15-
interval = seconds / 3600;
16-
if (interval > 1) return pluralize(Math.floor(interval), 'hour') + ' ago';
17-
interval = seconds / 60;
18-
if (interval > 1) return pluralize(Math.floor(interval), 'minute') + ' ago';
19-
return pluralize(Math.floor(interval), 'second') + ' ago';
8+
const seconds = Math.floor((new Date() - date) / 1000);
9+
let interval = seconds / 31536000;
10+
if (interval > 1) return pluralize(Math.floor(interval), 'year') + ' ago';
11+
interval = seconds / 2592000;
12+
if (interval > 1) return pluralize(Math.floor(interval), 'month') + ' ago';
13+
interval = seconds / 86400;
14+
if (interval > 1) return pluralize(Math.floor(interval), 'day') + ' ago';
15+
interval = seconds / 3600;
16+
if (interval > 1) return pluralize(Math.floor(interval), 'hour') + ' ago';
17+
interval = seconds / 60;
18+
if (interval > 1) return pluralize(Math.floor(interval), 'minute') + ' ago';
19+
return pluralize(Math.floor(interval), 'second') + ' ago';
2020
}
2121

2222
module.exports = {
23-
cooldown: 10,
24-
data: new SlashCommandBuilder()
25-
.setName('links')
26-
.setDescription('Provides information about repeaters linked to the reflector.'),
23+
cooldown: 10,
24+
data: new SlashCommandBuilder()
25+
.setName('links')
26+
.setDescription('Provides information about repeaters linked to the reflector.'),
2727

28-
async execute(interaction) {
28+
async execute(interaction) {
2929

30-
// retrieve links
31-
var {statusCode, headers, body} = await request(url + '/json/links');
32-
if (statusCode != 200) return interaction.editReply(`links api returned error ${statusCode}.`);
33-
const links = await body.json();
30+
// retrieve links
31+
const { statusCode, body } = await request(url + '/json/links');
32+
if (statusCode != 200) return interaction.editReply(`links api returned error ${statusCode}.`);
33+
const links = await body.json();
34+
if (!links) return interaction.editReply('No links connected.');
3435

35-
// retrieve reflector metadata
36-
var {statusCode, headers, body} = await request(url + '/json/metadata');
37-
if (statusCode != 200) return interaction.editReply(`metadata api returned error ${statusCode}.`);
38-
const metadata = await body.json();
36+
// retrieve reflector metadata
37+
const { statusCode: metaStatusCode, body: metaBody } = await request(url + '/json/metadata');
38+
if (metaStatusCode != 200) return interaction.editReply(`metadata api returned error ${metaStatusCode}.`);
39+
const metadata = await metaBody.json();
3940

40-
const embed = new EmbedBuilder()
41-
.setColor(0x2e8b57)
42-
.setTitle('Linked Reflectors')
43-
.setAuthor({ name: `${metadata.reflector_callsign}`, iconURL: `${icon}`, url: `${url}` })
44-
.addFields(
45-
{ name: 'Callsign', value: ' ', inline: true },
46-
{ name: 'Module', value: ' ', inline: true },
47-
{ name: 'Connected', value: ' ', inline: true },
48-
);
49-
50-
let [callsign, module, connected] = ["", "", ""];
41+
const embed = new EmbedBuilder()
42+
.setColor(0x2e8b57)
43+
.setTitle('Linked Reflectors')
44+
.setAuthor({ name: `${metadata.reflector_callsign}`, iconURL: `${icon}`, url: `${url}` })
45+
.addFields(
46+
{ name: 'Callsign', value: ' ', inline: true },
47+
{ name: 'Module', value: ' ', inline: true },
48+
{ name: 'Connected', value: ' ', inline: true },
49+
);
5150

52-
for (var i in links) {
53-
console.log(links[i].callsign, links[i].ip, links[i].linkedmodule, links[i].protocol, links[i].connecttime, links[i].lastheardtime)
54-
callsign += links[i].callsign + ' / ' + links[i].protocol.replace("DMRMmdvm", "DMR") + '\n';
55-
module += links[i].linkedmodule + '\n';
56-
connected += TimeSince(new Date(links[i].connecttime)) + '\n';
51+
let [callsign, module, connected] = ['', '', ''];
5752

58-
}
53+
for (const i in links) {
54+
console.log(links[i].callsign, links[i].ip, links[i].linkedmodule, links[i].protocol, links[i].connecttime, links[i].lastheardtime);
55+
callsign += links[i].callsign + ' / ' + links[i].protocol.replace('DMRMmdvm', 'DMR') + '\n';
56+
module += links[i].linkedmodule + '\n';
57+
connected += TimeSince(new Date(links[i].connecttime)) + '\n';
5958

60-
embed.addFields(
61-
{ name: ' ', value: `${callsign} `, inline: true },
62-
{ name: ' ', value: `${module} `, inline: true },
63-
{ name: ' ', value: `${connected} `, inline: true },
64-
);
59+
}
6560

66-
interaction.editReply({ embeds: [embed] });
67-
68-
},
61+
embed.addFields(
62+
{ name: ' ', value: `${callsign} `, inline: true },
63+
{ name: ' ', value: `${module} `, inline: true },
64+
{ name: ' ', value: `${connected} `, inline: true },
65+
);
66+
67+
interaction.editReply({ embeds: [embed] });
68+
69+
},
6970
};

0 commit comments

Comments
 (0)