-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.lua
167 lines (138 loc) · 4.3 KB
/
server.lua
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
CreateThread(function()
MySQL.query([[
CREATE TABLE IF NOT EXISTS `friends` (
`dbID` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`identifier` VARCHAR(64) NOT NULL DEFAULT '',
`friend` VARCHAR(64) NULL DEFAULT NULL,
`discordId` VARCHAR(64) NULL DEFAULT NULL,
`add_date` DATETIME NULL DEFAULT NOW(),
`pending` INT(1) NULL DEFAULT '1'
)
COLLATE='utf8mb4_unicode_ci';
]])
end)
function getPlayerFriends(player, typ)
local xPlayer = false
if type(player) == "table" then
xPlayer = player
else
xPlayer = ESX.GetPlayerFromId(player)
end
return MySQL.query.await(
[[
SELECT friends.*, CONCAT(users.firstname, ' ', users.lastname) AS charName
FROM friends, users
WHERE friends.identifier = ? AND users.identifier=friends.friend
]],
{ xPlayer.identifier }
)
end
exports("getPlayerFriends", getPlayerFriends)
ESX.RegisterServerCallback("requestPlayerFriends", function(player, cb)
cb(getPlayerFriends(player))
end)
function getPlayerDiscordName(player)
local ids = GetPlayerIdentifiers(player)
local discordId = false
for _, id in pairs(ids) do
if id:find("discord:") then
discordId = id:gsub("discord:", "")
break
end
end
if not discordId then
return "Unknown"
end
local p = promise.new()
PerformHttpRequest(
"https://discord.com/api/v9/users/" .. discordId,
function(code, result)
if code ~= 200 then
return p:resolve("Unknown")
end
result = json.decode(result)
p:resolve(result.username .. " #" .. result.discriminator)
end,
"GET",
"",
{
Authorization = "Bot " .. DISCORD_BOT_TOKEN,
}
)
return Citizen.Await(p)
end
exports("getPlayerDiscordName", getPlayerDiscordName)
RegisterCommand(PANEL_COMMAND, function(player)
TriggerClientEvent("openFriendsPanel", player, getPlayerFriends(player))
end)
ESX.RegisterServerCallback("newFriendRequest", function(player, cb, targetId)
if not targetId then
return cb(false, "Player not found!")
end
local xPlayer = ESX.GetPlayerFromId(player)
if not xPlayer then
return cb(false, "Error!")
end
local xTarget = ESX.GetPlayerFromId(tonumber(targetId))
if not xTarget then
return cb(false, "Player not found!")
end
local exists = MySQL.scalar.await(
"SELECT dbID FROM friends WHERE identifier = ? AND friend = ?",
{ xPlayer.identifier, xTarget.identifier }
)
if exists then
return cb(false, "You are already friends or have a pending request.")
end
MySQL.insert.await(
"INSERT INTO friends SET identifier = ?, friend = ?, pending = 0",
{ xPlayer.identifier, xTarget.identifier }
)
MySQL.insert.await("INSERT INTO friends SET identifier = ?, friend = ?", { xPlayer.identifier, xTarget.identifier })
cb(getPlayerFriends(player), "Friend request sent")
end)
ESX.RegisterServerCallback("friendInteraction", function(player, cb, method, targetPlayer)
if not method then
return cb(false, "Unknown error!")
end
if not targetPlayer or not targetPlayer.dbID then
return cb(false, "Target player invalid!")
end
if method == "delete" then
MySQL.query.await(
"DELETE FROM friends WHERE identifier = ? AND friend = ?",
{ targetPlayer.identifier, targetPlayer.friend }
)
MySQL.query.await(
"DELETE FROM friends WHERE identifier = ? AND friend = ?",
{ targetPlayer.friend, targetPlayer.identifier }
)
MySQL.query.await("DELETE FROM friends WHERE dbID = ?", { targetPlayer.dbID })
elseif method == "accept" then
MySQL.query.await("UPDATE friends SET pending = 0 WHERE dbID = ? ", { targetPlayer.dbID })
end
local xTarget = ESX.GetPlayerFromIdentifier(targetPlayer.friend)
if xTarget then
TriggerClientEvent("updatePlayerFriends", xTarget.source, getPlayerFriends(xTarget))
end
cb(getPlayerFriends(player))
end)
ESX.RegisterServerCallback("requestPlayerInfo", function(player, cb, identifier)
if not identifier then
return cb(false, "Player not found!")
end
local result = MySQL.query.await(
"SELECT friends.*, CONCAT(firstname, ' ', lastname) AS charName, phone_number FROM friends, users WHERE friends.friend = ? AND users.identifier=friends.friend LIMIT 1",
{ identifier }
)
if #result <= 0 then
return cb(false, "Player not found!")
end
result = result[1]
local xTarget = ESX.GetPlayerFromIdentifier(identifier)
if xTarget then
result.online = true
result.discordName = getPlayerDiscordName(xTarget.source)
end
cb(result)
end)