From 2e392da2f6ecd3d3125645401af88155af7064f0 Mon Sep 17 00:00:00 2001 From: loicbersier Date: Wed, 21 Aug 2019 19:58:51 +0200 Subject: [PATCH] banning word system --- commands/admin/banword.js | 50 +++++++++++++++++++ event/listeners/message.js | 24 ++++++++- .../20190821164402-create-banned-words.js | 30 +++++++++++ models/bannedwords.js | 11 ++++ 4 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 commands/admin/banword.js create mode 100644 migrations/20190821164402-create-banned-words.js create mode 100644 models/bannedwords.js diff --git a/commands/admin/banword.js b/commands/admin/banword.js new file mode 100644 index 00000000..471cb1b0 --- /dev/null +++ b/commands/admin/banword.js @@ -0,0 +1,50 @@ +const { Command } = require('discord-akairo'); +const BannedWords = require('../../models').bannedWords; + +class BannedWordsCommand extends Command { + constructor() { + super('BannedWords', { + aliases: ['bannedword', 'banword'], + category: 'admin', + userPermissions: ['MANAGE_MESSAGES'], + args: [ + { + id: 'word', + type: 'string', + prompt: { + start: 'What word should be banned', + }, + match: 'rest' + }, + { + id: 'remove', + match: 'flag', + flag: '--remove' + } + ], + channelRestriction: 'guild', + description: { + content: 'Ban word on the server. --remove to delete a banned word', + usage: '[word to ban]', + examples: ['owo'] + } + }); + } + + async exec(message, args) { + const bannedWords = await BannedWords.findOne({where: {word: args.word, serverID: message.guild.id}}); + + if (!bannedWords) { + const body = {word: args.word, serverID: message.guild.id}; + await BannedWords.create(body); + return message.channel.send(`The word ${args.word} have been banned`); + } else if (args.remove && bannedWords) { + BannedWords.destroy({where: {word: args.word, serverID: message.guild.id}}); + return message.channel.send(`The word ${args.word} is no longer banned`); + } else { + message.channel.send('This word is already banned'); + } + } +} + +module.exports = BannedWordsCommand; \ No newline at end of file diff --git a/event/listeners/message.js b/event/listeners/message.js index 0896017d..d1d1c16d 100644 --- a/event/listeners/message.js +++ b/event/listeners/message.js @@ -1,9 +1,11 @@ const { Listener } = require('discord-akairo'); const { MessageEmbed } = require('discord.js'); const rand = require('../../rand.js'); +const Sequelize = require('sequelize'); const Tag = require('../../models').Tag; const autoResponse = require('../../models').autoresponse; const autoResponseStat = require('../../models').autoresponseStat; +const BannedWords = require('../../models').bannedWords; class messageListener extends Listener { constructor() { @@ -15,8 +17,28 @@ class messageListener extends Listener { async exec(message) { if (message.author.bot) return; - const autoresponseStat = await autoResponseStat.findOne({where: {serverID: message.guild.id}}); + + // Banned words + + const bannedWords = await BannedWords.findAll({where: {word: Sequelize.where(Sequelize.fn('LOCATE', Sequelize.col('word'), message.content), Sequelize.Op.ne, 0), serverID: message.guild.id}}); + if (bannedWords[0].get('word')) { + let censoredMessage = message.content; + for (let i = 0; i < bannedWords.length; i++) { + censoredMessage = censoredMessage.replace(bannedWords[i].get('word'), '█'.repeat(bannedWords[i].get('word').length)); + console.log(censoredMessage); + } + let Embed = new MessageEmbed() + .setColor('#FF0000') + .setAuthor(message.author.username, message.author.displayAvatarURL()) + .setDescription(censoredMessage); + + message.channel.send(Embed); + return message.delete(); + } + + // auto responses + const autoresponseStat = await autoResponseStat.findOne({where: {serverID: message.guild.id}}); if (autoresponseStat) { // Infinit haha very yes if (message.content.toLowerCase().startsWith('haha very') && message.content.toLowerCase().endsWith('yes')) { diff --git a/migrations/20190821164402-create-banned-words.js b/migrations/20190821164402-create-banned-words.js new file mode 100644 index 00000000..bc2da4e0 --- /dev/null +++ b/migrations/20190821164402-create-banned-words.js @@ -0,0 +1,30 @@ +'use strict'; +module.exports = { + up: (queryInterface, Sequelize) => { + return queryInterface.createTable('bannedWords', { + 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('bannedWords'); + } +}; \ No newline at end of file diff --git a/models/bannedwords.js b/models/bannedwords.js new file mode 100644 index 00000000..077b5063 --- /dev/null +++ b/models/bannedwords.js @@ -0,0 +1,11 @@ +'use strict'; +module.exports = (sequelize, DataTypes) => { + const bannedWords = sequelize.define('bannedWords', { + word: DataTypes.STRING, + serverID: DataTypes.BIGINT + }, {}); + bannedWords.associate = function(models) { + // associations can be defined here + }; + return bannedWords; +}; \ No newline at end of file