import { DisharmonyClient , Logger } from "disharmony" ; import ActivityMonitorConfig from "../models/activity-monitor-config" ; import Guild from "../models/guild" ; import GuildMember from "../models/guild-member" ; import Message from "../models/message" ; export default class ActivityRegisterer { public startListening ( ) { this . client . onMessage . sub ( message => this . registerActivity ( message . guild , message . member , message . textChannelName ) ) ; this . client . onVoiceStateUpdate . sub ( args => this . registerActivity ( new Guild ( args . newMember . djs . guild ) , args . newMember , args . newMember . voiceChannelName || args . oldMember . voiceChannelName ) ) ; } public async registerActivity ( guild , member , channelName , date ? ) { if ( ! member || ! guild || member . id === this . client . botId || ! guild . botHasPermissions ( this . client . config . requiredPermissions ) ) return ; await guild . loadDocument ( ) ; if ( ! guild . allowRoleAddition || guild . isMemberIgnored ( member ) || ! guild . isRoleConfigured ( guild . activeRoleId ) || guild . isRoleBadlyConfigured ( guild . activeRoleId ) ) return ; guild . users . set ( member . id , date || new Date ( ) ) ; await this . markMemberActive ( guild , member , channelName ) ; await guild . save ( ) ; } private async markMemberActive ( guild , member , channelName ) { try { const reasonStr = `Activity detected in channel '${ channelName }'` ; if ( member . djs . roles . has ( guild . activeRoleId ) ) return ; await member . addRole ( guild . activeRoleId , reasonStr ) ; const hasInactiveRole = guild . inactiveRoleId && guild . inactiveRoleId !== "disabled" ; let didRemoveInactiveRole = false ; if ( hasInactiveRole ) { await member . removeRole ( guild . inactiveRoleId , reasonStr ) ; didRemoveInactiveRole = true ; } Logger . logEvent ( "MarkedMemberActive" , { guildId : guild . id , memberId : member . id , removedInactiveRole : didRemoveInactiveRole } ) ; } catch ( e ) { if ( e . code !== 50013 ) Logger . debugLogError ( `Error marking user ${ member . id } active in guild ${ guild . id }.` , e ) ; Logger . logEvent ( "ErrorMarkingMemberActive" , { guildId : guild . id , memberId : member . id , code : e . code } ) ; } } constructor ( private client ) { } }