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-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
async listener ( meta ) {
const pluginData = meta . pluginData ;
const msg = meta . args . message ;
const autoReaction = await pluginData . state . autoReactions . getForChannel ( msg . channel . id ) ;
if ( ! autoReaction ) return ;
for ( const reaction of autoReaction . reactions ) {
try {
await msg . addReaction ( reaction ) ;
} catch ( e ) {
if ( isDiscordRESTError ( e ) ) {
logger . warn (
` Could not apply auto-reaction to ${ msg . channel . id } / ${ msg . id } in guild ${ pluginData . guild . name } ( ${ pluginData . guild . id } ) (error code ${ e . code } ) ` ,
) ;
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-07-20 00:23:47 +02:00
body : ` Could not apply auto-reactions in <# ${ msg . channel . id } > for message \` ${ msg . id } \` . Make sure nothing is deleting the message before the reactions are applied. ` ,
} ) ;
} else {
2020-07-30 15:50:30 +03:00
logs . log ( LogType . BOT_ALERT , {
2020-07-20 00:23:47 +02:00
body : ` Could not apply auto-reactions in <# ${ msg . channel . id } > for message \` ${ msg . id } \` . Error code ${ e . code } . ` ,
} ) ;
}
return ;
} else {
throw e ;
}
}
}
} ,
} ) ;