Discord roles
Give a role on your Discord server to every customer with a license, and take it back when the license ends. You bring your own Discord application, so the bot carries your name and only sits on your server. Customers link their Discord account in the customer portal.
Settings are per environment: connect a test server in test and your real server in live. All calls below need an API key with discord:write (discord:read to read).
1. Create a Discord application
- In Discord's developer portal, create an application. Copy the application ID.
- Under OAuth2, copy the client secret and add the redirect URI that
GET /v1/discordnames, such ashttps://portal.example.com/portal/discord/callback. It follows your own portal domain once you have one, so check it again after adding a domain. - Under Bot, create the bot and copy its token.
- Invite the bot to your server with the Manage Roles permission. In your server's role list, drag the bot's role above every role it should give: Discord only lets a bot give roles below its own.
- Turn on developer mode in Discord, right-click your server and copy the server ID.
2. Connect
In the dashboard, open Discord under the settings of the environment, copy the redirect URI from there into your application and enter the application ID, the server ID, the client secret and the bot token. Or through the API:
curl -X PUT https://api.example.com/v1/discord \
-H "Authorization: Bearer $API_KEY" -H "Environment: live" -H "Content-Type: application/json" \
-d '{
"application_id": "1187654321098765432",
"guild_id": "1123456789012345678",
"client_secret": "...",
"bot_token": "..."
}'
Noctix checks with the token that the bot is on the server before it saves anything. The secret and token are stored encrypted and are never returned. To change the server later, send application_id and guild_id without the secrets; they stay. Roles given on the old server are then forgotten, not taken back.
| Error | Meaning |
|---|---|
discord_credentials_invalid |
Discord refused the bot token |
discord_bot_not_in_server |
The bot is not on that server |
discord_unavailable |
Discord could not be reached; try again |
3. Add role rules
On the dashboard page, Add rule picks a role of your server and a plan or an entitlement; the page also lists linked customers with their sync state and has Sync now. Through the API, GET /v1/discord/roles lists the roles of your server. A rule gives one role for one plan, or for every plan that grants an entitlement:
curl -X POST https://api.example.com/v1/discord/role-rules \
-H "Authorization: Bearer $API_KEY" -H "Environment: live" -H "Content-Type: application/json" \
-d '{ "role_id": "1133333333333333333", "plan_id": "plan_pro" }'
curl -X POST https://api.example.com/v1/discord/role-rules \
-H "Authorization: Bearer $API_KEY" -H "Environment: live" -H "Content-Type: application/json" \
-d '{ "role_id": "1144444444444444444", "entitlement": "priority_support" }'
An entitlement counts unless its value in the plan is false or 0. DELETE /v1/discord/role-rules/{id} removes a rule.
4. Customers link their account
In the customer portal, the card Discord appears once you connected. Customers choose Link Discord and allow it on Discord's page. Noctix only asks for identify: the Discord user ID and username, nothing else. Roles come at once. A Discord account can be linked to one customer only. A customer must be on your server to get a role; if they are not, the link shows not_in_server and the roles come in the next round after they join.
How roles stay in sync
Every five minutes Noctix compares, for every linked customer, the roles due from your rules with the roles it gave:
- A license counts while it is unused or active and its term plus grace days has not ended.
- Missing roles are given; roles no longer due are taken back. An expired, suspended or revoked license loses its role within one round.
- Noctix only takes back roles it gave itself. Roles you give by hand stay.
POST /v1/discord/syncruns the round at once for this environment.
GET /v1/discord/links lists linked customers with granted_roles and sync_error:
sync_error |
What to do |
|---|---|
not_in_server |
The customer has to join your server |
missing_permissions |
Give the bot Manage Roles and move its role above the roles it gives |
unknown_role |
A rule names a role that no longer exists; delete the rule |
bot_token_invalid |
Connect again with a new bot token |
rate_limited, discord_unavailable |
Nothing; the next round tries again |
Unlinking in the portal takes back the roles Noctix gave. DELETE /v1/discord disconnects; rules and links stay, and roles already given stay on your server until you connect again.
Events
discord.connected, discord.updated, discord.disconnected, discord.role_rule_created, discord.role_rule_deleted, discord.linked, discord.unlinked and discord.roles_synced (only when roles changed or the reason changed), in the audit log (GET /v1/events).
Post events to a channel
A webhook endpoint in the format discord posts a short message to a channel of your server for each event it subscribed to, such as new licenses or paid orders. In Discord, open the channel's settings, Integrations, Webhooks, create one and copy its URL. Then:
curl -X POST https://api.example.com/v1/webhook-endpoints \
-H "Authorization: Bearer $API_KEY" -H "Environment: live" -H "Content-Type: application/json" \
-d '{"url": "https://discord.com/api/webhooks/123/abc", "format": "discord",
"event_types": ["license.created", "order.paid"]}'
The message names the event type, the environment, the subject (such as the license ID), who caused it and when, never the event's data, and it pings nobody. Delivery and retries work as for every endpoint (webhooks); the format cannot be changed later, and such an endpoint has no signing secret.