Skip to content

Instantly share code, notes, and snippets.

@Shigbeard
Last active May 18, 2023 06:47
Show Gist options
  • Save Shigbeard/6de6f435e4226a312e8c92ffc9e21180 to your computer and use it in GitHub Desktop.
Save Shigbeard/6de6f435e4226a312e8c92ffc9e21180 to your computer and use it in GitHub Desktop.
Relay server chat to discord.
--[[
Gmod to Discord Chat Relay:
Have your chat display in discord!
You need a Webhook URL to make this work.
Step 1: Go into discord, and hover your mouse over the desired channel you'd like messages to be sent to.
Step 2: Click on Edit Channel, and click on Webhooks.
Step 3: Click on "Create Webohook"
Step 4: Fill in a default username, and give it an avatar of your choosing. Then click save. Edit it again before going onto step 5
Step 5: Copy and paste the webhook URL down below.
You need a SteamWebAPIKey for avatars to be fetched.
Step 1: Goto http://steamcommunity.com/dev/apikey
Step 2: Fill in a domain name, I recommend the IP address of your server.
Step 3: Copy and paste the key down below.
HOW TO INSTALL
put it in garrysmod/lua/autorun/server
]]
local WebhookURL = "Change Me"
local SteamWebAPIKey = "Change Me"
function getAvatarFromJson( j_response ) -- Thanks https://facepunch.com/showthread.php?t=1484549&p=48631437&viewfull=1#post48631437
local t_response = util.JSONToTable( j_response )
if ( !istable( t_response ) or !t_response.response ) then return false end
if ( !t_response.response.players or !t_response.response.players[1] ) then return false end
return t_response.response.players[1].avatarfull
end
function getAvatarURL( code, body, headers )
if !body then
local t_struct = {
failed = function( err ) MsgC( Color(255,0,0), "HTTP error: " .. err ) end,
method = "get",
url = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/",
parameters = { key = SteamWebAPIKey, steamid = code },
success = getAvatarURL
}
HTTP( t_struct )
else
return( getAvatarFromJson( body ) )
end
end
function sendChat(p_sender, s_text, b_teamChat)
if !p_sender then return end
local t_post = {
content = s_text,
username = "(Gmod) " .. (p_sender:Nick() or "Unknown"),
avatar_url = getAvatarURL( p_sender:SteamID64() )
}
local t_struct = {
failed = function( err ) MsgC( Color(255,0,0), "HTTP error: " .. err ) end,
method = "post",
url = WebhookURL,
parameters = t_post,
type = "application/json; charset=utf-8" --JSON Request type, because I'm a good boy.
}
HTTP( t_struct )
end
hook.Add("PlayerSay","Discord_Webhook_Chat", sendChat)
@Shigbeard
Copy link
Author

I updated the gist to show where it goes, though you should probably use a better script with two way relay, this only sends to discord, not the other way around.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment