Haha-Yes/commands/admin/autoresponse.js

83 lines
2.8 KiB
JavaScript
Raw Normal View History

2018-10-19 23:39:42 +02:00
const fs = require('fs');
2018-12-30 01:20:24 +01:00
const { Command } = require('discord-akairo');
class autoresponseCommand extends Command {
constructor() {
super('autoresponse', {
aliases: ['autoresponse'],
category: 'admin',
2018-10-19 23:39:42 +02:00
args: [
{
2018-12-30 01:20:24 +01:00
id: 'text',
type: 'string'
2018-11-20 19:33:04 +01:00
},
{
2018-12-30 01:20:24 +01:00
id: 'all',
type: 'string'
2018-10-19 23:39:42 +02:00
}
2018-12-30 01:20:24 +01:00
],
userPermissions: ['ADMINISTRATOR'],
channelRestriction: 'guild',
description: {
content: 'enable/disable autoresponse',
usage: '[enable/disable] (optional) [all]',
examples: ['enable all']
}
2018-10-19 23:39:42 +02:00
});
}
2018-12-30 01:20:24 +01:00
async exec(message, args) {
let text = args.text;
let all = args.all;
2018-10-19 23:39:42 +02:00
2019-01-02 04:35:34 +01:00
let autoresponse = {};
let json = JSON.stringify(autoresponse);
2018-10-19 23:39:42 +02:00
2019-01-02 04:35:34 +01:00
if (all == 'all') {
const guild = this.client.guilds.get(message.guild.id);
2018-12-30 01:20:24 +01:00
2019-01-02 04:35:34 +01:00
fs.readFile('./json/autoresponse.json', 'utf8', function readFileCallback(err, data) {
if (err) {
fs.close();
console.log(err);
} else {
2018-12-30 01:20:24 +01:00
2018-11-20 19:33:04 +01:00
autoresponse = JSON.parse(data); //now it an object
2019-01-02 04:35:34 +01:00
guild.channels.forEach(channel => autoresponse[channel] = text);
2018-11-20 19:33:04 +01:00
json = JSON.stringify(autoresponse); //convert it back to json
2019-01-02 04:35:34 +01:00
json = json.replace(/[<#>]/g, '');
fs.writeFile('./json/autoresponse.json', json, 'utf8', function (err) {
if (err) {
fs.close();
2018-11-20 19:33:04 +01:00
return console.log(err);
2019-01-02 04:35:34 +01:00
}
})
}
});
2018-11-20 19:33:04 +01:00
2019-01-02 04:35:34 +01:00
fs.close();
return message.channel.send('Auto response have been disable/enable on every channel');
2018-12-30 01:20:24 +01:00
2019-01-02 04:35:34 +01:00
} else if (text == 'disable' || 'enable') {
fs.readFile('./json/autoresponse.json', 'utf8', function readFileCallback(err, data) {
if (err) {
console.log(err);
} else {
2018-12-30 01:20:24 +01:00
autoresponse = JSON.parse(data); //now it an object
2019-01-02 04:35:34 +01:00
autoresponse[message.channel.id] = text;
2018-12-30 01:20:24 +01:00
json = JSON.stringify(autoresponse); //convert it back to json
2019-01-02 04:35:34 +01:00
fs.writeFile('./json/autoresponse.json', json, 'utf8', function (err) {
if (err) {
fs.close();
2018-12-30 01:20:24 +01:00
return console.log(err);
2019-01-02 04:35:34 +01:00
}
})
}
})
};
fs.close();
return message.channel.send(`Autoresponse have been ${text}d`);
}
}
2018-12-30 01:20:24 +01:00
module.exports = autoresponseCommand;