Forráskód Böngészése

Finish /verify command

Markus-Rost 4 éve
szülő
commit
e3f664fc87
7 módosított fájl, 321 hozzáadás és 293 törlés
  1. 17 2
      bot.js
  2. 25 0
      cmds/verification.js
  3. 24 0
      dashboard/util.js
  4. 46 0
      dashboard/verification.js
  5. 4 0
      interactions/commands.json
  6. 201 287
      package-lock.json
  7. 4 4
      package.json

+ 17 - 2
bot.js

@@ -61,6 +61,21 @@ const client = new Discord.Client( {
 	}
 	}
 } );
 } );
 
 
+client.api.applications(process.env.bot).commands.get().then( response => {
+	console.log( '- ' + shardId + ': Slash commands successfully loaded.' );
+	const slashCommands = require('./interactions/commands.json');
+	response.forEach( command => {
+		var slashCommand = slashCommands.find( slashCommand => slashCommand.name === command.name );
+		if ( slashCommand ) {
+			slashCommand.id = command.id;
+			slashCommand.application_id = command.application_id;
+		}
+		else slashCommands.push(slashCommand);
+	} );
+}, error => {
+	console.log( '- ' + shardId + ': Error while getting the global slash commands: ' + error );
+} );
+
 global.pause = {};
 global.pause = {};
 var isStop = false;
 var isStop = false;
 client.on( 'ready', () => {
 client.on( 'ready', () => {
@@ -377,9 +392,9 @@ client.on( 'warn', warning => log_warn(warning, false) );
 
 
 client.login(process.env.token).catch( error => {
 client.login(process.env.token).catch( error => {
 	log_error(error, true, 'LOGIN-');
 	log_error(error, true, 'LOGIN-');
-	client.login(process.env.token).catch( error => {
+	return client.login(process.env.token).catch( error => {
 		log_error(error, true, 'LOGIN-');
 		log_error(error, true, 'LOGIN-');
-		client.login(process.env.token).catch( error => {
+		return client.login(process.env.token).catch( error => {
 			log_error(error, true, 'LOGIN-');
 			log_error(error, true, 'LOGIN-');
 			process.exit(1);
 			process.exit(1);
 		} );
 		} );

+ 25 - 0
cmds/verification.js

@@ -1,6 +1,7 @@
 const help_setup = require('../functions/helpsetup.js');
 const help_setup = require('../functions/helpsetup.js');
 const {limit: {verification: verificationLimit}} = require('../util/default.json');
 const {limit: {verification: verificationLimit}} = require('../util/default.json');
 var db = require('../util/database.js');
 var db = require('../util/database.js');
+const slashCommand = require('../interactions/commands.json').find( slashCommand => slashCommand.name === 'verify' );
 
 
 /**
 /**
  * Processes the "verification" command.
  * Processes the "verification" command.
@@ -48,6 +49,21 @@ function cmd_verification(lang, msg, args, line, wiki) {
 			}
 			}
 			return db.query( 'INSERT INTO verification(guild, configid, channel, role) VALUES($1, $2, $3, $4)', [msg.guild.id, new_configid, '|' + msg.channel.id + '|', roles] ).then( () => {
 			return db.query( 'INSERT INTO verification(guild, configid, channel, role) VALUES($1, $2, $3, $4)', [msg.guild.id, new_configid, '|' + msg.channel.id + '|', roles] ).then( () => {
 				console.log( '- Verification successfully added.' );
 				console.log( '- Verification successfully added.' );
+				if ( !rows.length && slashCommand?.id ) msg.client.api.applications(msg.client.user.id).guilds(msg.guild.id).commands(slashCommand.id).permissions.put( {
+					data: {
+						permissions: [
+							{
+								id: msg.guild.id,
+								type: 1,
+								permission: true
+							}
+						]
+					}
+				} ).then( () => {
+					console.log( '- Slash command successfully enabled.' );
+				}, error => {
+					console.log( '- Error while enabling the slash command: ' + error );
+				} );
 				msg.replyMsg( lang.get('verification.added') + formatVerification(false, false, {configid: new_configid, role: roles}), {}, true );
 				msg.replyMsg( lang.get('verification.added') + formatVerification(false, false, {configid: new_configid, role: roles}), {}, true );
 			}, dberror => {
 			}, dberror => {
 				console.log( '- Error while adding the verification: ' + dberror );
 				console.log( '- Error while adding the verification: ' + dberror );
@@ -78,6 +94,15 @@ function cmd_verification(lang, msg, args, line, wiki) {
 			if ( process.env.READONLY ) return msg.replyMsg( lang.get('general.readonly') + '\n' + process.env.invite, {}, true );
 			if ( process.env.READONLY ) return msg.replyMsg( lang.get('general.readonly') + '\n' + process.env.invite, {}, true );
 			return db.query( 'DELETE FROM verification WHERE guild = $1 AND configid = $2', [msg.guild.id, row.configid] ).then( () => {
 			return db.query( 'DELETE FROM verification WHERE guild = $1 AND configid = $2', [msg.guild.id, row.configid] ).then( () => {
 				console.log( '- Verification successfully removed.' );
 				console.log( '- Verification successfully removed.' );
+				if ( rows.length === 1 && slashCommand?.id ) msg.client.api.applications(msg.client.user.id).guilds(msg.guild.id).commands(slashCommand.id).permissions.put( {
+					data: {
+						permissions: []
+					}
+				} ).then( () => {
+					console.log( '- Slash command successfully disabled.' );
+				}, error => {
+					console.log( '- Error while disabling the slash command: ' + error );
+				} );
 				msg.replyMsg( lang.get('verification.deleted'), {}, true );
 				msg.replyMsg( lang.get('verification.deleted'), {}, true );
 			}, dberror => {
 			}, dberror => {
 				console.log( '- Error while removing the verification: ' + dberror );
 				console.log( '- Error while removing the verification: ' + dberror );

+ 24 - 0
dashboard/util.js

@@ -12,6 +12,30 @@ db.on( 'error', dberror => {
 	console.log( '- Dashboard: Error while connecting to the database: ' + dberror );
 	console.log( '- Dashboard: Error while connecting to the database: ' + dberror );
 } );
 } );
 
 
+got.get( 'https://discord.com/api/v8/applications/' + process.env.bot + '/commands', {
+	headers:{
+		Authorization: 'Bot ' + process.env.token
+	},
+	timeout: 10000
+} ).then( response=> {
+	if ( response.statusCode !== 200 || !response.body ) {
+		console.log( '- Dashboard: ' + response.statusCode + ': Error while getting the global slash commands: ' + response.body?.message );
+		return;
+	}
+	console.log( '- Dashboard: Slash commands successfully loaded.' );
+	const slashCommands = require('../interactions/commands.json');
+	response.body.forEach( command => {
+		var slashCommand = slashCommands.find( slashCommand => slashCommand.name === command.name );
+		if ( slashCommand ) {
+			slashCommand.id = command.id;
+			slashCommand.application_id = command.application_id;
+		}
+		else slashCommands.push(slashCommand);
+	} );
+}, error => {
+	console.log( '- Dashboard: Error while getting the global slash commands: ' + error );
+} );
+
 /**
 /**
  * @typedef Settings
  * @typedef Settings
  * @property {String} state
  * @property {String} state

+ 46 - 0
dashboard/verification.js

@@ -1,6 +1,7 @@
 const {limit: {verification: verificationLimit}, usergroups} = require('../util/default.json');
 const {limit: {verification: verificationLimit}, usergroups} = require('../util/default.json');
 const Lang = require('../util/i18n.js');
 const Lang = require('../util/i18n.js');
 const {got, db, sendMsg, createNotice, escapeText, hasPerm} = require('./util.js');
 const {got, db, sendMsg, createNotice, escapeText, hasPerm} = require('./util.js');
+const slashCommand = require('../interactions/commands.json').find( slashCommand => slashCommand.name === 'verify' );
 
 
 const fieldset = {
 const fieldset = {
 	channel: '<label for="wb-settings-channel">Channel:</label>'
 	channel: '<label for="wb-settings-channel">Channel:</label>'
@@ -408,6 +409,28 @@ function update_verification(res, userSettings, guild, type, settings) {
 		if ( settings.delete_settings ) return db.query( 'DELETE FROM verification WHERE guild = $1 AND configid = $2 RETURNING channel, role, editcount, postcount, usergroup, accountage, rename', [guild, type] ).then( ({rows:[row]}) => {
 		if ( settings.delete_settings ) return db.query( 'DELETE FROM verification WHERE guild = $1 AND configid = $2 RETURNING channel, role, editcount, postcount, usergroup, accountage, rename', [guild, type] ).then( ({rows:[row]}) => {
 			console.log( `- Dashboard: Verification successfully removed: ${guild}#${type}` );
 			console.log( `- Dashboard: Verification successfully removed: ${guild}#${type}` );
 			res(`/guild/${guild}/verification`, 'save');
 			res(`/guild/${guild}/verification`, 'save');
+			if ( slashCommand?.id ) db.query( 'SELECT COUNT(1) FROM verification WHERE guild = $1', [guild] ).then( ({rows:[{count}]}) => {
+				if ( count > 0 ) return;
+				got.put( 'https://discord.com/api/v8/applications/' + process.env.bot + '/guilds/' + guild + '/commands/' + slashCommand.id + '/permissions', {
+					headers:{
+						Authorization: 'Bot ' + process.env.token
+					},
+					json: {
+						permissions: []
+					},
+					timeout: 10000
+				} ).then( response=> {
+					if ( response.statusCode !== 200 || !response.body ) {
+						console.log( '- Dashboard: ' + response.statusCode + ': Error while disabling the slash command: ' + response.body?.message );
+						return;
+					}
+					console.log( '- Dashboard: Slash command successfully disabled.' );
+				}, error => {
+					console.log( '- Dashboard: Error while disabling the slash command: ' + error );
+				} );
+			}, dberror => {
+				console.log( '- Dashboard: Error while disabling the slash command: ' + dberror );
+			} );
 			if ( row ) db.query( 'SELECT lang FROM discord WHERE guild = $1 AND channel IS NULL', [guild] ).then( ({rows:[channel]}) => {
 			if ( row ) db.query( 'SELECT lang FROM discord WHERE guild = $1 AND channel IS NULL', [guild] ).then( ({rows:[channel]}) => {
 				var lang = new Lang(channel.lang);
 				var lang = new Lang(channel.lang);
 				var text = lang.get('verification.dashboard.removed', `<@${userSettings.user.id}>`, type);
 				var text = lang.get('verification.dashboard.removed', `<@${userSettings.user.id}>`, type);
@@ -485,6 +508,29 @@ function update_verification(res, userSettings, guild, type, settings) {
 				db.query( 'INSERT INTO verification(guild, configid, channel, role, editcount, postcount, usergroup, accountage, rename) VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9)', [guild, configid, '|' + settings.channel.join('|') + '|', settings.role.join('|'), settings.editcount, settings.postcount, settings.usergroup.join('|'), settings.accountage, ( settings.rename ? 1 : 0 )] ).then( () => {
 				db.query( 'INSERT INTO verification(guild, configid, channel, role, editcount, postcount, usergroup, accountage, rename) VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9)', [guild, configid, '|' + settings.channel.join('|') + '|', settings.role.join('|'), settings.editcount, settings.postcount, settings.usergroup.join('|'), settings.accountage, ( settings.rename ? 1 : 0 )] ).then( () => {
 					console.log( `- Dashboard: Verification successfully added: ${guild}#${configid}` );
 					console.log( `- Dashboard: Verification successfully added: ${guild}#${configid}` );
 					res(`/guild/${guild}/verification/${configid}`, 'save');
 					res(`/guild/${guild}/verification/${configid}`, 'save');
+					if ( !row.count.length && slashCommand?.id ) got.put( 'https://discord.com/api/v8/applications/' + process.env.bot + '/guilds/' + guild + '/commands/' + slashCommand.id + '/permissions', {
+						headers:{
+							Authorization: 'Bot ' + process.env.token
+						},
+						json: {
+							permissions: [
+								{
+									id: guild,
+									type: 1,
+									permission: true
+								}
+							]
+						},
+						timeout: 10000
+					} ).then( response=> {
+						if ( response.statusCode !== 200 || !response.body ) {
+							console.log( '- Dashboard: ' + response.statusCode + ': Error while enabling the slash command: ' + response.body?.message );
+							return;
+						}
+						console.log( '- Dashboard: Slash command successfully enabled.' );
+					}, error => {
+						console.log( '- Dashboard: Error while enabling the slash command: ' + error );
+					} );
 					var lang = new Lang(row.lang);
 					var lang = new Lang(row.lang);
 					var text = lang.get('verification.dashboard.added', `<@${userSettings.user.id}>`, configid);
 					var text = lang.get('verification.dashboard.added', `<@${userSettings.user.id}>`, configid);
 					text += '\n' + lang.get('verification.channel') + ' <#' + settings.channel.join('>, <#') + '>';
 					text += '\n' + lang.get('verification.channel') + ' <#' + settings.channel.join('>, <#') + '>';

+ 4 - 0
interactions/commands.json

@@ -1,5 +1,7 @@
 [
 [
 	{
 	{
+		"id": "",
+		"application_id": "",
 		"name": "inline",
 		"name": "inline",
 		"description": "Post a message with inline wiki links.",
 		"description": "Post a message with inline wiki links.",
 		"default_permission": true,
 		"default_permission": true,
@@ -13,6 +15,8 @@
 		]
 		]
 	},
 	},
 	{
 	{
+		"id": "",
+		"application_id": "",
 		"name": "verify",
 		"name": "verify",
 		"description": "Verify your Discord account with your wiki account.",
 		"description": "Verify your Discord account with your wiki account.",
 		"default_permission": false,
 		"default_permission": false,

A különbségek nem kerülnek megjelenítésre, a fájl túl nagy
+ 201 - 287
package-lock.json


+ 4 - 4
package.json

@@ -15,15 +15,15 @@
     "node": ">=14.0.0"
     "node": ">=14.0.0"
   },
   },
   "dependencies": {
   "dependencies": {
-    "cheerio": "^1.0.0-rc.5",
+    "cheerio": "^1.0.0-rc.6",
     "discord-oauth2": "^2.6.0",
     "discord-oauth2": "^2.6.0",
     "discord.js": "^12.5.1",
     "discord.js": "^12.5.1",
     "dotenv": "^8.2.0",
     "dotenv": "^8.2.0",
     "full-icu": "^1.3.1",
     "full-icu": "^1.3.1",
     "got": "^11.8.1",
     "got": "^11.8.1",
-    "htmlparser2": "^6.0.0",
-    "npm": "^7.5.3",
-    "pg": "^8.5.1"
+    "htmlparser2": "^6.1.0",
+    "npm": "^7.11.2",
+    "pg": "^8.6.0"
   },
   },
   "repository": {
   "repository": {
     "type": "git",
     "type": "git",

Nem az összes módosított fájl került megjelenítésre, mert túl sok fájl változott