Greasy Fork is available in English.

DRRR.COM智能脚本 - 自动对话 - 自动点歌

2020/10/24 上午11:02:03

您查看的为 2020-10-24 11:09:53 UTC 提交的版本。查看 最新版本

// ==UserScript==
// @name       DRRR.COM智能脚本 - 自动对话 - 自动点歌
// @namespace   Violentmonkey Scripts
// @match       https://drrr.com/room/?id=*
// @grant       none
// @version     1.0
// @author      阿太网络 QQ:121610059
// @description 2020/10/24 上午11:02:03
// ==/UserScript==

// 默认不运行
// 启动命令 localStorage.setItem("running",true);
$("body").append('<script>function startBot(){localStorage.setItem("botRunning",true);console.log("自动回复已启动");$("#startBot").text("关闭自动回复");$("#startBot").attr("onclick","stopBot()");}function stopBot(){localStorage.setItem("botRunning",false);console.log("自动回复已关闭");$("#startBot").text("启动自动回复");$("#startBot").attr("onclick","startBot()");}function startMusic(){localStorage.setItem("musicRunning",true);console.log("点歌功能已启动");$("#startMusic").text("关闭点歌功能");$("#startMusic").attr("onclick","stopMusic()");}function stopMusic(){localStorage.setItem("musicRunning",false);console.log("点歌功能已关闭");$("#startMusic").text("启动点歌功能");$("#startMusic").attr("onclick","startMusic()");}</script>');

swal("提示信息", "欢迎使用 DRRR BOT 智能脚本\n请先点击房间名称右边的启动\n目前脚本有如下功能:\n1.智能Ai聊天(格式:@机器人名称聊天内容)\n2.点歌功能(格式:点歌歌曲名称)");

$(".room-input-wrap").append('<div id="startBot" onclick="startBot()" style="text-align: center;background-color: #009688;color: #ffffff;width: 200px;border-radius: 10px;font-size: 12px;line-height: 1;padding-top: 5px; padding-bottom: 5px;font-weight: 400;margin: 5px auto 0;">启动自动回复</div>');
$(".room-input-wrap").append('<div id="startMusic" onclick="startMusic()" style="text-align: center;background-color: #4CAF50;color: #ffffff;width: 200px;border-radius: 10px;font-size: 12px;line-height: 1;padding-top: 5px; padding-bottom: 5px;font-weight: 400;margin: 5px auto 0;">启动点歌功能</div>');

localStorage.setItem("botRunning",false);
localStorage.setItem("musicRunning",false);

var newMsg;
var receive = false;
var talks = document.querySelector(".talks");
var timestamp = new Date().getTime();
var ws = new WebSocket("wss://bot.dulele.me/wss");

var botRunning = localStorage.getItem("botRunning");
var musicRunning = localStorage.getItem("musicRunning");
var botName = localStorage.getItem("username");
//console.log(botName);

// websocket连接成功
ws.onopen = function(evt) { 
  console.log("websocket连接成功!"); 
  console.log('请启动Bot 启动命令 localStorage.setItem("running",true)'); 
};

ws.close = function(evt) { 
  console.log("websocket连接关闭!"); 
  console.log('重新建立websocket'); 
  ws = new WebSocket("wss://bot.dulele.me/wss");
};

// websocket监听消息
ws.onmessage = function(evt) {
	var res = JSON.parse(evt.data);
	console.log('收到' + res.msgType + '类型消息');
	if(res.msgType == "text"){
		sendMsg(res.msg);
	}
	if(res.msgType == "music"){
		sendMusic(res.musicName,res.musicUrl);
	}
};


// 监听最近消息
talks.addEventListener('DOMNodeInserted', function () {
    botRunning = localStorage.getItem("botRunning");
    musicRunning = localStorage.getItem("musicRunning");
    var num = $("#talks dl").length;
    var roomId = getQueryVariable("id");
	var name = $(".talks").children().eq(0).children("dt").children(".name").children("span").text();
    var msg = $(".talks").children().eq(0).children("dd").children(".bubble").children("p").text();
    if(botRunning == "true" || musicRunning == "true"){
      //Receive(JSON.stringify(data));
      Receive(roomId,name,msg);
    }
}, false);

// 接收获取到的最新消息
function Receive(roomId,name,msg){
  if(!receive){
    receive = true;
  }else{
    receive = false
	var data = {};	
	data["roomId"] = roomId;
	data["name"] = name;
	var songName = msg.match(/点歌(.*)/);
	console.log('收到消息:' + msg);
	if(songName && musicRunning == "true" && botName != name){
		data['songName'] = songName[1];
	}
	if(!songName && botRunning == "true" && botName != name){
		data['msg'] = msg;
	}
    ws.send(JSON.stringify(data));
  }
}

// 发送文本消息
function sendMsg(msg,method = 1,url = "",to = ""){
	if(method == 0){
		$.ajax({
			type: "POST",
			url:"https://drrr.com/room/?ajax=1",
			data:{message:msg,url:url,to:url},
			success:function(result){
		        console.log("发送消息成功");
			},
		});
	}
	if(method == 1){
		$(".form-control").val(msg).click();
	}
}

// 发送音乐
function sendMusic(name,url){
	$.ajax({
		type: "POST",
		url:"https://drrr.com/room/?ajax=1",
		data:{music:"music",url:url,name:name},
		success:function(result){
	        console.log("发送音乐成功");
		},
	});
}

// 获取GET参数
function getQueryVariable(variable){
       var query = window.location.search.substring(1);
       var vars = query.split("&");
       for (var i=0;i<vars.length;i++) {
               var pair = vars[i].split("=");
               if(pair[0] == variable){return pair[1];}
       }
       return(false);
}

// 获取机器人名称
function getBotname(){
	$.ajax({
		type: "GET",
		url:"https://drrr.com/lounge?api=json",
		success:function(result){
	        //console.log("Bot 名称:" + result.profile.name);
			return	JSON.stringify(result);
		},
	});
}