From 320ac3a3f0965787ac7bb1904963eec1e3d94692 Mon Sep 17 00:00:00 2001 From: loicbersier Date: Thu, 30 Apr 2020 22:34:54 +0200 Subject: [PATCH] Some channel logging (W.I.P) Signed-off-by: loicbersier --- commands/admin/log.js | 50 +++++++++++++++++++ event/listeners/log/channelCreate.js | 27 ++++++++++ event/listeners/log/channelDelete.js | 27 ++++++++++ event/listeners/log/channelUpdate.js | 50 +++++++++++++++++++ migrations/20200430005935-create-log-stats.js | 30 +++++++++++ models/logstats.js | 11 ++++ 6 files changed, 195 insertions(+) create mode 100644 commands/admin/log.js create mode 100644 event/listeners/log/channelCreate.js create mode 100644 event/listeners/log/channelDelete.js create mode 100644 event/listeners/log/channelUpdate.js create mode 100644 migrations/20200430005935-create-log-stats.js create mode 100644 models/logstats.js diff --git a/commands/admin/log.js b/commands/admin/log.js new file mode 100644 index 00000000..dc4ff1d7 --- /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 00000000..0140ca26 --- /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 00000000..c32cd413 --- /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 00000000..014f268a --- /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 00000000..69bfacb5 --- /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 00000000..eba9ca85 --- /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