Skip to content

Instantly share code, notes, and snippets.

@fucksophie
Created August 21, 2023 17:37
Show Gist options
  • Save fucksophie/a27b6ac88932db05bfa0e7a586a4d11d to your computer and use it in GitHub Desktop.
Save fucksophie/a27b6ac88932db05bfa0e7a586a4d11d to your computer and use it in GitHub Desktop.
switchcraft and matrix bridge :)
import { Client } from "switchchat";
import {Level} from "level"
import {
MatrixClient,
SimpleFsStorageProvider,
} from "matrix-bot-sdk";
const storage = new SimpleFsStorageProvider("server.json");
const client = new MatrixClient("https://matrix.yourfriend.lol", "", storage);
const db = new Level('./db', { valueEncoding: 'json' })
async function isSet(key) {
try {
await db.get(key)
return true;
} catch {
return false
}
}
const sc = new Client("");
const senderClients = [sc, new Client("")];
sc.defaultName = "matrix bridge";
sc.defaultFormattingMode = "markdown";
sc.on("death", async (event) => {
if(await isSet("optedIn_"+event.user.uuid)) {
let text = event.rawText;
let gg = text.split(" ")
let emoji = gg.shift();
gg.unshift("["+emoji+"]")
await client.sendText("!xWojehQjMiyDCAdwqU:bark.lgbt", gg.join(" "))
}
})
sc.on("join", async (event) => {
if(await isSet("optedIn_"+event.user.uuid)) {
await client.sendMessage("!xWojehQjMiyDCAdwqU:bark.lgbt", {
formatted_body: `<font color="#32cd32">+</font> <font color="#006400"><strong>${event.user.name}</strong></font> <font color="#ffff00">joined the game</font>`,
format: 'org.matrix.custom.html',
msgtype: "m.text"
});
}
})
sc.on("leave", async (event) => {
if(await isSet("optedIn_"+event.user.uuid)) {
await client.sendMessage("!xWojehQjMiyDCAdwqU:bark.lgbt", {
formatted_body: `<font color="#ff0000">-</font> <font color="#006400"><strong>${event.user.name}</strong></font> <font color="#ffff00">left the game</font>`,
format: 'org.matrix.custom.html',
msgtype: "m.text"
});
}
})
sc.on("chat_discord", async (event) => {
let username = (event.discordUser).linkedUser.name;
let uuid = (event.discordUser).linkedUser.uuid;
let message = event.rawText;
if(await isSet("optedIn_"+uuid)) {
await client.sendMessage("!xWojehQjMiyDCAdwqU:bark.lgbt", {
formatted_body: `<strong>&lt;${username}&gt;</strong>: ${message}`,
format: 'org.matrix.custom.html',
msgtype: "m.text"
});
}
})
sc.on("chat_ingame", async (event) => {
let username = event.user.name;
let uuid = event.user.uuid;
let message = event.rawText;
if(await isSet("optedIn_"+uuid)) {
await client.sendMessage("!xWojehQjMiyDCAdwqU:bark.lgbt", {
formatted_body: `<strong>&lt;${username}&gt;</strong>: ${message}`,
format: 'org.matrix.custom.html',
msgtype: "m.text"
});
}
})
sc.on("command", async (cmd) => {
if (cmd.command === "matrix") {
if(cmd.args[0] == "optin") {
if(await isSet("optedIn_"+cmd.user.uuid)) {
await sc.tell(cmd.user.name,"You are already opted in!")
return;
}
await sc.tell(cmd.user.name, "Opted into bridging with Matrix! Join at #switchcraft:bark.lgbt!")
await db.put("optedIn_"+cmd.user.uuid, "true")
} else if(cmd.args[0] == "optout") {
if(!await isSet("optedIn_"+cmd.user.uuid)) {
await sc.tell(cmd.user.name,"You are already opted out!")
return;
}
await sc.tell(cmd.user.name, "Opted out :( We hope to see you soon!")
await db.del("optedIn_"+cmd.user.uuid)
} else if(cmd.args[0] == "status") {
if(await isSet("optedIn_"+cmd.user.uuid)) {
await sc.tell(cmd.user.name,"You are currently opted into bridging with matrix!")
} else {
await sc.tell(cmd.user.name,"You are currently opted out of bridging with matrix!")
}
} else {
await sc.tell(cmd.user.name, "Use /matrix optin, /matrix optout, /matrix status")
}
}
});
sc.on("ready", () => {
console.log("Connected!");
});
client.on("room.message", async (roomId, event) => {
if (event['sender'] === await client.getUserId()) return;
if(roomId != "!xWojehQjMiyDCAdwqU:bark.lgbt") return;
sc.players.forEach(async z => {
if(await isSet("optedIn_"+z.uuid)) {
let body = event.content.body
if(event.content.msgtype == "m.image") {
body = `[${event.content.body} | Image](${client.mxcToHttp(event.content.url)})`
}
if(event.content.msgtype == "m.video") {
body = `[${event.content.body} | Video](${client.mxcToHttp(event.content.url)})`
}
if(event.content.msgtype == "m.audio") {
body = `[${event.content.body} | Audio](${client.mxcToHttp(event.content.url)})`
}
if(event.content.msgtype == "m.notice") {
body = `*${event.content.body}*`
}
if(event.content.msgtype == "m.file") {
body = `[${event.content.body} | File](${client.mxcToHttp(event.content.url)})`
}
let cclnt = senderClients[Math.floor(Math.random() * senderClients.length)];
cclnt.tell(z.name, body, event.sender)
}
})
})
senderClients.forEach(async z => {
z.connect();
})
client.start().then(() => {
console.log("matrix running!")
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment