Mayumi_H

Local chat to discord relay.lsl

Jul 31st, 2020
74
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. //
  3. // 参考:https://discord.com/developers/docs/resources/webhook
  4.  
  5. //設定必須 (modify required) ///////////////////////////////////////////////////
  6. string WEBHOOK_URL = "";
  7.  
  8. ////////////////////////////////////////////////////////////////////////////////
  9. // こんな感じでURLが払い出されます
  10. // WEBHOOK_URL =
  11. // |----------------------------------------------------------------------------------------------------------------------|
  12. // https://discord.com/api/webhooks/123456789012345678/abcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcd
  13. //                                 |~~~~~~~~~~~~~~~~~~|
  14. //                                 |WEBHOOK_CHANNEL = |
  15. //                                                    |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  16. //                                                    |WEBHOOK_TOKEN =
  17. ////////////////////////////////////////////////////////////////////////////////
  18. list    REQUEST_List;
  19.  
  20. integer G_Handle = 0;
  21. integer SW = FALSE;
  22.  
  23. default{//ユーザー名から正規のNAMEとUUIDと表示名を取得
  24.     on_rez(integer i){
  25.         llOwnerSay(llList2String(["discord relay OFF","discord relay ON"],SW));
  26.     }
  27.  
  28.     state_entry(){
  29.         //URLがあれば
  30.         if(WEBHOOK_URL != ""){
  31.             llOwnerSay("タッチで処理を開始します。");
  32.         }else{
  33.             llOwnerSay("スクリプトを編集し、DiscordのHook URL(WEBHOOK_URL)を設定してください。");
  34.         }
  35.     }
  36.  
  37.     touch_start(integer total_number){
  38.         if(SW = !SW){
  39.             //start
  40.             llOwnerSay("relay start.");
  41.             G_Handle = llListen(0, "", NULL_KEY, "");
  42.         }else{
  43.             //stop
  44.             llOwnerSay("relay stop.");
  45.             llListenRemove(G_Handle);
  46.             REQUEST_List =[];
  47.         }
  48.     }
  49.  
  50.     listen(integer channel, string name, key id, string message){
  51.         //アイコンとユーザー名を上書きする
  52.         string User_NAME = llGetUsername(id);
  53.         string User_DISP = llGetDisplayName(id);
  54.         list json;
  55.         if(User_NAME == "" && User_DISP == ""){// is object
  56.              json = [
  57.             "username", name,
  58.             "content", message,
  59.             "avatar_url", "https://my-secondlife-agni.akamaized.net/assets/default/avatar_thumb.png"
  60.                     ];
  61.         }else{
  62.              json = [
  63.             "username", User_DISP + " (" + User_NAME + ")",
  64.             "content", message,
  65.             "avatar_url", "https://my-secondlife-agni.akamaized.net/users/" + User_NAME + "/thumb_sl_image.png"
  66.                     ];
  67.         }
  68.         //画像が未登録の場合表示エラーになるけれどWEBHOOKで定義したアイコンになる
  69.         key REQUEST_KEY = llHTTPRequest(WEBHOOK_URL,
  70.                     [
  71.             HTTP_METHOD, "POST",
  72.             HTTP_MIMETYPE, "application/json",
  73.             HTTP_VERIFY_CERT,TRUE,
  74.             HTTP_VERBOSE_THROTTLE, TRUE,
  75.             HTTP_PRAGMA_NO_CACHE, TRUE
  76.                     ], llList2Json(JSON_OBJECT, json));
  77.         REQUEST_List += [REQUEST_KEY];
  78.     }
  79.  
  80.     http_response(key request_id, integer status, list metadata, string body){
  81.         integer p = llListFindList(REQUEST_List,[request_id]);
  82.         if(~p){
  83.             if(status < 200 || status > 206) llOwnerSay("status = " + (string)status + " ;body = " + body);
  84.             REQUEST_List = llDeleteSubList(REQUEST_List,p,p);
  85.         }
  86.     }
  87. }
  88.  
RAW Paste Data