2020-07-20 00:23:47 +02:00
import { autoReactionsEvt } from "../types" ;
import { isDiscordRESTError } from "src/utils" ;
import { LogType } from "src/data/LogType" ;
2020-07-22 22:56:21 +03:00
import { logger } from "../../../logger" ;
2020-07-30 15:50:30 +03:00
import { LogsPlugin } from "../../Logs/LogsPlugin" ;
2020-08-07 00:09:05 +03:00
import { Constants , GuildChannel } from "eris" ;
import { memberHasChannelPermissions } from "../../../utils/memberHasChannelPermissions" ;
const p = Constants . Permissions ;
2020-07-20 00:23:47 +02:00
2020-07-21 17:55:25 +02:00
export const AddReactionsEvt = autoReactionsEvt ( {
2020-07-20 00:23:47 +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 ;
2020-08-07 00:09:05 +03:00
if (
! memberHasChannelPermissions ( message . member , message . channel as GuildChannel , [
p . readMessages ,
p . readMessageHistory ,
p . addReactions ,
] )
) {
const logs = pluginData . getPlugin ( LogsPlugin ) ;
logs . log ( LogType . BOT_ALERT , {
body : ` Missing permissions to apply auto-reactions in <# ${ message . channel . id } >. Ensure I can read messages, read message history, and add reactions. ` ,
} ) ;
return ;
}
2020-07-20 00:23:47 +02:00
for ( const reaction of autoReaction . reactions ) {
try {
2020-08-07 00:09:05 +03:00
await message . addReaction ( reaction ) ;
2020-07-20 00:23:47 +02:00
} catch ( e ) {
if ( isDiscordRESTError ( 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 ;
}
}
}
} ,
} ) ;