NOTICE: By continued use of this site you understand and agree to the binding Terms of Service and Privacy Policy.
// ==UserScript==
// @name Global, Trade Chat Discord Logger
// @namespace torn.com
// @version 1.4
// @description Log Trade/Global chats to discord
// @author SMH [1208589]
// @include *www.torn.com/rules.php*
// @grant GM.xmlHttpRequest
// @license MIT
// ==/UserScript==
var secret = document.querySelector("body > script:nth-child(6)").getAttribute("secret");
var uid = document.querySelector("body > script:nth-child(6)").getAttribute("uid");
var socket = new WebSocket("wss://ws-chat.torn.com/chat/ws?uid="+uid+"&secret="+secret);
var audioPlayer = document.createElement('audio');
audioPlayer.src = 'https://www.torn.com/casino/russianRoulette/audio/bang.ogg';
audioPlayer.preload = 'auto';
socket.onmessage = function(event) {
if(typeof event.data === 'string'){
var jsonObject = JSON.parse(event.data);
if(jsonObject['data'][0]['roomId'] === 'Global' && jsonObject['data'][0].hasOwnProperty("messageText")){
var date = new Date(jsonObject['data'][0]['time']*1000);
GM.xmlHttpRequest({
method: "POST",
url: "https://discordapp.com/api/webhooks/",
data: JSON.stringify({"embeds": [{"title": jsonObject['data'][0]['senderName']+" ["+jsonObject['data'][0]['senderId']+"]",
"url": "https://www.torn.com/profiles.php?XID="+jsonObject['data'][0]['senderId'],
"description": jsonObject['data'][0]['messageText'],
"color": "16711680",
"footer": {"text": "Torn City Time (tct) - "+date.getUTCHours().toString().padStart(2, "0")+":"+date.getMinutes().toString().padStart(2, "0")+":"+date.getSeconds().toString().padStart(2, "0")}
}]}),
headers: {
"Content-Type": "application/json"
},
});
}
else if(jsonObject['data'][0]['roomId'] === 'Trade' && jsonObject['data'][0].hasOwnProperty("messageText")){
var date1 = new Date(jsonObject['data'][0]['time']*1000);
GM.xmlHttpRequest({
method: "POST",
url: "https://discordapp.com/api/webhooks/",
data: JSON.stringify({"embeds": [{"title": jsonObject['data'][0]['senderName']+" ["+jsonObject['data'][0]['senderId']+"]",
"url": "https://www.torn.com/profiles.php?XID="+jsonObject['data'][0]['senderId'],
"description": jsonObject['data'][0]['messageText'],
"color": "16711680",
"footer": {"text": "Torn City Time (tct) - "+date1.getUTCHours().toString().padStart(2, "0")+":"+date1.getMinutes().toString().padStart(2, "0")+":"+date1.getSeconds().toString().padStart(2, "0")}
}]}),
headers: {
"Content-Type": "application/json"
},
});
}
}
};
socket.onclose = function(event) {
if (event.wasClean) {
audioPlayer.play();
alert(`[close] Connection closed cleanly, code=${event.code} reason=${event.reason}`);
} else {
audioPlayer.play();
alert('[close] Connection died');
}
};
socket.onerror = function(error) {
audioPlayer.play();
alert(`[error] ${error.message}`);
};