2022-11-16 13:53:42 -05:00
import Discord from 'discord.js' ;
2022-11-13 19:18:15 -05:00
import { TClient } from './client' ;
2022-11-11 19:58:11 -05:00
const client = new TClient ;
client . init ( ) ;
2022-11-16 13:53:42 -05:00
import fs from 'node:fs' ;
import MPDB from './models/MPServer' ;
2022-11-18 06:29:03 -05:00
import { Punishment , punOpt } from './typings/interfaces' ;
2022-11-11 19:58:11 -05:00
client . on ( 'ready' , async ( ) = > {
client . guilds . cache . forEach ( async ( e : { members : { fetch : ( ) = > any ; } ; } ) = > { await e . members . fetch ( ) } ) ;
setInterval ( async ( ) = > {
2022-11-17 12:58:19 -05:00
client . user . setPresence ( { activities : [ { name : 'Slash commands!' , type : 0 } ] , status : 'online' } ) ;
2022-11-11 19:58:11 -05:00
// Playing: 0, Streaming (Requires YT/Twitch URL to work): 1, Listening to: 2, Watching: 3, Competing in: 5
} , 60000 ) ;
2022-11-14 03:45:40 -05:00
if ( client . config . botSwitches . registerCommands ) ( client . guilds . cache . get ( client . config . mainServer . id ) as Discord . Guild ) . commands . set ( client . registry ) . catch ( ( e ) = > { console . log ( ` Couldn't register slash commands: ${ e } ` ) } )
2022-11-11 19:58:11 -05:00
setInterval ( ( ) = > {
2022-11-13 19:18:15 -05:00
const guild = client . guilds . cache . get ( client . config . mainServer . id ) as Discord . Guild ;
guild . invites . fetch ( ) . then ( ( invs ) = > {
invs . forEach ( async ( inv ) = > {
client . invites . set ( inv . code , { uses : inv.uses , creator : inv.inviterId } )
2022-11-11 19:58:11 -05:00
} )
} )
} , 500000 ) ;
2022-11-13 19:18:15 -05:00
console . log ( ` ${ client . user . tag } has logged into Discord API and now ready for operation ` ) ;
console . log ( client . config . botSwitches ) ;
2022-11-17 12:58:19 -05:00
//(client.channels.resolve(client.config.mainServer.channels.bot_status) as Discord.TextChannel).send(`${client.user.username} is active\n\`\`\`json\n${Object.entries(client.config.botSwitches).map((hi)=>`${hi[0]}: ${hi[1]}`).join('\n')}\`\`\``);
2022-11-11 19:58:11 -05:00
// Event handler
2022-11-16 13:53:42 -05:00
const eventFiles = fs . readdirSync ( 'src/events' ) . filter ( file = > file . endsWith ( '.ts' ) ) ;
2022-11-11 19:58:11 -05:00
eventFiles . forEach ( ( file ) = > {
const event = require ( ` ./events/ ${ file } ` ) ;
2022-11-16 13:53:42 -05:00
client . on ( event . default . name , async ( . . . args ) = > event . default . execute ( client , . . . args ) ) ;
2022-11-11 19:58:11 -05:00
} ) ;
} )
// Handle errors
2022-11-13 08:46:50 -05:00
process . on ( 'unhandledRejection' , async ( error : Error ) = > {
2022-11-13 19:18:15 -05:00
console . log ( error ) ;
2022-11-19 05:17:04 -05:00
//(client.channels.resolve(client.config.mainServer.channels.errors) as Discord.TextChannel).send({embeds: [new client.embed().setColor('#420420').setTitle('Error caught!').setDescription(`**Error:** \`${error.message}\`\n\n**Stack:** \`${`${error.stack}`.slice(0, 2500)}\``)]})
2022-11-11 19:58:11 -05:00
} ) ;
2022-11-13 08:46:50 -05:00
process . on ( 'uncaughtException' , async ( error : Error ) = > {
2022-11-13 19:18:15 -05:00
console . log ( error ) ;
2022-11-18 22:23:33 -05:00
( client . channels . resolve ( client . config . mainServer . channels . errors ) as Discord . TextChannel ) . send ( { embeds : [ new client . embed ( ) . setColor ( '#420420' ) . setTitle ( 'Error caught!' ) . setDescription ( ` **Error:** \` ${ error . message } \` \ n \ n**Stack:** \` ${ ` ${ error . stack } ` . slice ( 0 , 2500 ) } \` ` ) ] } )
2022-11-11 19:58:11 -05:00
} ) ;
2022-11-13 08:46:50 -05:00
process . on ( 'error' , async ( error : Error ) = > {
2022-11-13 19:18:15 -05:00
console . log ( error ) ;
2022-11-18 22:23:33 -05:00
( client . channels . resolve ( client . config . mainServer . channels . errors ) as Discord . TextChannel ) . send ( { embeds : [ new client . embed ( ) . setColor ( '#420420' ) . setTitle ( 'Error caught!' ) . setDescription ( ` **Error:** \` ${ error . message } \` \ n \ n**Stack:** \` ${ ` ${ error . stack } ` . slice ( 0 , 2500 ) } \` ` ) ] } )
2022-11-11 19:58:11 -05:00
} ) ;
// Daggerwin MP loop
setInterval ( async ( ) = > {
if ( ! client . config . botSwitches . mpstats ) return ;
2022-11-17 12:58:19 -05:00
const msg = await ( client . channels . resolve ( '904192878140608563' ) as Discord . TextChannel ) . messages . fetch ( '1042835699906400346' )
2022-11-11 19:58:11 -05:00
const embed = new client . embed ( ) ;
let Players = [ ] ;
let Server : any ;
2022-11-16 13:53:42 -05:00
let CSG ;
2022-11-11 19:58:11 -05:00
let xmlData = undefined ;
// Connect to DB to retrieve the Gameserver info to fetch data.
2022-11-16 13:53:42 -05:00
MPDB . sync ( ) ;
2022-11-11 19:58:11 -05:00
const newServerId = client . config . mainServer . id
2022-11-16 13:53:42 -05:00
const ServerURL = MPDB . findOne ( { where : { serverId : newServerId } } )
const DBURL = ( await ServerURL ) . ip
2022-11-19 05:17:04 -05:00
const DBCode = ( await ServerURL ) . code
const verifyURL = DBURL . match ( /http/ ) ;
2022-11-11 19:58:11 -05:00
const completedURL_DSS = DBURL + '/feed/dedicated-server-stats.json?code=' + DBCode
const completedURL_CSG = DBURL + '/feed/dedicated-server-savegame.html?code=' + DBCode + '&file=careerSavegame'
2022-11-19 05:17:04 -05:00
if ( ! verifyURL ) return msg . edit ( { content : 'Invalid gameserver IP, please update!' , embeds : null } )
2022-11-16 13:53:42 -05:00
console . log ( DBURL + '\n' + DBCode )
2022-11-11 19:58:11 -05:00
try {
2022-11-13 08:46:50 -05:00
Server = await client . axios . get ( completedURL_DSS , { timeout : 4000 } )
2022-11-11 19:58:11 -05:00
} catch ( err ) {
2022-11-16 13:53:42 -05:00
if ( client . config . botSwitches . mpstatsDebug ) {
console . log ( err )
} else {
console . log ( ` [ ${ client . moment ( ) . format ( 'DD/MM/YY HH:mm:ss' ) } ] dag mp dss fail ` )
}
embed . setTitle ( 'Host is not responding.' ) . setColor ( client . config . embedColorRed )
msg . edit ( { content : null , embeds : [ embed ] } )
2022-11-11 19:58:11 -05:00
return ;
}
try {
2022-11-13 08:46:50 -05:00
CSG = await client . axios . get ( completedURL_CSG , { timeout : 4100 } ) . then ( ( xml : any ) = > {
xmlData = client . xjs . xml2js ( xml . data , { compact : true , spaces : 2 } ) . careerSavegame ;
2022-11-11 19:58:11 -05:00
} )
} catch ( err ) {
2022-11-16 13:53:42 -05:00
if ( client . config . botSwitches . mpstatsDebug ) {
console . log ( err )
} else {
console . log ( ` [ ${ client . moment ( ) . format ( 'DD/MM/YY HH:mm:ss' ) } ] dag mp csg fail ` )
}
2022-11-11 19:58:11 -05:00
}
if ( xmlData == undefined ) {
2022-11-16 13:53:42 -05:00
if ( client . config . botSwitches . mpstatsDebug ) {
console . log ( xmlData )
} else {
console . log ( ` [ ${ client . moment ( ) . format ( 'DD/MM/YY HH:mm:ss' ) } ] dag mp csg failed to convert ` )
}
2022-11-11 19:58:11 -05:00
embed . setFooter ( { text : 'XML Data retrieve failed. Retrying in next minute.' } )
msg . edit ( { embeds : [ embed ] } )
}
const DB = require ( ` ./database/MPPlayerData.json ` ) ;
DB . push ( Server . data . slots . used )
2022-11-16 13:53:42 -05:00
fs . writeFileSync ( __dirname + ` /database/MPPlayerData.json ` , JSON . stringify ( DB ) )
2022-11-11 19:58:11 -05:00
// Number format function
function formatNumber ( number : any , digits : any , icon : any ) {
var n = Number ( number )
return n . toLocaleString ( undefined , { minimumFractionDigits : digits } ) + icon
}
var timeScale = Number ( xmlData ? . settings ? . timeScale . _text )
if ( Server . data . server . name . length == 0 ) {
embed . setTitle ( 'The server seems to be offline.' ) . setColor ( client . config . embedColorRed ) ;
2022-11-16 13:53:42 -05:00
msg . edit ( { content : 'This embed will resume when server is back online.' , embeds : [ embed ] } )
2022-11-11 19:58:11 -05:00
} else {
const embed1 = new client . embed ( ) . setColor ( client . config . embedColor ) . setTitle ( 'Server details' ) . addFields (
2022-11-16 13:53:42 -05:00
{ name : 'Current Map' , value : ` ${ Server . data . server . mapName . length == 0 ? '\u200b' : Server . data . server . mapName } ` , inline : true } ,
2022-11-17 12:58:19 -05:00
{ name : 'Version' , value : ` ${ Server . data . server . version . length == 0 ? '\u200b' : Server . data . server . version } ` , inline : true } ,
2022-11-16 13:53:42 -05:00
{ name : 'In-game Time' , value : ` ${ ( '0' + Math . floor ( ( Server . data . server . dayTime / 3600 / 1000 ) ) ) . slice ( - 2 ) } : ${ ( '0' + Math . floor ( ( Server . data . server . dayTime / 60 / 1000 ) % 60 ) ) . slice ( - 2 ) } ` , inline : true } ,
{ name : 'Slot Usage' , value : ` ${ Number ( xmlData ? . slotSystem ? . _attributes ? . slotUsage ) . toLocaleString ( 'en-US' ) } ` , inline : true } ,
{ name : 'Timescale' , value : ` ${ formatNumber ( timeScale , 0 , 'x' ) } ` , inline : true }
2022-11-11 19:58:11 -05:00
) ;
await Server . data . slots . players . filter ( ( x ) = > x . isUsed !== false ) . forEach ( player = > {
2022-11-16 13:53:42 -05:00
Players . push ( ` ** ${ player . name } ${ player . isAdmin ? '| admin' : '' } ** \ nFarming for ${ ( Math . floor ( player . uptime / 60 ) ) } hr & ${ ( '0' + ( player . uptime % 60 ) ) . slice ( - 2 ) } min ` )
2022-11-11 19:58:11 -05:00
} )
embed . setDescription ( ` ${ Server . data . slots . used == 0 ? '*No players online*' : Players . join ( '\n\n' ) } ` ) . setTitle ( Server . data . server . name ) . setColor ( client . config . embedColor )
embed . setAuthor ( { name : ` ${ Server . data . slots . used } / ${ Server . data . slots . capacity } ` } ) ;
2022-11-16 13:53:42 -05:00
msg . edit ( { content : 'This embed updates every minute.' , embeds : [ embed1 , embed ] } )
2022-11-11 19:58:11 -05:00
}
} , 60000 )
// YouTube Upload notification
setInterval ( async ( ) = > {
2022-11-17 12:58:19 -05:00
client . YTLoop ( 'UCQ8k8yTDLITldfWYKDs3xFg' , 'Daggerwin' , '904192878140608563' ) ; // 528967918772551702 = #videos-and-streams
2022-11-16 13:53:42 -05:00
client . YTLoop ( 'UCguI73--UraJpso4NizXNzA' , 'Machinery Restorer' , '767444045520961567' ) // 767444045520961567 = #machinery-restorer
2022-11-18 22:23:33 -05:00
console . log ( ` [ ${ client . moment ( ) . format ( 'DD/MM/YY HH:mm:ss' ) } ] YTLoop interval ` )
2022-11-11 19:58:11 -05:00
} , 300000 )
// Event loop for punishments and daily msgs
setInterval ( async ( ) = > {
const now = Date . now ( )
const lrsStart = client . config . LRSstart ;
2022-11-13 08:46:50 -05:00
2022-11-15 09:06:18 -05:00
client . punishments . _content . filter ( ( x :Punishment ) = > x . endTime <= now && ! x . expired ) . forEach ( async ( punishment :Punishment ) = > {
2022-11-13 08:46:50 -05:00
console . log ( ` \ x1b[36m[ ${ client . moment ( ) . format ( 'DD/MM/YY HH:mm:ss' ) } ] ` , '\x1b[32m' + ` ${ punishment . member } \ 's ${ punishment . type } should expire now ` ) ;
const unpunishResult = await client . punishments . removePunishment ( punishment . id , client . user . id , 'Time\'s up!' ) ;
console . log ( ` \ x1b[36m[ ${ client . moment ( ) . format ( 'DD/MM/YY HH:mm:ss' ) } ] ` , '\x1b[32m' + unpunishResult ) ;
2022-11-11 19:58:11 -05:00
} ) ;
2022-11-13 08:46:50 -05:00
2022-11-11 19:58:11 -05:00
const formattedDate = Math . floor ( ( now - lrsStart ) / 1000 / 60 / 60 / 24 ) ;
const dailyMsgs = require ( './database/dailyMsgs.json' ) ;
2022-11-13 08:46:50 -05:00
interface UserLevels {
messages : number ,
level : number
}
2022-11-11 19:58:11 -05:00
if ( ! dailyMsgs . some ( x = > x [ 0 ] === formattedDate ) ) {
2022-11-13 08:46:50 -05:00
let total = Object . values < UserLevels > ( client . userLevels . _content ) . reduce ( ( a , b ) = > a + b . messages , 0 ) ; // sum of all users
2022-11-15 09:06:18 -05:00
const yesterday = dailyMsgs . find ( ( x :Array < number > ) = > x [ 0 ] === formattedDate - 1 ) ;
2022-11-11 19:58:11 -05:00
if ( total < yesterday ) { // messages went down.
total = yesterday
}
dailyMsgs . push ( [ formattedDate , total ] ) ;
2022-11-17 12:58:19 -05:00
fs . writeFileSync ( __dirname + '/database/dailyMsgs.json' , JSON . stringify ( dailyMsgs ) )
2022-11-13 08:46:50 -05:00
console . log ( ` \ x1b[36m[ ${ client . moment ( ) . format ( 'DD/MM/YY HH:mm:ss' ) } ] \ x1b[33m ` , ` Pushed [ ${ formattedDate } , ${ total } ] to dailyMsgs ` )
2022-11-11 19:58:11 -05:00
}
} , 5000 )
2022-11-13 08:46:50 -05:00
// Punishments
Object . assign ( client . punishments , {
createId ( ) {
2022-11-15 09:06:18 -05:00
return Math . max ( . . . client . punishments . _content . map ( ( x :Punishment ) = > x . id ) , 0 ) + 1 ;
2022-11-13 08:46:50 -05:00
} ,
2022-11-15 09:06:18 -05:00
async addPunishment ( type : string , member : any , options : punOpt , moderator : string ) {
2022-11-13 08:46:50 -05:00
const now = Date . now ( ) ;
2022-11-15 09:06:18 -05:00
const { time , reason , interaction } = options ;
const ms = require ( 'ms' ) ;
2022-11-13 08:46:50 -05:00
let timeInMillis ;
if ( type !== 'mute' ) {
timeInMillis = time ? ms ( time ) : null ;
} else {
timeInMillis = time ? ms ( time ) : 2419200000 ;
}
switch ( type ) {
case 'ban' :
2022-11-15 09:06:18 -05:00
const banData : Punishment = { type , id : this.createId ( ) , member : member.id , moderator , time : now } ;
let dm1 :any ;
2022-11-13 08:46:50 -05:00
try {
2022-11-18 06:29:03 -05:00
dm1 = await member . send ( ` You've been banned from ${ interaction . guild . name } ${ timeInMillis ? ` for ${ client . formatTime ( timeInMillis , 4 , { longNames : true , commas : true } )} ` : 'forever' } for reason \` ${ reason || 'Reason unspecified' } \` (Case # ${ banData . id } ) ` )
2022-11-13 08:46:50 -05:00
} catch ( err ) {
2022-11-15 09:06:18 -05:00
setTimeout ( ( ) = > interaction . channel . send ( 'Failed to DM user.' ) , 500 )
2022-11-13 08:46:50 -05:00
}
2022-11-15 09:06:18 -05:00
const banResult = await interaction . guild . bans . create ( member . id , { reason : ` ${ reason || 'Reason unspecified' } | Case # ${ banData . id } ` } ) . catch ( ( err : Error ) = > err . message ) ;
2022-11-13 08:46:50 -05:00
if ( typeof banResult === 'string' ) {
dm1 . delete ( ) ;
return ` Ban was unsuccessful: ${ banResult } ` ;
} else {
if ( timeInMillis ) {
banData . endTime = now + timeInMillis ;
banData . duration = timeInMillis ;
}
if ( reason ) banData . reason = reason ;
client . makeModlogEntry ( banData , client ) ;
this . addData ( banData ) ;
this . forceSave ( ) ;
return new client . embed ( ) . setColor ( client . config . embedColor ) . setTitle ( ` Case # ${ banData . id } : Ban ` ) . setDescription ( ` ${ member . tag } \ n<@ ${ member . id } > \ n( \` ${ member . id } \` ) ` ) . addFields (
2022-11-15 09:06:18 -05:00
{ name : 'Reason' , value : ` \` ${ reason || 'Reason unspecified' } \` ` } ,
2022-11-18 06:29:03 -05:00
{ name : 'Duration' , value : ` ${ timeInMillis ? ` for ${ client . formatTime ( timeInMillis , 4 , { longNames : true , commas : true } )} ` : 'forever' } ` }
2022-11-13 08:46:50 -05:00
)
}
case 'softban' :
const guild = member . guild ;
2022-11-15 09:06:18 -05:00
const softbanData : Punishment = { type , id : this.createId ( ) , member : member.user.id , moderator , time : now } ;
const dm2 = await member . send ( ` You've been softbanned from ${ member . guild . name } for reason \` ${ reason || 'Reason unspecified' } \` (Case # ${ softbanData . id } ) ` ) . catch ( err = > setTimeout ( ( ) = > interaction . channel . send ( ` Failed to DM <@ ${ member . user . id } >. ` ) , 500 ) ) ;
const softbanResult = await member . ban ( { deleteMessageDays : 7 , reason : ` ${ reason || 'Reason unspecified' } | Case # ${ softbanData . id } ` } ) . catch ( ( err : Error ) = > err . message ) ;
2022-11-13 08:46:50 -05:00
if ( typeof softbanResult === 'string' ) {
dm2 . delete ( ) ;
return ` Softban was unsuccessful: ${ softbanResult } ` ;
} else {
2022-11-15 09:06:18 -05:00
const unbanResult = guild . members . unban ( softbanData . member , ` ${ reason || 'Reason unspecified' } | Case # ${ softbanData . id } ` ) . catch ( ( err : Error ) = > err . message ) ;
2022-11-13 08:46:50 -05:00
if ( typeof unbanResult === 'string' ) {
return ` Softban (unban) was unsuccessful: ${ softbanResult } `
} else {
if ( reason ) softbanData . reason = reason ;
client . makeModlogEntry ( softbanData , client ) ;
this . addData ( softbanData )
this . forceSave ( ) ;
return new client . embed ( ) . setColor ( client . config . embedColor ) . setTitle ( ` Case # ${ softbanData . id } : Softban ` ) . setDescription ( ` ${ member . user . tag } \ n<@ ${ member . user . id } > \ n( \` ${ member . user . id } \` ) ` ) . addFields (
2022-11-15 09:06:18 -05:00
{ name : 'Reason' , value : ` \` ${ reason || 'Reason unspecified' } \` ` }
2022-11-13 08:46:50 -05:00
)
}
}
case 'kick' :
2022-11-15 09:06:18 -05:00
const kickData : Punishment = { type , id : this.createId ( ) , member : member.user.id , moderator , time : now } ;
const dm3 = await member . send ( ` You've been kicked from ${ member . guild . name } for reason \` ${ reason || 'Reason unspecified' } \` (Case # ${ kickData . id } ) ` ) . catch ( ( err :Error ) = > setTimeout ( ( ) = > interaction . channel . send ( ` Failed to DM <@ ${ member . user . id } >. ` ) , 500 ) ) ;
const kickResult = await member . kick ( ` ${ reason || 'Reason unspecified' } | Case # ${ kickData . id } ` ) . catch ( ( err :Error ) = > err . message ) ;
2022-11-13 08:46:50 -05:00
if ( typeof kickResult === 'string' ) {
dm3 . delete ( ) ;
return ` Kick was unsuccessful: ${ kickResult } ` ;
} else {
if ( reason ) kickData . reason = reason ;
client . makeModlogEntry ( kickData , client ) ;
this . addData ( kickData ) ;
this . forceSave ( ) ;
return new client . embed ( ) . setColor ( client . config . embedColor ) . setTitle ( ` Case # ${ kickData . id } : Kick ` ) . setDescription ( ` ${ member . user . tag } \ n<@ ${ member . user . id } > \ n( \` ${ member . user . id } \` ) ` ) . addFields (
2022-11-15 09:06:18 -05:00
{ name : 'Reason' , value : ` \` ${ reason || 'Reason unspecified' } \` ` }
2022-11-13 08:46:50 -05:00
)
}
case 'mute' :
2022-11-15 09:06:18 -05:00
const muteData : Punishment = { type , id : this.createId ( ) , member : member.user.id , moderator , time : now } ;
2022-11-13 08:46:50 -05:00
let muteResult ;
2022-11-18 06:29:03 -05:00
const dm4 = await member . send ( ` You've been muted in ${ member . guild . name } ${ timeInMillis ? ` for ${ client . formatTime ( timeInMillis , 4 , { longNames : true , commas : true } )} ` : 'forever' } for reason \` ${ reason || 'Reason unspecified' } \` (Case # ${ muteData . id } ) ` ) . catch ( ( err :Error ) = > setTimeout ( ( ) = > interaction . channel . send ( 'Failed to DM user.' ) , 500 ) ) ;
2022-11-13 08:46:50 -05:00
if ( timeInMillis ) {
2022-11-15 09:06:18 -05:00
muteResult = await member . timeout ( timeInMillis , ` ${ reason || 'Reason unspecified' } | Case # ${ muteData . id } ` ) . catch ( ( err : Error ) = > err . message ) ;
2022-11-13 08:46:50 -05:00
} else {
2022-11-15 09:06:18 -05:00
muteResult = await member . timeout ( 2419200000 , ` ${ reason || 'Reason unspecified' } | Case # ${ muteData . id } ` ) . catch ( ( err : Error ) = > err . message ) ;
2022-11-13 08:46:50 -05:00
}
if ( typeof muteResult === 'string' ) {
dm4 . delete ( ) ;
return ` Mute was unsuccessful: ${ muteResult } ` ;
} else {
if ( timeInMillis ) {
muteData . endTime = now + timeInMillis ;
muteData . duration = timeInMillis
}
if ( reason ) muteData . reason = reason ;
client . makeModlogEntry ( muteData , client ) ;
this . addData ( muteData ) ;
this . forceSave ( ) ;
2022-11-15 09:06:18 -05:00
const embedm = new client . embed ( ) . setColor ( client . config . embedColor ) . setTitle ( ` Case # ${ muteData . id } : Mute ` ) . setDescription ( ` ${ member . user . tag } \ n<@ ${ member . user . id } > \ n( \` ${ member . user . id } \` ) ` ) . addFields (
{ name : 'Reason' , value : ` \` ${ reason || 'Reason unspecified' } \` ` } ,
2022-11-18 06:29:03 -05:00
{ name : 'Duration' , value : ` ${ client . formatTime ( timeInMillis , 4 , { longNames : true , commas : true } )} ` }
2022-11-13 08:46:50 -05:00
)
if ( moderator !== '795443537356521502' ) { return embedm } ;
}
case 'warn' :
2022-11-15 09:06:18 -05:00
const warnData : Punishment = { type , id : this.createId ( ) , member : member.user.id , moderator , time : now } ;
const warnResult = await member . send ( ` You've been warned in ${ member . guild . name } for reason \` ${ reason || 'Reason unspecified' } \` (Case # ${ warnData . id } ) ` ) . catch ( ( err :Error ) = > setTimeout ( ( ) = > interaction . channel . send ( ` Failed to DM <@ ${ member . user . id } >. ` ) , 500 ) ) ;
2022-11-13 08:46:50 -05:00
if ( typeof warnResult === 'string' ) {
return ` Warn was unsuccessful: ${ warnResult } ` ;
} else {
if ( reason ) warnData . reason = reason ;
client . makeModlogEntry ( warnData , client ) ;
this . addData ( warnData ) ;
this . forceSave ( ) ;
const embedw = new client . embed ( ) . setColor ( client . config . embedColor ) . setTitle ( ` Case # ${ warnData . id } : Warn ` ) . setDescription ( ` ${ member . user . tag } \ n<@ ${ member . user . id } > \ n( \` ${ member . user . id } \` ) ` ) . addFields (
2022-11-15 09:06:18 -05:00
{ name : 'Reason' , value : ` \` ${ reason || 'Reason unspecified' } \` ` }
2022-11-13 08:46:50 -05:00
)
if ( moderator !== '795443537356521502' ) { return embedw } ;
}
}
} ,
async removePunishment ( caseId : number , moderator : any , reason : string ) {
const now = Date . now ( ) ;
const punishment = this . _content . find ( ( x :any ) = > x . id === caseId ) ;
const id = this . createId ( ) ;
if ( ! punishment ) return 'Punishment not found.' ;
if ( [ 'ban' , 'mute' ] . includes ( punishment . type ) ) {
const guild = client . guilds . cache . get ( client . config . mainServer . id ) as Discord . Guild ;
let removePunishmentResult ;
if ( punishment . type === 'ban' ) {
// unban
2022-11-15 09:06:18 -05:00
removePunishmentResult = await guild . members . unban ( punishment . member , ` ${ reason || 'Reason unspecified' } | Case # ${ id } ` ) . catch ( ( err : TypeError ) = > err . message ) ;
2022-11-13 08:46:50 -05:00
} else if ( punishment . type === 'mute' ) {
//remove role
const member = await guild . members . fetch ( punishment . member ) . catch ( err = > undefined ) ;
if ( member ) {
removePunishmentResult = await member
if ( typeof removePunishmentResult !== 'string' ) {
2022-11-15 09:06:18 -05:00
member . timeout ( null , ` ${ reason || 'Reason unspecified' } | Case # ${ id } ` )
2022-11-13 08:46:50 -05:00
removePunishmentResult . send ( ` You've been unmuted in ${ removePunishmentResult . guild . name } . ` )
removePunishmentResult = removePunishmentResult . user ; //removing a role returns a guildmember
}
} else {
// user probably left, lets quietly nuke the punishment.
const removePunishmentData = { type : ` un ${ punishment . type } ` , id , cancels : punishment.id , member : punishment.member , reason , moderator , time : now } ;
this . _content [ this . _content . findIndex ( x = > x . id === punishment . id ) ] . expired = true ;
this . addData ( removePunishmentData ) . forceSave ( ) ;
}
}
if ( typeof removePunishmentResult === 'string' ) return ` Un ${ punishment . type } was unsuccessful: ${ removePunishmentResult } ` ;
else {
const removePunishmentData = { type : ` un ${ punishment . type } ` , id , cancels : punishment.id , member : punishment.member , reason , moderator , time : now } ;
client . makeModlogEntry ( removePunishmentData , client ) ;
this . _content [ this . _content . findIndex ( x = > x . id === punishment . id ) ] . expired = true ;
this . addData ( removePunishmentData ) . forceSave ( ) ;
2022-11-18 06:29:03 -05:00
return ` Successfully ${ punishment . type === 'ban' ? 'unbanned' : 'unmuted' } ** ${ removePunishmentResult ? . tag } ** ( \` ${ removePunishmentResult ? . id } \` ) for reason \` ${ reason || 'Reason unspecified' } \` ` ;
2022-11-13 08:46:50 -05:00
}
} else {
try {
const removePunishmentData = { type : 'removeOtherPunishment' , id , cancels : punishment.id , member : punishment.member , reason , moderator , time : now } ;
client . makeModlogEntry ( removePunishmentData , client ) ;
this . _content [ this . _content . findIndex ( x = > x . id === punishment . id ) ] . expired = true ;
this . addData ( removePunishmentData ) . forceSave ( ) ;
return ` Successfully removed Case # ${ punishment . id } (type: ${ punishment . type } , user: ${ punishment . member } ). ` ;
2022-11-15 09:06:18 -05:00
} catch ( error : any ) {
2022-11-13 08:46:50 -05:00
return ` ${ punishment . type [ 0 ] . toUpperCase ( ) + punishment . type . slice ( 1 ) } removal was unsuccessful: ${ error . message } `
}
}
}
} )