Discord API Integration

Overview

The Discord API Integration provides a comprehensive interface for interacting with Discord guild members through the Discord REST API. This system allows you to manage guild members, roles, and nicknames directly from your server application.

Prerequisites

Configuration Requirements

  1. Discord Bot Token: Set up a Discord application and bot at Discord Developer Portal

  2. Server Configuration: Add the bot token to your server configuration:

    set discord_bot_token "YOUR_BOT_TOKEN_HERE"
  3. Guild ID: Configure your Discord guild ID in the Config table:

    Config.DiscordGuildId = "YOUR_GUILD_ID_HERE"

Bot Permissions

Your Discord bot requires the following permissions in your guild:

  • Manage Roles: To assign and remove roles

  • Manage Nicknames: To change member nicknames

  • View Server Members: To fetch member information

API Reference

API.GetDiscordGuildMember(discordId)

Retrieves detailed information about a specific guild member from Discord.

Parameters

  • discordId (string): The Discord user ID of the member to fetch

Returns

  • member (table): Discord guild member object containing user data, roles, and guild-specific information

  • nil: If the request fails or member is not found

Example

Response Structure


API.GetDiscordNameCached(discordId)

Retrieves a cached Discord username for performance optimization.

Parameters

  • discordId (string): The Discord user ID to retrieve cached name for

Returns

  • username (string): The cached username, or empty string if not cached

  • nil: If discordId is not provided

Example

Notes

  • This function provides instant access to previously fetched usernames

  • Returns empty string if the user hasn't been cached yet

  • Use this for performance-critical operations


API.DefineDiscordMemberRole(discordId, roleId)

Assigns a role to a Discord guild member.

Parameters

  • discordId (string): The Discord user ID of the member

  • roleId (string): The Discord role ID to assign

Returns

  • response (table): Discord API response object

  • nil: If discordId is not provided

Example

Notes

  • The member must be in the guild

  • The bot must have permission to manage roles

  • The bot's highest role must be higher than the role being assigned


API.GetDiscordRolesFromUser(discordId)

Retrieves all roles assigned to a specific guild member.

Parameters

  • discordId (string): The Discord user ID to query

Returns

  • roles (table): Array of role IDs assigned to the user

  • nil: If discordId is not provided or user not found

Example


API.DiscordMemberHasRole(discordId, roleId)

Checks if a guild member has a specific role.

Parameters

  • discordId (string): The Discord user ID to check

  • roleId (string): The Discord role ID to verify

Returns

  • hasRole (boolean): True if the member has the role, false otherwise

Example

Use Cases

  • Permission verification

  • Access control systems

  • Role-based feature unlocking


API.RemoveDiscordMemberRole(discordId, roleId)

Removes a role from a Discord guild member.

Parameters

  • discordId (string): The Discord user ID of the member

  • roleId (string): The Discord role ID to remove

Returns

  • response (table): Discord API response object

  • nil: If discordId is not provided

Example

Notes

  • The member must currently have the role

  • The bot must have permission to manage roles

  • The bot's highest role must be higher than the role being removed


API.GetDiscordMemberName(discordId)

Fetches and caches the display name of a Discord guild member.

Parameters

  • discordId (string): The Discord user ID to fetch the name for

Returns

  • username (string): The member's display name (global_name if available, otherwise username)

  • nil: If discordId is not provided or member not found

Example

Name Priority

  1. global_name: Discord's new display name system

  2. username: Fallback to traditional username

Caching Behavior

  • Automatically caches the retrieved name for performance

  • Subsequent calls to GetDiscordNameCached will return this cached value


API.DefineDiscordMemberName(discordId, name)

Sets the server nickname for a Discord guild member.

Parameters

  • discordId (string): The Discord user ID of the member

  • name (string, optional): The new nickname to set. If not provided, uses cached name

Returns

  • response (table): Discord API response object

  • nil: If discordId is not provided

Example

Notes

  • The bot must have "Manage Nicknames" permission

  • Cannot change the nickname of users with higher roles than the bot

  • Passing nil or empty name uses the cached username

Usage Examples

Role Management System

User Information Display

Event-Based Role Assignment

Batch Operations

Best Practices

Error Handling

Rate Limiting

Caching Strategy

Troubleshooting

Common Issues

"Unauthorized" Error (401)

  • Verify your bot token is correct

  • Ensure the bot is invited to your Discord guild

  • Check that the bot has necessary permissions

"Forbidden" Error (403)

  • Bot lacks required permissions (Manage Roles, Manage Nicknames)

  • Bot's role is lower than the target role in hierarchy

  • Bot cannot modify users with higher roles

"Not Found" Error (404)

  • Discord user ID is invalid

  • User is not a member of the specified guild

  • Role ID doesn't exist in the guild

Rate Limiting (429)

  • Too many requests in a short period

  • Implement delays between requests

  • Use batch operations efficiently

Debug Information

Configuration Validation

Security Considerations

  1. Token Security: Never expose your bot token in client-side code

  2. Permission Validation: Always verify user permissions before role operations

  3. Input Sanitization: Validate Discord IDs and role IDs before API calls

  4. Rate Limiting: Implement proper rate limiting to avoid API abuse

  5. Error Logging: Log failed requests for security monitoring

API Limits

  • Rate Limit: 50 requests per minute per endpoint

  • Burst Limit: 5 requests per 5 seconds

  • Global Limit: 50 requests per second across all endpoints

  • Role Hierarchy: Bots cannot modify users with higher roles

Last updated