Tobias Hopp e76ad758b8 Update bot
Took 12 minutes
2021-03-14 12:05:09 +01:00

320 lines
10 KiB
JavaScript

// Required Things
const Discord = require ( "discord.js" );
const config = require ( "./config.json" );
const AntiSpam = require ( "discord-anti-spam" );
const mySQL = require( "mysql" );
// new Discord Client
const client = new Discord.Client ( {partials: [ 'MESSAGE', 'CHANNEL' ], disableMentions: "none"} );
const antiSpam = new AntiSpam ( {
warnThreshold: 4, // Amount of messages sent in a row that will cause a warning.
muteThreshold: 6, // Amount of messages sent in a row that will cause a mute
kickThreshold: 10, // Amount of messages sent in a row that will cause a kick.
banThreshold: 15, // Amount of messages sent in a row that will cause a ban.
maxInterval: 2300, // Amount of time (in milliseconds) in which messages are considered spam.
warnMessage: '{@user}, du hast eine Verwarnung für Spaming erhalten.', // Message that will be sent in chat upon warning a user.
kickMessage: '**{user_tag}** wurde wegen Spaming von unserem Server gekickt.', // Message that will be sent in chat upon kicking a user.
muteMessage: '**{user_tag}** wurde wegen Spaming temporär stumm geschaltet.',// Message that will be sent in chat upon muting a user.
banMessage: '**{user_tag}** wurde wegen Spaming von unserer Community ausgeschlossen.', // Message that will be sent in chat upon banning a user.
maxDuplicatesWarning: 5, // Amount of duplicate messages that trigger a warning.
maxDuplicatesKick: 10, // Amount of duplicate messages that trigger a warning.
maxDuplicatesBan: 15, // Amount of duplicate messages that trigger a warning.
exemptPermissions: [ 'ADMINISTRATOR' ], // Bypass users with any of these permissions.
ignoreBots: true, // Ignore bot messages.
verbose: true, // Extended Logs from module.
ignoredUsers: [], // Array of User IDs that get ignored.
muteRoleName: "Muted", // Name of the role that will be given to muted users!
removeMessages: true // If the bot should remove all the spam messages when taking action on a user!
// And many more options... See the documentation.
} );
const con = mySQL.createConnection ( {
host: config.mysqlHost,
user: config.mysqlUsername,
password: config.mysqlPassword
} );
let badWords = {}; // 0 => {1:2,}
con.connect(function(err) {
if (err) {
console.log ( "[ERROR] Cannot connect to database! Critical error" );
process.exit();
return;
}
console.log("[INFO] Connected to mySQL Server!");
});
client.on ( "ready", () =>
{
console.log ( "[INFO] Bot is now ready" );
client.user.setPresence ( {
activity:
{
name: "Reading your lovely messages",
type: 'PLAYING'
}
} ).then ( r =>
{
console.log ( "[INFO] Presence set" );
} );
} );
client.on ( "message", async ( message ) =>
{
if ( message.author.bot ) return;
// Beleidigungsfilter?
if ( !message.content.startsWith ( config.prefix ) )
{
// Check if the user has Admin permissions, so skip him
if ( message.member.hasPermission ( "ADMINISTRATOR" ) ) return;
// Filter goes here
if ( checkMessage ( message.guild.id, message.content ) )
{
await message.delete ();
let notifyChannelID = getNotifyChannel ( message.guild.id );
if ( notifyChannelID.length === 0 || notifyChannelID === 0 )
{
// NotifyChannel not send, so we gonna send the msg here
const notify = new Discord.MessageEmbed ()
.setColor ( "#b30202" )
.setTitle ( "Nachricht gelöscht" )
.setDescription ( `Es wurde eine Nachricht von ${ message.author.toString () } entfernt, da ein dort enthaltenes Wort auf der Blacklist steht.` )
.addField ( 'Warnung', `Mir wurde noch nicht gesagt, wo ich Nachricht wie diese hinschreiben soll.\nDu kannst dies als Administrator mit ".w setNotify" in einem beliebigen Channel setzen.` );
message.channel.send ( notify ).then ( () =>
{
} ).catch ( () =>
{
console.log ( "[ERROR] Cannot send a message on guild " + message.guild.id + " in channel " + message.channel.id );
} );
} else
{
const notifyChannel = message.member.guild.channels.cache.find ( ch => ch.id === notifyChannelID );
const notify = new Discord.MessageEmbed ()
.setColor ( "#b30202" )
.setTitle ( "Nachricht gelöscht" )
.setDescription ( `Es wurde eine Nachricht von ${ message.author.toString () } entfernt, da ein dort enthaltenes Wort auf der Blacklist steht.` );
notifyChannel.send ( notify ).then ( () =>
{
} ).catch ( () =>
{
console.log ( "[ERROR] Cannot send a message on guild " + message.guild.id + " in channel " + message.channel.id );
} );
}
}
return;
}
// Commands here
if ( !message.member.hasPermission ( "ADMINISTRATOR" ) )
{
await message.reply ( "mir wurde beigebracht, dass ich mit fremden Leuten nicht reden darf :c" );
return;
}
const commandBody = message.content.slice ( config.prefix.length );
const args = commandBody.split ( ' ' );
const command = args.shift ().toLowerCase ();
if ( command === "ping" )
{
const timeTaken = Date.now () - message.createdTimestamp;
message.reply ( `Der Ping von mir liegt bei ${ timeTaken }ms.` ).then ( () => {} );
}
if ( command === "list" )
{
// Listet alle Wörter des Servers mit indexen auf
let words = listWords( message.guild.id );
let output = "";
for ( let wordsKey in words )
{
output += `${words[wordsKey]["id"]} | ${words[wordsKey]["word"]}\n`;
}
await message.channel.send ( `Folgende Wörter & Sätze stehen auf der Schwarzen Liste:\nIndex\tWort\n` + output );
}
if ( command === "remove" )
{
if ( args.length >= 2 )
{
const index = Number.parseInt ( args[1] );
if ( !isNaN ( index ) )
{
} else
{
await message.reply ( `bitte gebe einen Index mit an. ${ config.prefix }delete <index>` )
}
} else
{
await message.reply ( `bitte gebe einen Index mit an. ${ config.prefix }delete <index>` )
}
}
if ( command === "add" )
{
if ( args.length >= 2 )
{
addWord ( message.guild.id, args[1] );
} else
{
await message.reply ( `bitte gebe ein Wort/Satz mit an. ${ config.prefix }add <Wort/Satz>` );
}
}
} );
client.on ( "guildCreate", ( guild ) =>
{
console.log ( `[INFO] Joined a new guild named "${ guild.name }" with ID ${ guild.id }` );
createServerEntry ( guild.id, guild.name );
} );
function createServerEntry( a_ServerID, a_ServerName )
{
console.log ( `[INFO] Creating server entry...` );
let query = "INSERT INTO guilds ( guild_id, server_name ) VALUES ( ?, ? )";
con.query( query, [ a_ServerID, a_ServerName ], function( error, results, fields )
{
if( error )
{
console.log ( `[ERROR] An error occurred while creating a new guild in database` );
return false;
}
return true;
} );
}
function checkMessage( a_ServerID, a_Message )
{
/*let query = "SELECT word FROM words WHERE word LIKE ?";
con.query( query, ['%' + a_Message + '%'], function( error, results, fields ) {
if( error )
{
console.log ( `[ERROR] An error occurred while fetching th` )
}
} );*/
if( badWords[a_ServerID].contains( a_Message ) )
{
return true;
}
return false;
}
function refreshWords( a_ServerID )
{
// Add the word to the database
let query = "SELECT id, word FROM words WHERE guildID = ?";
con.query( query, [ a_ServerID ], function( error, results, fields ) {
if( error )
{
console.log ( `[ERROR] An error occurred while getting filter for guild ${a_ServerID}` );
return false;
}
for (let i = 0; i < results.length; i++ ) {
badWords[a_ServerID][results[i].id] = results[i].word;
}
return true;
} );
return false;
}
function loadAllUp()
{
console.log( "[INFO] Loading all up..." );
// LOADING ALL UP, can take a while
// Add the word to the database
let query = "SELECT id, guild_id FROM guilds";
con.query( query, function( error, results, fields ) {
if( error )
{
console.log ( `[ERROR] An error occurred while loading all up` );
return false;
}
for (let i = 0; i < results.length; i++) {
refreshWords( results[i].guild_id );
}
return true;
} );
return false;
}
function getNotifyChannel( a_ServerID )
{
let query = "SELECT notify_channel FROM guilds WHERE guilds.guild_id = ?";
con.query( query, [ a_ServerID ], function( error, results, fields ) {
if( error )
{
console.log ( `[ERROR] An error occurred while fetching the notify channel` )
}
return results.notify_channel;
} );
return 0;
}
function deleteWord( a_ServerID, a_Index )
{
// Delete the word out of the database
// Add the word to the database
let query = "DELETE FROM words WHERE id = ? AND guildID = ?";
con.query( query, [ a_Index, a_ServerID ], function( error, results, fields ) {
if( error )
{
console.log ( `[ERROR] An error occurred while inserting a new filter entry` );
return false;
}
return true;
} );
return false;
}
function addWord( a_ServerID, a_Word )
{
// Add the word to the database
let query = "INSERT INTO words ( word, guildID ) VALUES ( ?, ? )";
con.query( query, [ a_Word, a_ServerID ], function( error, results, fields ) {
if( error )
{
console.log ( `[ERROR] An error occurred while inserting a new filter entry` );
return false;
}
return true;
} );
return false;
}
function listWords( a_ServerID )
{
return {0:{id:1,word:"huan"},1:{id:2,word:"sinnex"}};
}
// Discord Login
client.login ( config.TOKEN ).then ( r =>
{
console.log ( "[INFO] Logged in as Woam" )
} );