Advertisement
Mayumi_H

Local chat to discord relay.lsl

Jul 31st, 2020 (edited)
1,350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //MH+ presents, super simple Local chat to discord relay
  2. //join us! => https://discord.me/SLguru/
  3. //my blog  => https://sl-memo.blogspot.com/
  4. //reference https://discord.com/developers/docs/resources/webhook
  5.  
  6. // (modify required) ///////////////////////////////////////////////////
  7. string WEBHOOK_URL = "";
  8.  
  9.  
  10. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  11. // WebHook format
  12. // WEBHOOK_URL =
  13. // |----------------------------------------------------------------------------------------------------------------------|
  14. // https://discord.com/api/webhooks/123456789012345678/abcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcd
  15. //                                 |~~~~~~~~~~~~~~~~~~|
  16. //                                 |WEBHOOK_CHANNEL = |
  17. //                                                    |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  18. //                                                    |WEBHOOK_TOKEN =
  19. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  20. /*
  21. sample:
  22.  
  23. for single-channel
  24.  
  25. string WEBHOOK_URL = "https://discord.com/api/webhooks/123456789012345678/abcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcd";
  26.  
  27.  
  28. for forum-channel(with thread-id)
  29.  
  30. string WEBHOOK_URL = "https://discord.com/api/webhooks/123456789012345678/abcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcd?thread_id=1234567890123456789";
  31.  
  32. */
  33. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  34. list    REQUEST_List;
  35.  
  36. integer G_Handle = 0;
  37. integer SW = FALSE;
  38.  
  39. default{
  40.     on_rez(integer i){
  41.         llResetScript();
  42.     }
  43.  
  44.     state_entry(){
  45.         if(WEBHOOK_URL != ""){
  46.             llOwnerSay("Touch to start the relay");
  47.         }else{
  48.             llOwnerSay("Edit the script and set the Discord Hook URL (WEBHOOK_URL).");
  49.         }
  50.     }
  51.  
  52.     touch_start(integer total_number){
  53.         if(SW = !SW){
  54.             //start
  55.             llOwnerSay("relay start.");
  56.             G_Handle = llListen(0, "", NULL_KEY, "");
  57.         }else{
  58.             //stop
  59.             llOwnerSay("relay stop.");
  60.             llListenRemove(G_Handle);
  61.             REQUEST_List =[];
  62.         }
  63.     }
  64.  
  65.     listen(integer channel, string name, key id, string message){
  66.         //
  67.         string User_NAME = llGetUsername(id);
  68.         string User_DISP = llGetDisplayName(id);
  69.         list json;
  70.         if(User_NAME == "" && User_DISP == ""){// is object
  71.              json = [
  72.             "username", name,
  73.             "content", message,
  74.             "avatar_url", "https://my-secondlife-agni.akamaized.net/assets/default/avatar_thumb.png"
  75.                     ];
  76.         }else{
  77.              json = [
  78.             "username", User_DISP + " (" + User_NAME + ")",
  79.             "content", message,
  80.             "avatar_url", "https://my-secondlife-agni.akamaized.net/users/" + User_NAME + "/thumb_sl_image.png"
  81.                     ];
  82.         }
  83.         //if the image is not registered, a display error will occur, but it will be the icon defined by the webhook.
  84.         key REQUEST_KEY = llHTTPRequest(WEBHOOK_URL,
  85.                     [
  86.             HTTP_METHOD, "POST",
  87.             HTTP_MIMETYPE, "application/json",
  88.             HTTP_VERIFY_CERT,TRUE,
  89.             HTTP_VERBOSE_THROTTLE, TRUE,
  90.             HTTP_PRAGMA_NO_CACHE, TRUE
  91.                     ], llList2Json(JSON_OBJECT, json));
  92.         REQUEST_List += [REQUEST_KEY];
  93.     }
  94.  
  95.     http_response(key request_id, integer status, list metadata, string body){
  96.         integer p = llListFindList(REQUEST_List,[request_id]);
  97.         if(~p){
  98.             if(status < 200 || status > 206) llOwnerSay("status = " + (string)status + " ;body = " + body);
  99.             REQUEST_List = llDeleteSubList(REQUEST_List,p,p);
  100.         }
  101.     }
  102. }
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement