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.
local member = API.GetDiscordGuildMember("123456789012345678")
if member then
print("Member nickname:", member.nick)
print("Member roles count:", #member.roles)
print("Username:", member.user.username)
else
print("Member not found or error occurred")
end
local cachedName = API.GetDiscordNameCached("123456789012345678")
if cachedName and cachedName ~= "" then
print("Cached name:", cachedName)
else
-- Need to fetch from Discord API
local freshName = API.GetDiscordMemberName("123456789012345678")
end
-- Assign VIP role to a member
local vipRoleId = "987654321098765432"
local response = API.DefineDiscordMemberRole("123456789012345678", vipRoleId)
if response then
print("Role assigned successfully")
else
print("Failed to assign role")
end
local userRoles = API.GetDiscordRolesFromUser("123456789012345678")
if userRoles then
print("User has", #userRoles, "roles:")
for i, roleId in ipairs(userRoles) do
print("Role", i, ":", roleId)
end
else
print("Could not fetch user roles")
end
local adminRoleId = "111222333444555666"
local isAdmin = API.DiscordMemberHasRole("123456789012345678", adminRoleId)
if isAdmin then
print("User is an administrator")
-- Grant admin permissions
else
print("User is not an administrator")
-- Restrict access
end
-- Remove temporary role after event ends
local tempRoleId = "999888777666555444"
local response = API.RemoveDiscordMemberRole("123456789012345678", tempRoleId)
if response then
print("Role removed successfully")
else
print("Failed to remove role")
end
local displayName = API.GetDiscordMemberName("123456789012345678")
if displayName then
print("Member's display name:", displayName)
-- Name is now cached for future use
else
print("Could not fetch member name")
end
-- Set a custom nickname for a member
local response = API.DefineDiscordMemberName("123456789012345678", "VIP Player John")
if response then
print("Nickname updated successfully")
end
-- Reset to cached name (if available)
local response2 = API.DefineDiscordMemberName("123456789012345678")
-- Create a role-based permission system
local ROLES = {
ADMIN = "111111111111111111",
MODERATOR = "222222222222222222",
VIP = "333333333333333333",
MEMBER = "444444444444444444"
}
function PromoteToModerator(discordId)
-- Check if user is already admin
if API.DiscordMemberHasRole(discordId, ROLES.ADMIN) then
return false, "User is already an admin"
end
-- Remove member role and add moderator role
API.RemoveDiscordMemberRole(discordId, ROLES.MEMBER)
local success = API.DefineDiscordMemberRole(discordId, ROLES.MODERATOR)
if success then
return true, "Promoted to moderator successfully"
else
return false, "Failed to promote user"
end
end
function GetUserInfo(discordId)
-- Try to get cached name first for performance
local name = API.GetDiscordNameCached(discordId)
if not name or name == "" then
-- Fetch fresh name if not cached
name = API.GetDiscordMemberName(discordId)
end
-- Get user's roles
local roles = API.GetDiscordRolesFromUser(discordId)
local roleCount = roles and #roles or 0
return {
name = name or "Unknown",
roleCount = roleCount,
isVIP = API.DiscordMemberHasRole(discordId, ROLES.VIP)
}
end
-- Assign roles based on in-game achievements
function OnPlayerAchievement(playerId, achievementType)
local discordId = GetPlayerDiscordId(playerId) -- Your implementation
if not discordId then return end
local roleId = nil
local nickname = nil
if achievementType == "first_win" then
roleId = "555555555555555555" -- Winner role
nickname = "🏆 " .. API.GetDiscordNameCached(discordId)
elseif achievementType == "level_50" then
roleId = "666666666666666666" -- Veteran role
nickname = "⭐ " .. API.GetDiscordNameCached(discordId)
end
if roleId then
API.DefineDiscordMemberRole(discordId, roleId)
end
if nickname then
API.DefineDiscordMemberName(discordId, nickname)
end
end
-- Process multiple users efficiently
function UpdateMultipleUsers(discordIds, roleId, action)
local results = {}
for i, discordId in ipairs(discordIds) do
local success = false
if action == "add" then
success = API.DefineDiscordMemberRole(discordId, roleId) ~= nil
elseif action == "remove" then
success = API.RemoveDiscordMemberRole(discordId, roleId) ~= nil
end
results[discordId] = success
-- Add delay to respect rate limits
if i % 10 == 0 then
Citizen.Wait(1000) -- Wait 1 second every 10 requests
end
end
return results
end
function SafeRoleCheck(discordId, roleId)
if not discordId or not roleId then
print("Invalid parameters provided")
return false
end
local success, hasRole = pcall(API.DiscordMemberHasRole, discordId, roleId)
if not success then
print("Error checking role for user:", discordId)
return false
end
return hasRole
end
-- Implement rate limiting for bulk operations
local rateLimiter = {
requests = 0,
resetTime = 0
}
function MakeRateLimitedRequest(requestFunction, ...)
local currentTime = GetGameTimer()
if currentTime > rateLimiter.resetTime then
rateLimiter.requests = 0
rateLimiter.resetTime = currentTime + 60000 -- Reset every minute
end
if rateLimiter.requests >= 50 then -- Discord rate limit
print("Rate limit reached, waiting...")
Citizen.Wait(rateLimiter.resetTime - currentTime)
rateLimiter.requests = 0
end
rateLimiter.requests = rateLimiter.requests + 1
return requestFunction(...)
end
-- Implement smart caching for frequently accessed data
local cacheExpiry = {}
local CACHE_DURATION = 300000 -- 5 minutes
function GetDiscordNameSmart(discordId)
local currentTime = GetGameTimer()
local cachedName = API.GetDiscordNameCached(discordId)
-- Check if cache is expired
if cachedName ~= "" and cacheExpiry[discordId] and currentTime < cacheExpiry[discordId] then
return cachedName
end
-- Fetch fresh data
local freshName = API.GetDiscordMemberName(discordId)
if freshName then
cacheExpiry[discordId] = currentTime + CACHE_DURATION
end
return freshName
end
function DebugDiscordAPI(discordId)
print("=== Discord API Debug ===")
print("Discord ID:", discordId)
-- Test member fetch
local member = API.GetDiscordGuildMember(discordId)
if member then
print("✅ Member found")
print("Username:", member.user.username)
print("Nickname:", member.nick or "None")
print("Roles count:", #member.roles)
else
print("❌ Member not found")
end
-- Test cached name
local cachedName = API.GetDiscordNameCached(discordId)
print("Cached name:", cachedName ~= "" and cachedName or "Not cached")
print("=== End Debug ===")
end
function ValidateDiscordConfig()
local token = GetConvar("discord_bot_token", '')
local guildId = Config.DiscordGuildId
if token == '' then
print("ERROR: Discord bot token not configured")
return false
end
if not guildId or guildId == '' then
print("ERROR: Discord guild ID not configured")
return false
end
print("✅ Discord configuration appears valid")
return true
end