活動台指令 : !今

原先活動台的 !今,都是按照每天更換或是呼叫pastebin,

用Pastebin 比較麻煩的地方是每年都要去改,而且有人中途更改表單沒講就會出包,

直接更改成用GAS輸出表單的內容給Nightbot,讓奶爸當控制器去轉送就好了。

以FGC(Fun Game Carnival) 為例子:

Nightbot指令:

$(urlfetch https://script.google.com/macros/s/AKfycby-a4ciisCyOJ65-Yr_tmJtIH3mPRxHavEjLfRHgY-1ikFuBTfuZSbxnclx5Cbm5-jRPQ/exec?action=today)

GAS (Google App Script )

const SpreadSheetID = "你的SheetID";
const SpreadSheet = SpreadsheetApp.openById(SpreadSheetID);

function getSheet(sheetName) {
  return SpreadSheet.getSheetByName(sheetName);
}

function FGC_Today(action){

  var time = new Date();
  var now = Utilities.formatDate(time,"GMT+8", "yyyy-MM-dd");

  //ParseInt 轉成數字型態 , +3 是前三排忽略
  var MM = parseInt(Utilities.formatDate(time,"GMT+8", "MM"));
  var Day = parseInt(Utilities.formatDate(time,"GMT+8", "dd"))+3;
  const Sheet = getSheet('工作表1');

  // action = 天數增減
  Day = Day + action;


  //先把日期和時間斷開
  var datetime = Sheet.getSheetValues(Day, 1, 1 ,1).toLocaleString().split(/ |,/);
  //在抓年月日, 很詭異的是Nightbot 回傳是 d[2]=年,d[0]=月,d[1]日, 但網頁回傳的卻是 d[0]=年,d[1]=月,d[2]=日, 所以測試必須注意日期格式
  var d = datetime[0].split("/");

  var showtime = Sheet.getSheetValues(Day, 4, 1 ,1);
  var twitchID = Sheet.getSheetValues(Day, 2, 1 ,1);
  var game = Sheet.getSheetValues(Day, 3, 1 ,1);
  var note = Sheet.getSheetValues(Day, 5, 1 ,1);

  
  // 15+3 < 19 , 範圍抓1月 小於16號期間
  if(MM==1 && Day < 19 ){
    result = ContentService.createTextOutput("[FGC]"+d[2]+"/"+d[0]+"/"+d[1]+" 晚間"+showtime+", 由"+twitchID+"為您帶來的 "+game+", 據說是"+note+ ", 敬請期待!");
  }
  else{
    result = ContentService.createTextOutput("今年的活動已經結束囉!!");
  }

}

function FGC_title(){

  var time = new Date();
  var now = Utilities.formatDate(time,"GMT+8", "yyyy-MM-dd");

  //ParseInt 轉成數字型態 , +3 是前三排忽略
  var MM = parseInt(Utilities.formatDate(time,"GMT+8", "MM"));
  var Day = parseInt(Utilities.formatDate(time,"GMT+8", "dd"))+3;
  const Sheet = getSheet('工作表1');


  //先把日期和時間斷開
  var datetime = Sheet.getSheetValues(Day, 1, 1 ,1).toLocaleString().split(/ |,/);
  //在抓年月日
  var d = datetime[0].split("/");

  var showtime = Sheet.getSheetValues(Day, 4, 1 ,1);
  var twitchID = Sheet.getSheetValues(Day, 2, 1 ,1);
  var game = Sheet.getSheetValues(Day, 3, 1 ,1);
  var note = Sheet.getSheetValues(Day, 5, 1 ,1);

  
  // 15+3 < 19 , 範圍抓1月 小於16號期間
  if(MM==1 && Day < 19 ){
    result = ContentService.createTextOutput("【FGC"+d[2]+"】"+" Day "+d[1]+" - "+game+" by "+twitchID+" - "+d[0]+"/"+d[1]);
  }
  else{
    result = ContentService.createTextOutput("今年的活動已經結束囉!!");
  }

}


function doGet(e){

  const params = e?.parameter;
  //沒有東西回報nodata
  if(!params){
    return ContentService.createTextOutput("no data");
  }
  
  if(params.msg==""){
    return ContentService.createTextOutput("no data");
  }

  //網址後面的?action=參數,判斷要執行哪個函數。
  switch(params.action){
    case 'today':
    FGC_Today(0);
    return result;
    //return ContentService.createTextOutput("test");
    break;

    case 'tomorrow':
    FGC_Today(1);
    return result;
    break;    

    case 'title':
    FGC_title();
    return result;
    break;

    default:
    return ContentService.createTextOutput("Error: action not find. ");
    break;
  }

}

Last updated