Преглед изворни кода

Added functionality to convert tagged userIds in message content

Owen Diffey пре 5 година
родитељ
комит
1d56766807
4 измењених фајлова са 28 додато и 12 уклоњено
  1. 1 1
      README.md
  2. 1 1
      config.json.example
  3. 24 8
      index.js
  4. 2 2
      package.json

+ 1 - 1
README.md

@@ -14,7 +14,7 @@ I made this realy quick for a friend, so don't expect much.
   1. First make shure that you have the latest version of [node js](https://nodejs.org/en/) installed.
   2. Run this command in a command prompt to install all the libraries required to run the code: ``` npm i mysql discord.js ``` .
   3. Copy the example and open the config file and add your bot key.
-  4. Next put all the required setting about wich channel would you like to be checked (Put the ID that can be obtained by right clicking on the channel name on the channel list), and all the information reguarding the database you'd like the information to be sent. Note that you can put as much channels and server as you want as the 'servers' parameter is an array. Also here you are able to choose how the name of the user is save in the database with the setting 'userNameSetting'. This is for each server individualy and my default it's setted to the tag (username + discriminator), if you set it to 0 you get the id, if you set it 1 it sends the username only, anything else goes to default value.
+  4. Next put all the required setting about wich channel would you like to be checked (Put the ID that can be obtained by right clicking on the channel name on the channel list), and all the information reguarding the database you'd like the information to be sent. Note that you can put as much channels and server as you want as the 'servers' parameter is an array. Also here you are able to choose how the name of the user is save in the database with the setting 'mentionsMode'. This is for each server individualy and my default it's setted to the tag (username + discriminator), if you set it to 0 you get the id, if you set it 1 it sends the username only, anything else goes to default value.
   5. Save and with the help of the [developer portal on the discord website](https://discordapp.com/developers) make your bot join the server in wich the channel to check is present.
   6. Setup the database table with the following query, replacing "TABLE_NAME" as appropriate.
   ```

+ 1 - 1
config.json.example

@@ -3,7 +3,7 @@
 	"servers": [
 		{
 			"channel": "DISCORD_CHANNEL",
-			"userNameSetting": "AUTHOR_SAVE_SETTING",
+			"mentionsMode": "2",
 			"dbHost": "MYSQL_HOST",
 			"dbUser": "MYSQL_USER",
 			"dbPassword": "MYSQL_PASSWORD",

+ 24 - 8
index.js

@@ -41,16 +41,32 @@ client.on('message', message => {
 		password : serverData.dbPassword,
 		database : serverData.db
 	});
-	
-	if (serverData.userNameSetting == 0) {
-		var author = message.author.id;
-	} else if (serverData.userNameSetting == 1) {
-		var author = message.author.username;
-	} else {
-		var author = message.author.tag;
+
+	var mentions = [];
+	message.mentions.users.forEach(user => {
+		mentions.push({"userId": user.id, "username": user.username, "discriminator": user.discriminator});
+	});
+	var messageContent = message.content;
+	for (mention of mentions) {
+		if(messageContent.includes(mention.userId)) {
+			if (serverData.mentionsMode == 0) {
+				var author = message.author.id;
+				messageContent = messageContent.replace(new RegExp("<@"+mention.userId+">", 'g'), "@"+mention.userId);
+				messageContent = messageContent.replace(new RegExp("<@!"+mention.userId+">", 'g'), "@"+mention.userId);
+			} else if (serverData.mentionsMode == 1) {
+				var author = message.author.username;
+				messageContent = messageContent.replace(new RegExp("<@"+mention.userId+">", 'g'), "@"+mention.username);
+				messageContent = messageContent.replace(new RegExp("<@!"+mention.userId+">", 'g'), "@"+mention.username);
+			} else {
+				var author = message.author.tag;
+				messageContent = messageContent.replace(new RegExp("<@"+mention.userId+">", 'g'), "@"+mention.username+"#"+mention.discriminator);
+				messageContent = messageContent.replace(new RegExp("<@!"+mention.userId+">", 'g'), "@"+mention.username+"#"+mention.discriminator);
+			}
+			
+		}
 	}
 
-	var post = {message:message.content, id:message.id, time:message.createdTimestamp, user:author, links:JSON.stringify(l), images:JSON.stringify(i)};
+	var post = {message:messageContent, id:message.id, time:message.createdTimestamp, user:author, links:JSON.stringify(l), images:JSON.stringify(i)};
 	var sql = 'INSERT INTO '+serverData.dbTable+' SET ?';
 	connection.connect();
 

+ 2 - 2
package.json

@@ -1,6 +1,6 @@
 {
-  "name": "",
-  "version": "",
+  "name": "SiteLink",
+  "version": "0.0.1",
   "dependencies": {
     "discord.js": "^11.5.1",
     "mysql": "^2.17.1",