Add Discord channel message puller
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.
This commit is contained in:
1 parent
b45cd20452
commit
8f27a5fc1a
3 files changed
+197
No files matched your search
@@ -0,0 +1,36 @@
|
||||
# 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
|
||||
```
|
||||
Reference in new issue
Block a user