From 63c10d758cbb59194c1a80b34ef3c846ea557f6c Mon Sep 17 00:00:00 2001 From: loicbersier Date: Mon, 24 Feb 2020 14:31:52 +0100 Subject: [PATCH] Whitelist word --- commands/admin/whitelistword.js | 68 +++++++++++++++++++ event/listeners/message.js | 8 +++ .../20200224133009-create-whitelist-word.js | 30 ++++++++ models/whitelistword.js | 11 +++ 4 files changed, 117 insertions(+) create mode 100644 commands/admin/whitelistword.js create mode 100644 migrations/20200224133009-create-whitelist-word.js create mode 100644 models/whitelistword.js diff --git a/commands/admin/whitelistword.js b/commands/admin/whitelistword.js new file mode 100644 index 00000000..83cdca42 --- /dev/null +++ b/commands/admin/whitelistword.js @@ -0,0 +1,68 @@ +const { Command } = require('discord-akairo'); +const whitelistWord = require('../../models').whitelistWord; + +class whitelistWordCommand extends Command { + constructor() { + super('whitelistWord', { + aliases: ['whitelistWord'], + category: 'admin', + userPermissions: ['MANAGE_MESSAGES'], + clientPermissions: ['MANAGE_MESSAGES', 'SEND_MESSAGES'], + args: [ + { + id: 'word', + type: 'string', + match: 'rest' + }, + { + id: 'remove', + match: 'flag', + flag: '--remove' + }, + { + id: 'removeall', + match: 'flag', + flag: '--removeall' + } + ], + channelRestriction: 'guild', + description: { + content: 'Ban word on the server. use the unbanword alias to delete a banned word, unbanword alias and --removeaall to remove every banned word', + usage: '[word to ban]', + examples: ['owo'] + } + }); + } + + async exec(message, args) { + if (!args.word) args.word = ''; + args.word = args.word.replace(/[\u0250-\ue007]/g, ''); + const WhitelistWord = await whitelistWord.findOne({where: {word: args.word.toLowerCase(), serverID: message.guild.id}}); + + if (message.util.parsed.alias == 'unbanword') { + if (args.removeall) { + whitelistWord.destroy({where: {serverID: message.guild.id}}); + return message.channel.send('The whitelisted words has been reset.'); + } + + if (WhitelistWord) { + whitelistWord.destroy({where: {word: args.word.toLowerCase(), serverID: message.guild.id}}); + return message.channel.send(`The word ${args.word.toLowerCase()} is no longer whitelisted`); + } else { + return message.channel.send('There was no word to whiteliste'); + } + } + + if (!args.word) return message.channel.send('Please specify a word to whiteliste!'); + + if (!WhitelistWord) { + const body = {word: args.word.toLowerCase(), serverID: message.guild.id}; + await whitelistWord.create(body); + return message.channel.send(`The word ${args.word.toLowerCase()} has been whitelisted`); + } else { + message.channel.send('This word is already whitelisted'); + } + } +} + +module.exports = whitelistWordCommand; \ No newline at end of file diff --git a/event/listeners/message.js b/event/listeners/message.js index 7cb7521a..dd643a25 100644 --- a/event/listeners/message.js +++ b/event/listeners/message.js @@ -5,6 +5,7 @@ const Tag = require('../../models').Tag; const autoResponse = require('../../models').autoresponse; const autoResponseStat = require('../../models').autoresponseStat; const BannedWords = require('../../models').bannedWords; +const WhitelistWord = require('../../models').whitelistWord; class messageListener extends Listener { constructor() { @@ -27,6 +28,12 @@ class messageListener extends Listener { // Banned words const bannedWords = await BannedWords.findAll({where: {word: Sequelize.where(Sequelize.fn('LOCATE', Sequelize.col('word'), message.content.replace(/\u200B/g, '').replace(/[\u0250-\ue007]/g, '')), Sequelize.Op.ne, 0), serverID: message.guild.id}}); + const whitelistWord = await WhitelistWord.findAll({where: {word: Sequelize.where(Sequelize.fn('LOCATE', Sequelize.col('word'), message.content.replace(/\u200B/g, '').replace(/[\u0250-\ue007]/g, '')), Sequelize.Op.ne, 0), serverID: message.guild.id}}); + + if (whitelistWord[0]) { + return; // If word is whitelisted just return + } + if (bannedWords[0]) { // Remove accent let censoredMessage = message.content.toLowerCase().normalize('NFD').replace(/[\u0300-\u036f]/g, ''); @@ -46,6 +53,7 @@ class messageListener extends Listener { message.channel.send(Embed); return message.delete({reason: `Deleted message: ${message.content}`}); + } else { // auto responses const autoresponseStat = await autoResponseStat.findOne({where: {serverID: message.guild.id}}); diff --git a/migrations/20200224133009-create-whitelist-word.js b/migrations/20200224133009-create-whitelist-word.js new file mode 100644 index 00000000..bc44d8ab --- /dev/null +++ b/migrations/20200224133009-create-whitelist-word.js @@ -0,0 +1,30 @@ +'use strict'; +module.exports = { + up: (queryInterface, Sequelize) => { + return queryInterface.createTable('whitelistWords', { + id: { + allowNull: false, + autoIncrement: true, + primaryKey: true, + type: Sequelize.INTEGER + }, + word: { + type: Sequelize.STRING + }, + serverID: { + type: Sequelize.BIGINT + }, + createdAt: { + allowNull: false, + type: Sequelize.DATE + }, + updatedAt: { + allowNull: false, + type: Sequelize.DATE + } + }); + }, + down: (queryInterface, Sequelize) => { + return queryInterface.dropTable('whitelistWords'); + } +}; \ No newline at end of file diff --git a/models/whitelistword.js b/models/whitelistword.js new file mode 100644 index 00000000..aa9212f8 --- /dev/null +++ b/models/whitelistword.js @@ -0,0 +1,11 @@ +'use strict'; +module.exports = (sequelize, DataTypes) => { + const whitelistWord = sequelize.define('whitelistWord', { + word: DataTypes.STRING, + serverID: DataTypes.BIGINT + }, {}); + whitelistWord.associate = function(models) { + // associations can be defined here + }; + return whitelistWord; +}; \ No newline at end of file