Skip to content

Commit

Permalink
!status and module system
Browse files Browse the repository at this point in the history
  • Loading branch information
troit5ky committed Sep 5, 2022
1 parent 219db66 commit 6b97dee
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 19 deletions.
9 changes: 9 additions & 0 deletions lua/autorun/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ if SERVER then
include("relay/sv_msgSend.lua")
include("relay/sv_msgGet.lua")

-- commands
local files, _ = file.Find( 'relay/commands/' .. "*", "LUA" )

for num, fl in ipairs(files) do
include("relay/commands/" .. fl)
print('[Discord] module ' .. fl .. ' added!')
end
--

AddCSLuaFile('cl_config.lua')
AddCSLuaFile('relay/cl_msgReceive.lua')

Expand Down
39 changes: 39 additions & 0 deletions lua/relay/commands/status.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
Discord.commands['status'] = function()
local plys = player.GetCount() .. '/' .. game.MaxPlayers()
local plyList = ''
local plysTable = player.GetAll()

if #plysTable > 0 then
for num, ply in ipairs(plysTable) do
plyList = plyList .. ply:Nick() .. '\n'
end
else plyList = 'никого ¯\\_(ツ)_/¯' end

local form = {
['embeds'] = {{
['color'] = 5793266,
['title'] = GetHostName(),
['description'] = [[
**Подключиться** - steam://connect/]] .. game.GetIPAddress() .. [[
**Карта сейчас** - ]] .. game.GetMap() .. [[
**Игроков** - ]] .. plys .. [[
]],
['fields'] = {{
['name'] = 'Список игроков',
['value'] = plyList
}}
}}
}

Discord.send(form)
end

Discord.commands['ping'] = function()
local form = {
['content'] = ':ping_pong: pong'
}

Discord.send(form)
end
34 changes: 24 additions & 10 deletions lua/relay/sv_msgGet.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,27 @@ end

function socket:onMessage(txt)
local resp = util.JSONToTable(txt)
if Discord.debug then
if Discord.debug then
print("[Discord] Received: ")
PrintTable(resp)
end

if resp.op == 10 and resp.t == nil then createHeartbeat() end
if resp.op == 1 then heartbeat() end
if resp.d then
if resp.t == "MESSAGE_CREATE" && resp.d.channel_id == Discord.readChannelID && resp.d.content != '' then
if resp.t == "MESSAGE_CREATE" && resp.d.channel_id == Discord.readChannelID && resp.d.content != '' then
if resp.d.author.bot == true then return end
if string.sub(resp.d.content, 0, 1) == '!' then
command = string.sub(resp.d.content, 2)

if Discord.commands[command] then Discord.commands[command]() end

return
end
broadcastMsg({
['author'] = resp.d.author.username,
['content'] = resp.d.content
})
})
end
end
end
Expand All @@ -67,32 +74,39 @@ function socket:onConnected()
"op": 2,
"d": {
"token": "]]..Discord.botToken..[[",
"compress": true,
"intents": 512,
"status": "dnd",
"properties": {
"os": "linux",
"browser": "disco",
"device": "disco"
"browser": "gmod",
"device": "pc"
},
"presence": {
"activities": [{
"name": "Garry's Mod",
"type": 0
}]
}
}
}
]]

timer.Simple(2, function() socket:write(req) end)
heartbeat()
timer.Simple(3, function() socket:write(req) end)
end

function socket:onDisconnected()
print("[Discord] WebSocket disconnected")
timer.Remove('!!discord_hearbeat')

if Discord.isSocketReloaded != true then
if Discord.isSocketReloaded != true then
print('[Discord] WebSocket reload in 5 sec...')
timer.Simple(5, function() socket:open() end)
end
end

print('[Discord] Socket init...')
timer.Simple(3, function()
timer.Simple(3, function()
socket:open()
Discord.isSocketReloaded = false
Discord.isSocketReloaded = false
end)
18 changes: 10 additions & 8 deletions lua/relay/sv_msgSend.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,17 @@ local function playerConnect(ply)
end

local function plyFrstSpawn(ply)
local form = {
["username"] = Discord.hookname,
["embeds"] = {{
["title"] = "Игрок "..ply:GetName().." ("..ply:SteamID()..") подключился",
["color"] = 4915018,
}}
}
if IsValid(ply) then
local form = {
["username"] = Discord.hookname,
["embeds"] = {{
["title"] = "Игрок "..ply:Nick().." ("..ply:SteamID()..") подключился",
["color"] = 4915018,
}}
}

Discord.send(form)
Discord.send(form)
end
end

local function plyDisconnect(ply)
Expand Down
7 changes: 6 additions & 1 deletion lua/sv_config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ Discord = {

['botToken'] = 'token',

["botPrefix"] = "!",

-- For developers (logs)
['debug'] = false
['debug'] = false,

-- Don't touch!
['commands'] = {}
}

0 comments on commit 6b97dee

Please sign in to comment.