uv script that fetches messages from a channel for the last N hours via the Discord REST API and saves them as JSON for use with jq.
37 lines
971 B
Markdown
37 lines
971 B
Markdown
# pull-messages-from-discord
|
|
|
|
Pull messages from a Discord channel for the last N hours and save them as JSON.
|
|
|
|
## Setup
|
|
|
|
Put the bot token in `.env` (gitignored):
|
|
|
|
```
|
|
HM_DISCORD_BOT_TOKEN=...
|
|
```
|
|
|
|
The bot needs View Channel + Read Message History on the channel, and the
|
|
Message Content Intent enabled in the Developer Portal (otherwise `content`
|
|
and `embeds` come back empty).
|
|
|
|
## Usage
|
|
|
|
```
|
|
uv run script.py --channel homelab-alerts --time 24h
|
|
uv run script.py -c homelab-alerts -t 24
|
|
```
|
|
|
|
Options: `-c/--channel` (required), `-t/--time` hours, `24` or `24h` (default 24),
|
|
`-g/--guild` name or ID if the channel name is ambiguous, `-o/--output` file path.
|
|
|
|
Output goes next to the script: `discord_<channel>_<UTC timestamp>.json`.
|
|
|
|
## jq examples
|
|
|
|
```
|
|
jq '.count' discord_homelab-alerts_*.json
|
|
jq -r '.messages[] | "\(.timestamp) \(.content)"' file.json
|
|
jq '.messages[].embeds[].title' file.json
|
|
jq '.messages[] | select(.content | test("error"; "i"))' file.json
|
|
```
|