diff --git a/commands/admin/log.js b/commands/admin/log.js new file mode 100644 index 0000000..dc4ff1d --- /dev/null +++ b/commands/admin/log.js @@ -0,0 +1,50 @@ +const { Command } = require('discord-akairo'); +const LogStats = require('../../models').LogStats; + +class logCommand extends Command { + constructor() { + super('log', { + aliases: ['log', 'logging'], + category: 'admin', + userPermissions: ['MANAGE_MESSAGES'], + channel: 'guild', + description: { + content: 'Setup logging in current channel (W.I.P)', + usage: '', + examples: [''] + } + }); + } + + async exec(message, args) { + const logStats = await LogStats.findOne({where: {guild: message.guild.id}}); + const ownerID = this.client.ownerID; + + if (!logStats) { + const body = {guild: message.guild.id, channel: message.channel.id}; + await LogStats.create(body); + return message.channel.send('Logging has been enabled on this channel'); + } else if (logStats.get('ownerID') == message.author.id || message.member.hasPermission('ADMINISTRATOR') || message.author.id == ownerID) { + message.channel.send('The log channel is already setup, do you want to delete 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.toLowerCase()); + if (messageContent == 'y' || messageContent == 'yes') { + await LogStats.destroy({where: {guild: message.guild.id}}); + return message.channel.send('Log channel has been disabled!'); + } else { + return message.channel.send('Not updating.'); + } + }) + .catch(err => { + console.error(err); + return message.channel.send('Took too long to answer. didin\'t change anything.'); + }); + } else { + return message.channel.send(`You are not the owner of this tag, if you think it is problematic ask an admin to remove it by doing ${this.client.commandHandler.prefix[0]}tag ${args.trigger} --remove`); + } + } +} + +module.exports = logCommand; \ No newline at end of file diff --git a/event/listeners/log/channelCreate.js b/event/listeners/log/channelCreate.js new file mode 100644 index 0000000..0140ca2 --- /dev/null +++ b/event/listeners/log/channelCreate.js @@ -0,0 +1,27 @@ +const { Listener } = require('discord-akairo'); +const LogStats = require('../../../models/').LogStats; + +class channelCreateListener extends Listener { + constructor() { + super('channelCreate', { + emitter: 'client', + event: 'channelCreate' + }); + } + + async exec(GuildChannel) { + const logStats = await LogStats.findOne({where: {guild: GuildChannel.guild.id}}); + if (logStats) { + const channel = this.client.channels.resolve(await logStats.get('channel')); + let Embed = this.client.util.embed() + .setColor('NAVY') + .setTitle('Channel created!') + .setDescription(`${GuildChannel.type} channel ${GuildChannel} got created!`) + .setFooter(`Channel ID: ${GuildChannel.id}`) + .setTimestamp(); + + channel.send(Embed); + } + } +} +module.exports = channelCreateListener; \ No newline at end of file diff --git a/event/listeners/log/channelDelete.js b/event/listeners/log/channelDelete.js new file mode 100644 index 0000000..c32cd41 --- /dev/null +++ b/event/listeners/log/channelDelete.js @@ -0,0 +1,27 @@ +const { Listener } = require('discord-akairo'); +const LogStats = require('../../../models/').LogStats; + +class channelDeleteListener extends Listener { + constructor() { + super('channelDelete', { + emitter: 'client', + event: 'channelDelete' + }); + } + + async exec(GuildChannel) { + const logStats = await LogStats.findOne({where: {guild: GuildChannel.guild.id}}); + if (logStats) { + console.log(GuildChannel); + const channel = this.client.channels.resolve(await logStats.get('channel')); + let Embed = this.client.util.embed() + .setColor('NAVY') + .setTitle('Channel created!') + .setDescription(`${GuildChannel.type} channel ${GuildChannel.name} got deleted!`) + .setTimestamp(); + + channel.send(Embed); + } + } +} +module.exports = channelDeleteListener; \ No newline at end of file diff --git a/event/listeners/log/channelUpdate.js b/event/listeners/log/channelUpdate.js new file mode 100644 index 0000000..014f268 --- /dev/null +++ b/event/listeners/log/channelUpdate.js @@ -0,0 +1,50 @@ +const { Listener } = require('discord-akairo'); +const LogStats = require('../../../models/').LogStats; + +class channelUpdateListener extends Listener { + constructor() { + super('channelUpdate', { + emitter: 'client', + event: 'channelUpdate' + }); + } + + async exec(oldChannel, newChannel) { + const logStats = await LogStats.findOne({where: {guild: newChannel.guild.id}}); + + if (logStats) { + const channel = this.client.channels.resolve(await logStats.get('channel')); + let Embed = this.client.util.embed() + .setColor('NAVY') + .setTitle(`${newChannel.type} channel updated!`) + .setTimestamp(); + + if (oldChannel.name !== newChannel.name) { + Embed.addField('​', '​') + .addField('Previous channel', oldChannel.name, true) + .addField('New channel', newChannel.name, true); + } + + if (oldChannel.topic !== newChannel.topic) { + Embed.addField('​', '​') + .addField('Previous channel topic', oldChannel.topic, true) + .addField('New channel topic', newChannel.topic, true); + } + + if (oldChannel.nsfw !== newChannel.nsfw) { + Embed.addField('​', '​') + .addField('Previous channel nsfw', oldChannel.nsfw, true) + .addField('New channel nsfw', newChannel.nsfw, true); + } + + if (oldChannel.rateLimitPerUser !== newChannel.rateLimitPerUser) { + Embed.addField('​', '​') + .addField('Previous channel slowmode', `${oldChannel.rateLimitPerUser} seconds`, true) + .addField('New channel slowmode', `${newChannel.rateLimitPerUser} seconds`, true); + } + + channel.send(Embed); + } + } +} +module.exports = channelUpdateListener; \ No newline at end of file diff --git a/migrations/20200430005935-create-log-stats.js b/migrations/20200430005935-create-log-stats.js new file mode 100644 index 0000000..69bfacb --- /dev/null +++ b/migrations/20200430005935-create-log-stats.js @@ -0,0 +1,30 @@ +'use strict'; +module.exports = { + up: (queryInterface, Sequelize) => { + return queryInterface.createTable('LogStats', { + id: { + allowNull: false, + autoIncrement: true, + primaryKey: true, + type: Sequelize.INTEGER + }, + guild: { + type: Sequelize.BIGINT + }, + channel: { + type: Sequelize.BIGINT + }, + createdAt: { + allowNull: false, + type: Sequelize.DATE + }, + updatedAt: { + allowNull: false, + type: Sequelize.DATE + } + }); + }, + down: (queryInterface, Sequelize) => { + return queryInterface.dropTable('LogStats'); + } +}; \ No newline at end of file diff --git a/models/logstats.js b/models/logstats.js new file mode 100644 index 0000000..eba9ca8 --- /dev/null +++ b/models/logstats.js @@ -0,0 +1,11 @@ +'use strict'; +module.exports = (sequelize, DataTypes) => { + const LogStats = sequelize.define('LogStats', { + guild: DataTypes.BIGINT, + channel: DataTypes.BIGINT + }, {}); + LogStats.associate = function(models) { + // associations can be defined here + }; + return LogStats; +}; \ No newline at end of file