Update bot

Took 12 minutes
This commit is contained in:
Tobias Hopp 2021-03-14 12:05:09 +01:00
parent 21e364ee38
commit e76ad758b8
3 changed files with 61 additions and 8 deletions

3
.idea/dataSources.xml generated
View File

@ -5,8 +5,7 @@
<driver-ref>mariadb</driver-ref> <driver-ref>mariadb</driver-ref>
<synchronize>true</synchronize> <synchronize>true</synchronize>
<jdbc-driver>org.mariadb.jdbc.Driver</jdbc-driver> <jdbc-driver>org.mariadb.jdbc.Driver</jdbc-driver>
<jdbc-url>jdbc:mariadb://localhost:3306/c1woam_bot</jdbc-url> <jdbc-url>jdbc:mariadb://localhost:3307/c1woam_bot</jdbc-url>
<working-dir>$ProjectFileDir$</working-dir>
</data-source> </data-source>
</component> </component>
</project> </project>

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<dataSource name="c1woam_bot@localhost"> <dataSource name="c1woam_bot@localhost">
<database-model serializer="dbm" dbms="MARIADB" family-id="MARIADB" format-version="4.20"> <database-model serializer="dbm" dbms="MARIADB" family-id="MARIADB" format-version="4.19">
<root id="1"> <root id="1">
<ServerVersion>10.5.9</ServerVersion> <ServerVersion>10.5.9</ServerVersion>
<DefaultEngine>InnoDB</DefaultEngine> <DefaultEngine>InnoDB</DefaultEngine>
@ -1021,7 +1021,7 @@
</collation> </collation>
<user id="326" parent="1" name="c1woam_bot"> <user id="326" parent="1" name="c1woam_bot">
<Host>localhost</Host> <Host>localhost</Host>
<ObjectGrants>ALL PRIVILEGES|c1woam_bot <ObjectGrants>ALL PRIVILEGES|c1woam_bot:null
</ObjectGrants> </ObjectGrants>
</user> </user>
<table id="327" parent="2" name="guilds"> <table id="327" parent="2" name="guilds">

View File

@ -36,7 +36,7 @@ const con = mySQL.createConnection ( {
password: config.mysqlPassword password: config.mysqlPassword
} ); } );
let badWords = {"0":{}}; let badWords = {}; // 0 => {1:2,}
con.connect(function(err) { con.connect(function(err) {
if (err) { if (err) {
@ -75,7 +75,7 @@ client.on ( "message", async ( message ) =>
// Filter goes here // Filter goes here
if ( checkMessage ( message.content ) ) if ( checkMessage ( message.guild.id, message.content ) )
{ {
await message.delete (); await message.delete ();
let notifyChannelID = getNotifyChannel ( message.guild.id ); let notifyChannelID = getNotifyChannel ( message.guild.id );
@ -204,7 +204,7 @@ function createServerEntry( a_ServerID, a_ServerName )
} }
function checkMessage( a_Message ) function checkMessage( a_ServerID, a_Message )
{ {
/*let query = "SELECT word FROM words WHERE word LIKE ?"; /*let query = "SELECT word FROM words WHERE word LIKE ?";
con.query( query, ['%' + a_Message + '%'], function( error, results, fields ) { con.query( query, ['%' + a_Message + '%'], function( error, results, fields ) {
@ -213,6 +213,49 @@ function checkMessage( a_Message )
console.log ( `[ERROR] An error occurred while fetching th` ) 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; return false;
} }
@ -229,9 +272,20 @@ function getNotifyChannel( a_ServerID )
return 0; return 0;
} }
function deleteWord( a_Index ) function deleteWord( a_ServerID, a_Index )
{ {
// Delete the word out of the database // 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 ) function addWord( a_ServerID, a_Word )