forked from iamvillain/redm_loadscreen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.lua
More file actions
155 lines (137 loc) · 7.49 KB
/
server.lua
File metadata and controls
155 lines (137 loc) · 7.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
local latestChangelogEntry = nil -- Stores the single latest entry { content, author, timestamp }
local backgroundImageUrlList = {} -- Stores a list of image URLs for the background
-- Function to fetch background images
function FetchBackgroundImages()
if not Config.EnableDiscordBackgrounds then return end
local botToken = Config.DiscordBotToken
if not botToken or botToken == '' or botToken == 'YOUR_DISCORD_BOT_TOKEN_HERE' then
return
end
if not Config.DiscordBackgroundChannelID or Config.DiscordBackgroundChannelID == 'YOUR_BACKGROUND_IMAGE_CHANNEL_ID_HERE' then
return
end
local messageLimit = Config.DiscordBackgroundMessageLimit or 50
local url = ('https://discord.com/api/v10/channels/%s/messages?limit=%d'):format(Config.DiscordBackgroundChannelID, messageLimit)
local headers = {
['Authorization'] = 'Bot ' .. botToken,
['Content-Type'] = 'application/json'
}
PerformHttpRequest(url, function(errorCode, responseData, responseHeaders)
if errorCode == 200 then
local data = json.decode(responseData)
if data and type(data) == 'table' then
local foundImageUrls = {}
for _, message in ipairs(data) do
if message then
if message.attachments and type(message.attachments) == 'table' and #message.attachments > 0 then
for _, attachment in ipairs(message.attachments) do
if attachment then
local isImage = false -- Default to false
local contentType = attachment.content_type or ''
local attachmentUrl = attachment.url -- Store URL locally
-- 1. Primary Check: Content Type (Simpler Substring Check)
-- Check if the string starts with "image/"
-- Add a length check first to prevent error if contentType is too short
if #contentType >= 6 and contentType:sub(1, 6) == 'image/' then
isImage = true
-- 2. Fallback Check: URL Extension (only if primary failed AND URL exists)
elseif attachmentUrl then
local urlPath = attachmentUrl:match("([^?]+)") -- Get URL part before any '?'
if urlPath then
if string.match(urlPath:lower(), '%.(jpe?g|png|gif|webp)$') then
isImage = true
end
end
end
-- Final Decision
if isImage and attachmentUrl then
table.insert(foundImageUrls, attachmentUrl)
end
end
end
end
if message.embeds and type(message.embeds) == 'table' then
for _, embed in ipairs(message.embeds) do
if embed.image and embed.image.url then
local imageUrl = embed.image.url
if imageUrl:match("([^?]+)") and imageUrl:lower():match("%.jpe?g$") or imageUrl:lower():match("%.png$") or imageUrl:lower():match("%.gif$") or imageUrl:lower():match("%.webp$") then
table.insert(foundImageUrls, imageUrl)
end
end
end
end
end
end
-- Check if any URLs were actually found
if #foundImageUrls > 0 then
backgroundImageUrlList = foundImageUrls -- Replace the old list
end
end
end
end, 'GET', '', headers)
end
function FetchLatestChangelog()
if not Config.EnableChangelog then return end
local botToken = Config.DiscordBotToken -- Direct from Config (Less Secure)
if not botToken or botToken == '' or botToken == 'YOUR_DISCORD_BOT_TOKEN_HERE' then
return
end
if not Config.DiscordChannelID or Config.DiscordChannelID == 'YOUR_DISCORD_CHANNEL_ID_HERE' then
return
end
local url = ('https://discord.com/api/v10/channels/%s/messages?limit=1'):format(Config.DiscordChannelID)
local headers = {
['Authorization'] = 'Bot ' .. botToken,
['Content-Type'] = 'application/json'
}
PerformHttpRequest(url, function(errorCode, responseData, responseHeaders)
if errorCode == 200 then
local data = json.decode(responseData) -- Use built-in JSON decode
if data and type(data) == 'table' and #data > 0 then
local message = data[1] -- Get the first (and only) message
-- Add safety check for message object itself
if message and message.content and message.content ~= "" then
-- Add safety check for author object
local authorName = "Unknown"
if message.author and message.author.username then
authorName = message.author.username
end
latestChangelogEntry = {
content = message.content,
author = authorName,
timestamp = message.timestamp
}
else
latestChangelogEntry = nil -- Clear if latest message is empty/invalid or message object is malformed
end
else
latestChangelogEntry = nil -- Clear cache on parse failure or empty response
end
end
end, 'GET', '', headers)
end
SetTimeout(3000, FetchLatestChangelog)
SetTimeout(4000, FetchBackgroundImages)
AddEventHandler('playerConnecting', function(playerName, setKickReason, deferrals)
local source = source
local connectingPlayerName = GetPlayerName(source) or playerName or "Player" -- Add fallback
local serverProjectName = GetConvar('sv_projectName', 'RedM Server') -- Get the convar, provide a fallback name
serverProjectName = serverProjectName:gsub('%^%d', '')
local handoverData = {
playerName = connectingPlayerName,
serverName = serverProjectName,
logoUrl = Config.LogoURL,
-- logoPosition = Config.LogoPosition, -- Send table posibles positions in loadscreen
enableMusic = Config.EnableMusic,
youtubeVideoIds = Config.YouTubeVideoID,
initialVolume = Config.InitialVolume,
messages = Config.Messages,
enableChangelog = Config.EnableChangelog,
changelogTitle = Config.ChangelogTitle,
changelogEntry = latestChangelogEntry, -- Send the single cached entry object (can be nil)
enableDiscordBackgrounds = Config.EnableDiscordBackgrounds,
backgroundImageUrls = backgroundImageUrlList -- Send the list of URLs
}
-- TriggerClientEvent('basic_loadscreen:client:startLoading', source, true)
deferrals.handover(handoverData)
end)