NOTICE: By continued use of this site you understand and agree to the binding Terms of Service and Privacy Policy.
// ==UserScript==
// @name Faction chat discord logger
// @namespace torn.com
// @version 1.1
// @description Log Trade/Global chats to discord
// @author Ahab [1735214]
// @include *torn.com*
// @updateURL https://openuserjs.org/meta/Ahab/Faction_chat_discord_logger.meta.js
// @downloadURL https://openuserjs.org/install/Ahab/Faction_chat_discord_logger.user.js
// @grant GM.xmlHttpRequest
// @license MIT
// ==/UserScript==
var act = 0
document.addEventListener('visibilitychange', function (event) {
if (document.hidden) {
act = 1
} else {
act = 0
}
});
function listen(fn){
fn = fn || console.log;
let property = Object.getOwnPropertyDescriptor(MessageEvent.prototype, "data");
const data = property.get;
function lookAtMessage() {
let socket = this.currentTarget instanceof WebSocket;
if (!socket) {
return data.call(this);
}
let msg = data.call(this);
Object.defineProperty(this, "data", { value: msg } ); //anti-loop
fn({ data: msg, socket:this.currentTarget, event:this });
return msg;
}
property.get = lookAtMessage;
Object.defineProperty(MessageEvent.prototype, "data", property);
}
listen( ({data}) => send(data))
function send(data){
if(data.substring(0, 4) == "MESG" && act == 0){
var jsonObject = JSON.parse(data.slice(4))
if(jsonObject.channel_url.substring(0, 7) === "faction"){
GM.xmlHttpRequest({
method: "POST",
url: "DISCORD WEBHOOK URL HERE",
data: JSON.stringify({"embeds": [{"title": jsonObject.user.name+" ["+jsonObject.user.guest_id+"]",
"url": "https://www.torn.com/profiles.php?XID="+jsonObject.user.guest_id,
"description": jsonObject.message,
"color": "16711680",
"footer": {"text": "Torn City Time (tct) - "+new Date(jsonObject.last_updated_at).toJSON().replace('T', ' ').slice(0, -5)}
}]}),
headers: {
"Content-Type": "application/json"
},
});
}
}
};