2024-08-22 17:34:03 +02:00
import apfetch from "./auth-fetch.js" ;
2024-08-21 17:00:40 +02:00
import { encode } from "html-entities" ;
import * as sdk from "matrix-js-sdk" ;
import sdkExt from "./lib/ext.js" ;
import { resolve } from "node:path" ;
import fs from "node:fs" ;
import util from "util" ;
2024-08-25 20:37:24 +02:00
import { LocalStorage } from 'node-localstorage' ;
2024-08-21 17:00:40 +02:00
import fetch from "node-fetch" ;
global . fetch = fetch ;
import Olm from "@matrix-org/olm" ;
global . Olm = Olm ;
import env from "dotenv" ;
env . config ( ) ;
2024-08-25 20:37:24 +02:00
const localStorage = new LocalStorage ( "./data/localstorage" ) ;
const cryptoStore = new sdk . LocalStorageCryptoStore ( localStorage ) ;
const store = new sdk . MemoryStore ( { localStorage } ) ;
2024-08-21 17:00:40 +02:00
const client = sdk . createClient ( {
baseUrl : process . env . BASE _URL ,
accessToken : process . env . ACCESS _TOKEN ,
deviceId : process . env . DEVICE _ID ,
2024-08-25 20:37:24 +02:00
userId : process . env . USER _ID ,
cryptoStore ,
store
2024-08-21 17:00:40 +02:00
} ) ;
sdkExt ( client ) ;
var prefixes = [ process . env . PREFIX ] ;
client . initialized = false ;
client . modules = [ ] ;
client . commands = [ ] ;
for ( const file of fs . readdirSync ( resolve ( "modules" ) ) ) {
try {
if ( file . startsWith ( "." ) ) continue ;
var module = ( await import ( resolve ( "modules" , file ) ) ) . default ;
client . modules . push ( module ) ;
client . log ( "[load:modules]" , ` loaded ${ module . name } ` ) ;
} catch ( err ) {
client . error ( "[load:modules]" , ` failed to load " ${ file } ": \n ` , err ) ;
}
}
for ( const file of fs . readdirSync ( resolve ( "commands" ) ) ) {
try {
if ( file . startsWith ( "." ) ) continue ;
var command = ( await import ( resolve ( "commands" , file ) ) ) . default ;
command . usage = process . env . PREFIX + command . command + ( command . usage ? " " + command . usage : '' ) ;
client . commands . push ( command ) ;
client . log ( "[load:commands]" , ` loaded ${ command . name } ` ) ;
} catch ( err ) {
client . error ( "[load:commands]" , ` failed to load " ${ file } ": ` , err ) ;
}
}
function doModule ( client , event ) {
client . modules . forEach ( m => {
if ( ! m ) return ;
2024-08-22 18:22:52 +02:00
var config = client . cache . get ( event . sender . roomId ) ? ? { } ;
if ( config [ m . name ] === false ) return ;
2024-08-21 17:00:40 +02:00
m . hooks ? . message ( client , event ) ;
} ) ;
}
function doCommand ( client , event , cmd , args ) {
var command ;
command = client . commands . filter ( c => c . command == cmd ) [ 0 ] ;
if ( ! command )
return false ;
2024-08-25 15:15:45 +02:00
if ( ( command . owner && event . sender . userId != process . env . OWNER _ID ) || ( command . minPwr && event . sender . powerLevel < command . minPwr && event . sender . userId != process . env . OWNER _ID ) ) {
var addl = command . minPwr ? ` this command requires a power level of ${ command . minPwr } \n your power level is ${ event . sender . powerLevel } ` : "this command is owner-only." ;
2024-08-25 20:37:24 +02:00
client . reply ( event , addl , '<img src="mxc://possum.city/b4Vo1BTcq49B7TbFWCqq76HQWQEdNIqq" alt="nuh uh" /><br>' + addl . replaceAll ( "\n" , "<br>" ) ) ;
2024-08-21 17:00:40 +02:00
return true ;
}
command . execute ( client , event , args ) ;
return true ;
}
client . once ( "sync" , async function ( state , prevState , data ) {
2024-08-25 20:37:24 +02:00
if ( state != "PREPARED" ) return ;
client . setGlobalErrorOnUnknownDevices ( false ) ;
client . name = client . _store . users [ client . credentials . userId ] . displayName ;
client . log ( "[sdk:sync]" , "connected:" , client . name ) ;
prefixes . push ( client . name + ": " ) ;
prefixes . push ( client . name + " " ) ;
client . initialized = true ;
2024-08-21 17:00:40 +02:00
} ) ;
client . on ( sdk . RoomEvent . Timeline , async function ( event , room , toStartOfTimeline ) {
2024-08-25 20:37:24 +02:00
await client . decryptEventIfNeeded ( event ) ;
2024-08-21 17:00:40 +02:00
if ( ! client . initialized || toStartOfTimeline || event . getType ( ) !== "m.room.message" || event . sender . userId == client . credentials . userId ) {
return ;
}
2024-08-25 20:37:24 +02:00
var content = event . getContent ( ) ;
2024-08-27 17:18:30 +02:00
if ( content [ "m.relates_to" ] !== undefined )
2024-08-25 20:37:24 +02:00
content . body = content . body . substring ( content . body . indexOf ( "\n\n" ) + 2 ) ;
2024-08-21 17:00:40 +02:00
2024-08-22 18:22:52 +02:00
event . sender = client . getRoom ( event . sender . roomId ) . getMember ( event . getSender ( ) ) ;
2024-08-25 20:37:24 +02:00
var content = content . body ;
2024-08-21 17:00:40 +02:00
var isCommand = false ;
2024-08-25 15:08:10 +02:00
2024-08-21 17:00:40 +02:00
for ( const prefix of prefixes ) {
if ( content . startsWith ( prefix ) ) {
isCommand = true ;
content = content . substring ( prefix . length ) ;
break ;
}
}
2024-08-25 15:08:10 +02:00
2024-08-21 17:00:40 +02:00
if ( ! isCommand ) {
doModule ( client , event ) ;
} else {
var args = content . split ( /\s/g ) ;
var cmd = args . shift ( ) ;
args = content . substring ( cmd . length + 1 ) ;
if ( ! doCommand ( client , event , cmd , args ) ) {
client . reply ( event , ` Command " ${ cmd } " not found. \n Run " ${ process . env . PREFIX } help" for a list of commands. ` ) ;
}
}
} ) ;
client . on ( "Room.myMembership" , function ( room , membership , prevMembership ) {
if ( membership === "invite" ) {
client . joinRoom ( room . roomId ) . then ( function ( ) {
client . log ( "[client:autojoin] joined %s" , room . roomId ) ;
} ) ;
}
} ) ;
2024-08-25 20:37:24 +02:00
await client . initCrypto ( ) ;
2024-08-21 17:00:40 +02:00
await client . startClient ( ) ;