2019-05-10 16:36:17 +02:00
const { Command } = require ( 'discord-akairo' ) ;
2019-11-27 12:16:30 +01:00
const leaveChannel = require ( '../../models' ) . leaveChannel ;
2019-05-10 16:36:17 +02:00
class byeCommand extends Command {
constructor ( ) {
super ( 'bye' , {
2019-11-09 12:02:55 +01:00
aliases : [ 'bye' , 'leave' ] ,
2019-05-10 16:36:17 +02:00
category : 'admin' ,
channelRestriction : 'guild' ,
userPermissions : [ 'MANAGE_CHANNELS' ] ,
2019-11-09 12:02:55 +01:00
clientPermissions : [ 'SEND_MESSAGES' ] ,
2019-05-10 16:36:17 +02:00
args : [
2019-11-09 12:02:55 +01:00
{
id : 'remove' ,
match : 'flag' ,
flag : '--remove'
} ,
2019-05-10 16:36:17 +02:00
{
id : 'message' ,
type : 'string' ,
2019-11-09 12:02:55 +01:00
match : 'rest' ,
default : '[member] just left the server :('
2019-05-10 16:36:17 +02:00
}
] ,
description : {
2019-05-11 18:34:19 +02:00
content : 'Send a message to the current channel when a person leave, you can use [member] to show the member username and [server] to show the name of the server' ,
2019-05-10 16:36:17 +02:00
usage : '[bye message]' ,
2019-11-09 12:02:55 +01:00
examples : [ '[member] left the server, he deserve a ban' ]
2019-05-10 16:36:17 +02:00
}
} ) ;
}
async exec ( message , args ) {
2019-11-27 12:16:30 +01:00
const leave = await leaveChannel . findOne ( { where : { guildID : message . guild . id } } ) ;
2019-05-10 16:36:17 +02:00
2019-11-09 12:02:55 +01:00
if ( args . remove ) {
2019-11-27 12:16:30 +01:00
if ( leave ) {
leave . destroy ( { where : { guildID : message . guild . id , channelID : message . channel . id } } ) ;
return message . channel . send ( 'successfully deleted the leave message' ) ;
} else {
return message . channel . send ( 'Did not find the a leave message, are you sure you have one setup?' ) ;
}
2019-11-09 12:02:55 +01:00
}
2019-11-27 12:16:30 +01:00
if ( ! args . message ) {
return message . channel . send ( 'Please provide a message' ) ;
}
if ( ! leave ) {
const body = { guildID : message . guild . id , channelID : message . channel . id , message : args . message } ;
await leaveChannel . create ( body ) ;
return message . channel . send ( ` The leave message have been set with ${ args . message } ` ) ;
} else {
message . channel . send ( 'The server already have a leave message, do you want to replace it? y/n' ) ;
const filter = m => m . content && m . author . id == message . author . id ;
message . channel . awaitMessages ( filter , { time : 5000 , max : 1 , errors : [ 'time' ] } )
. then ( async messages => {
let messageContent = messages . map ( messages => messages . content ) ;
if ( messageContent == 'y' || messageContent == 'yes' ) {
const body = { guildID : message . guild . id , channelID : message . channel . id , message : args . message } ;
2019-11-27 16:03:40 +01:00
await leaveChannel . update ( body , { where : { guildID : message . guild . id } } ) ;
2019-11-27 12:16:30 +01:00
return message . channel . send ( ` The leave message have been set with ${ args . message } ` ) ;
} else {
return message . channel . send ( 'Not updating.' ) ;
}
} )
. catch ( err => {
console . error ( err ) ;
return message . channel . send ( 'Took too long to answer. didin\'t update anything.' ) ;
} ) ;
}
2019-05-10 16:36:17 +02:00
}
}
module . exports = byeCommand ;