-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.lua
More file actions
215 lines (187 loc) · 7.7 KB
/
Copy pathserver.lua
File metadata and controls
215 lines (187 loc) · 7.7 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
-- Error Check
if DiscordWebhookSystemInfos == nil and DiscordWebhookKillinglogs == nil and DiscordWebhookChat == nil then
local Content = LoadResourceFile(GetCurrentResourceName(), 'config.lua')
Content = load(Content)
Content()
end
if DiscordWebhookSystemInfos == 'WEBHOOK_LINK_HERE' then
print('\n\nERROR\n' .. GetCurrentResourceName() .. ': Please add your "System Infos" webhook\n\n')
else
PerformHttpRequest(DiscordWebhookSystemInfos, function(Error, Content, Head)
if Content == '{"code": 50027, "message": "Invalid Webhook Token"}' then
print('\n\nERROR\n' .. GetCurrentResourceName() .. ': "System Infos" webhook non-existing!\n\n')
end
end)
end
if DiscordWebhookKillinglogs == 'WEBHOOK_LINK_HERE' then
print('\n\nERROR\n' .. GetCurrentResourceName() .. ': Please add your "Killing Log" webhook\n\n')
else
PerformHttpRequest(DiscordWebhookKillinglogs, function(Error, Content, Head)
if Content == '{"code": 50027, "message": "Invalid Webhook Token"}' then
print('\n\nERROR\n' .. GetCurrentResourceName() .. ': "Killing Log" webhook non-existing!\n\n')
end
end)
end
if DiscordWebhookChat == 'WEBHOOK_LINK_HERE' then
print('\n\nERROR\n' .. GetCurrentResourceName() .. ': Please add your "Chat" webhook\n\n')
else
PerformHttpRequest(DiscordWebhookChat, function(Error, Content, Head)
if Content == '{"code": 50027, "message": "Invalid Webhook Token"}' then
print('\n\nERROR\n' .. GetCurrentResourceName() .. ': "Chat" webhook non-existing!\n\n')
end
end)
end
-- System Infos
PerformHttpRequest(DiscordWebhookSystemInfos, function(Error, Content, Head) end, 'POST', json.encode({username = SystemName, content = '**FiveM server is now online!**'}), { ['Content-Type'] = 'application/json' })
AddEventHandler('playerConnecting', function()
TriggerEvent('FiveMLogs:ToDiscord', DiscordWebhookSystemInfos, SystemName, '```css\n' .. GetPlayerName(source) .. ' is connecting to the server\n```', SystemAvatar, false)
end)
AddEventHandler('playerDropped', function(Reason)
TriggerEvent('FiveMLogs:ToDiscord', DiscordWebhookSystemInfos, SystemName, '```fix\n' .. GetPlayerName(source) .. ' left (' .. Reason .. ')\n```', SystemAvatar, false)
end)
-- Killing Log
RegisterServerEvent('FiveMLogs:playerDied')
AddEventHandler('FiveMLogs:playerDied', function(Message, Weapon)
local date = os.date('*t')
if date.day < 10 then date.day = '0' .. tostring(date.day) end
if date.month < 10 then date.month = '0' .. tostring(date.month) end
if date.hour < 10 then date.hour = '0' .. tostring(date.hour) end
if date.min < 10 then date.min = '0' .. tostring(date.min) end
if date.sec < 10 then date.sec = '0' .. tostring(date.sec) end
if Weapon then
Message = Message .. ' [' .. Weapon .. ']'
end
TriggerEvent('FiveMLogs:ToDiscord', DiscordWebhookKillinglogs, SystemName, Message .. ' `' .. date.day .. '.' .. date.month .. '.' .. date.year .. ' - ' .. date.hour .. ':' .. date.min .. ':' .. date.sec .. '`', SystemAvatar, false)
end)
-- Chat
AddEventHandler('chatMessage', function(Source, Name, Message)
local Webhook = DiscordWebhookChat; TTS = false
--Removing Color Codes (^0, ^1, ^2 etc.) from the name and the message
for i = 0, 9 do
Message = Message:gsub('%^' .. i, '')
Name = Name:gsub('%^' .. i, '')
end
--Splitting the message in multiple strings
MessageSplitted = stringsplit(Message, ' ')
--Checking if the message contains a command which has his own webhook
if IsCommand(MessageSplitted, 'HavingOwnWebhook') then
Webhook = GetOwnWebhook(MessageSplitted)
end
--Combining the message to one string again
Message = ''
for Key, Value in ipairs(MessageSplitted) do
Message = Message .. Value .. ' '
end
--Adding the username if needed
Message = Message:gsub('USERNAME_NEEDED_HERE', GetPlayerName(Source))
--Adding the userid if needed
Message = Message:gsub('USERID_NEEDED_HERE', Source)
-- Shortens the Name, if needed
if Name:len() > 23 then
Name = Name:sub(1, 23)
end
--Getting the steam avatar if available
local AvatarURL = UserAvatar
if GetIDFromSource('steam', Source) then
PerformHttpRequest('http://steamcommunity.com/profiles/' .. tonumber(GetIDFromSource('steam', Source), 16) .. '/?xml=1', function(Error, Content, Head)
local SteamProfileSplitted = stringsplit(Content, '\n')
for i, Line in ipairs(SteamProfileSplitted) do
if Line:find('<avatarFull>') then
AvatarURL = Line:gsub(' <avatarFull><!%[CDATA%[', ''):gsub(']]></avatarFull>', '')
TriggerEvent('FiveMLogs:ToDiscord', Webhook, Name .. ' [ID: ' .. Source .. ']', Message, AvatarURL, false, Source, TTS) --Sending the message to discord
break
end
end
end)
else
--Using the default avatar if no steam avatar is available
TriggerEvent('FiveMLogs:ToDiscord', Webhook, Name .. ' [ID: ' .. Source .. ']', Message, AvatarURL, false, Source, TTS) --Sending the message to discord
end
end)
--Event to actually send Messages to Discord
RegisterServerEvent('FiveMLogs:ToDiscord')
AddEventHandler('FiveMLogs:ToDiscord', function(WebHook, Name, Message, Image, External, Source, TTS)
if Message == nil or Message == '' then
return nil
end
if TTS == nil or TTS == '' then
TTS = false
end
if External then
if WebHook:lower() == 'chat' then
WebHook = DiscordWebhookChat
elseif WebHook:lower() == 'system' then
WebHook = DiscordWebhookSystemInfos
elseif WebHook:lower() == 'kill' then
WebHook = DiscordWebhookSystemInfos
elseif WebHook:lower() == 'staff' then
WebHook = DiscordWebhookStafflogs
elseif not Webhook:find('discord.com/api/webhooks') then
print('FiveMLogs event called without a specified webhook!')
return nil
end
if Image:lower() == 'steam' then
Image = UserAvatar
if GetIDFromSource('steam', Source) then
PerformHttpRequest('http://steamcommunity.com/profiles/' .. tonumber(GetIDFromSource('steam', Source), 16) .. '/?xml=1', function(Error, Content, Head)
local SteamProfileSplitted = stringsplit(Content, '\n')
for i, Line in ipairs(SteamProfileSplitted) do
if Line:find('<avatarFull>') then
Image = Line:gsub(' <avatarFull><!%[CDATA%[', ''):gsub(']]></avatarFull>', '')
return PerformHttpRequest(WebHook, function(Error, Content, Head) end, 'POST', json.encode({username = Name, content = Message, avatar_url = Image, tts = TTS}), {['Content-Type'] = 'application/json'})
end
end
end)
end
elseif Image:lower() == 'user' then
Image = UserAvatar
else
Image = SystemAvatar
end
end
PerformHttpRequest(WebHook, function(Error, Content, Head) end, 'POST', json.encode({username = Name, content = Message, avatar_url = Image, tts = TTS}), {['Content-Type'] = 'application/json'})
end)
-- Functions
function IsCommand(String, Type)
if Type == 'HavingOwnWebhook' then
for i, OwnWebhookCommand in ipairs(OwnWebhookCommands) do
if String[1]:lower() == OwnWebhookCommand[1]:lower() then
return true
end
end
end
return false
end
function GetOwnWebhook(String)
for i, OwnWebhookCommand in ipairs(OwnWebhookCommands) do
if String[1]:lower() == OwnWebhookCommand[1]:lower() then
if OwnWebhookCommand[2] == 'WEBHOOK_LINK_HERE' then
print('Please enter a webhook link for the command: ' .. String[1])
return DiscordWebhookChat
else
return OwnWebhookCommand[2]
end
end
end
end
function stringsplit(input, seperator)
if seperator == nil then
seperator = '%s'
end
local t={} ; i=1
for str in string.gmatch(input, '([^'..seperator..']+)') do
t[i] = str
i = i + 1
end
return t
end
function GetIDFromSource(Type, ID)
local IDs = GetPlayerIdentifiers(ID)
for k, CurrentID in pairs(IDs) do
local ID = stringsplit(CurrentID, ':')
if (ID[1]:lower() == string.lower(Type)) then
return ID[2]:lower()
end
end
return nil
end