2021-06-06 23:51:32 +02:00
import { GuildChannel , Permissions } from "discord.js" ;
2020-10-01 01:43:38 +03:00
import { LogType } from "../../../data/LogType" ;
2021-06-30 23:06:02 +02:00
import { isDiscordAPIError } from "../../../utils" ;
2020-08-07 01:21:31 +03:00
import { getMissingChannelPermissions } from "../../../utils/getMissingChannelPermissions" ;
import { missingPermissionError } from "../../../utils/missingPermissionError" ;
2021-06-06 23:51:32 +02:00
import { readChannelPermissions } from "../../../utils/readChannelPermissions" ;
import { LogsPlugin } from "../../Logs/LogsPlugin" ;
import { autoReactionsEvt } from "../types" ;
2021-06-01 02:05:55 +02:00
const p = Permissions . FLAGS ;
2020-07-20 00:23:47 +02:00
2020-07-21 17:55:25 +02:00
export const AddReactionsEvt = autoReactionsEvt ( {
2021-07-04 17:41:44 +02:00
event : "messageCreate" ,
2020-07-30 16:43:32 +03:00
allowBots : true ,
allowSelf : true ,
2020-07-20 00:23:47 +02:00
2020-08-07 00:09:05 +03:00
async listener ( { pluginData , args : { message } } ) {
const autoReaction = await pluginData . state . autoReactions . getForChannel ( message . channel . id ) ;
2020-07-20 00:23:47 +02:00
if ( ! autoReaction ) return ;
2021-06-01 02:05:55 +02:00
const me = pluginData . guild . members . cache . get ( pluginData . client . user ! . id ) ! ;
2020-08-07 01:21:31 +03:00
const missingPermissions = getMissingChannelPermissions (
me ,
message . channel as GuildChannel ,
2021-06-01 02:05:55 +02:00
readChannelPermissions | p . ADD_REACTIONS ,
2020-08-07 01:21:31 +03:00
) ;
if ( missingPermissions ) {
2020-08-07 00:09:05 +03:00
const logs = pluginData . getPlugin ( LogsPlugin ) ;
logs . log ( LogType . BOT_ALERT , {
2020-08-07 01:21:31 +03:00
body : ` Cannot apply auto-reactions in <# ${ message . channel . id } >. ${ missingPermissionError ( missingPermissions ) } ` ,
2020-08-07 00:09:05 +03:00
} ) ;
return ;
}
2020-07-20 00:23:47 +02:00
for ( const reaction of autoReaction . reactions ) {
try {
2021-06-01 02:05:55 +02:00
await message . react ( reaction ) ;
2020-07-20 00:23:47 +02:00
} catch ( e ) {
2021-06-30 23:06:02 +02:00
if ( isDiscordAPIError ( e ) ) {
2020-07-30 15:50:30 +03:00
const logs = pluginData . getPlugin ( LogsPlugin ) ;
2020-07-20 00:23:47 +02:00
if ( e . code === 10008 ) {
2020-07-30 15:50:30 +03:00
logs . log ( LogType . BOT_ALERT , {
2020-08-07 00:09:05 +03:00
body : ` Could not apply auto-reactions in <# ${ message . channel . id } > for message \` ${ message . id } \` . Make sure nothing is deleting the message before the reactions are applied. ` ,
2020-07-20 00:23:47 +02:00
} ) ;
} else {
2020-07-30 15:50:30 +03:00
logs . log ( LogType . BOT_ALERT , {
2020-08-07 00:09:05 +03:00
body : ` Could not apply auto-reactions in <# ${ message . channel . id } > for message \` ${ message . id } \` . Error code ${ e . code } . ` ,
2020-07-20 00:23:47 +02:00
} ) ;
}
2020-08-07 00:09:05 +03:00
break ;
2020-07-20 00:23:47 +02:00
} else {
throw e ;
}
}
}
} ,
} ) ;