Keeping Discord channels up-to-date
Tags: discord, node, npm
Published: 2021-08-13 15:51:03

## Disclaimer

This uses [`channel-backup`](https://npm.im/channel-backup) that I have developed and published myself under the 'TinkerStorm' organization over the last year or so. Since its creation, it has gone through 6 iterations, only the most recent iteration being published.

{% github TinkerStorm/channel-backup %}

---

## Problem Theory

The problem most community managers may face when they need to update their information channels is that they have to update it themselves; ask a specific author; or know how to update a bot message using commands, web dashboard, etc.

While it may be a bit of a stretch to say that I've solved this problem, I think that it opens an opportunity for community members to help with contributions to the information channels instead of being reliant on an individual or selected group of delegates. *No harsh feelings towards those people though, I'm sure they do just fine.* 🙂

> Prior to being published, the script was just 180 lines stuffed into one file - which was sometimes iterated upon when I needed to do something *new* with it. :joy: I finally decided to motivate myself to publish it as someone had come to me, wanting to set it up on their own community.

## Explanation

The config can be setup to use direct file references or glob patterns that match to specific files in sequence. The program is *theoretically* accessible from an interface standpoint by importing its method directly into your own scripts, but is preferred from the command line.

```json
{
  "$schema": "https://github.com/TinkerStorm/channel-backup/raw/main/schemas/config.json",
  "files": {
    "./rules/*.md"
  }
}
```
> Matching to any markdown file in the 'rules' folder (but no sub-directory).

The webhook can be specified as part of the config if you would prefer to keep it among your staff team, however it isn't preferred. With the release of v1.1.1 yesterday, it added support to set `WEBHOOK_URL` as an environment variable to be loaded in.

A demo of this can be seen on TinkerStorm's [community repository], with it's folders, configurations and matrix run. When dispatched, this is sent directly to the [Discord server].

---

[Example workflow run](https://github.com/TinkerStorm/community/runs/3315881832?check_suite_focus=true) (expected to expire on Nov 10th, 2021)

```sh
# INFO_CHANNEL_WEBHOOK, ./info/config.json

> channel-backup-template@1.0.0 sequence
> channel-backup "--config" "./info/config.json"

Found 4 files in ./info/0*.md
steps(handle-message) './info/01_welcome.md' -> '875471689432039494'
steps(handle-message) './info/02_rules.md' -> '875471694070943744'
steps(handle-message) './info/03_channels.md' -> '875471698718253117'
steps(handle-message) './info/04_roles.md' -> '875471703394897950'
step(cleanup:update) 4 messages.

# README_CHANNEL_WEBHOOK, ./readme/config.json

> channel-backup-template@1.0.0 sequence
> channel-backup "--config" "./readme/config.json"

Found 2 files in ./readme/*-projects.md
Found 1 files in ./readme/community-upkeep.md
steps(handle-message) './readme/core-projects.md' -> '875475818036473917'
steps(handle-message) './readme/side-projects.md' -> '875475822620864584'
steps(handle-message) './readme/community-upkeep.md' -> '875475827444301825'
step(cleanup:update) 3 messages.

# ---
# {if cache.json was modified}
# set git name and email, then commit and push

[main 08c120d] auto: Update cache.json
 1 file changed, 1 insertion(+), 1 deletion(-)
To https://github.com/TinkerStorm/community
   6196053..08c120d  main -> main
```

## Conclusion

As an organization, we intend to expand this further into a more cohesive ecosystem that helps to better connect communities with their members.

If you want to set this up for your own server, make your own from our [repository template]. If you want to deploy on a CI workflow, feel free to use [our deploy workflow](https://github.com/TinkerStorm/community/blob/main/.github/workflows/update.yml) as a reference (which will also add the generated `cache.json` back into the repository. If you need any help with setting this up, drop by our [Discord server] and we'll see what we can do.

{% github RocketDragon/channel-backup-template no-readme %}

### Other possible uses

- Interval sticky message using GitHub Actions on a cron timer (with `--mode replace` active).

### Notes

> I am aware that [`discord.js@v13.x`](https://npm.im/discord.js) requires NodeJS 16.6.x or above, which should be patched with the release of `channel-backup@v1.1.2`. *Furthermore, we hope to further amend to community commitment by providing a custom webhook interface - designed for this library, which will hopefully support more LTS versions.*

If you attempt a matrix build and try to add a `${{ secrets.* }}` in as part of `${{ matrix.* }}`, the workflow will fail. Instead reference the key as part of the matrix and then use `${{ secrets[matrix.*] }}`.

I have also been made aware that multiple matrix runs cannot run in parallel, for which I have concluded the only way to resolve is to run sequential with `max-parallel: 1`. Payload trimming / sanitisation isn't done either, so entities like HTML comments become literal strings.

### Planned features include:

- GitHub Action Integration
  > Partial completion, with workflow demo.
- Payload validation and comparison
- Better cli support
- Better logging
- Unit tests
- Extensive plugin support

### Other possible features:

- [Discord Message Components](https://discord.dev/interactions/message-components) - Requires application webhook with a server listening
- Multi platform support (only if they have PATCH and DELETE endpoints)
  - Guilded
  - [Slack](https://api.slack.com/messaging/managing)

[repository]: https://github.com/TinkerStorm/channel-backup
[repository template]: https://github.com/RocketDragon/channel-backup-template
[community repository]: https://github.com/TinkerStorm/community
[project board]: https://github.com/TinkerStorm/channel-backup/projects/1
[Discord server]: https://discord.gg/Bb3JQQG