Kaynağa Gözat

Add some buttons

Markus-Rost 4 yıl önce
ebeveyn
işleme
7a66ddc52c
8 değiştirilmiş dosya ile 197 ekleme ve 24 silme
  1. 48 7
      bot.js
  2. 1 1
      cmds/rcscript.js
  3. 1 1
      cmds/settings.js
  4. 1 1
      cmds/verification.js
  5. 27 11
      cmds/verify.js
  6. 11 0
      functions/verify.js
  7. 1 0
      i18n/en.json
  8. 107 3
      interactions/verify.js

+ 48 - 7
bot.js

@@ -211,20 +211,33 @@ String.prototype.hasPrefix = function(prefix, flags = '') {
 
 const fs = require('fs');
 var slash = {};
+var buttons = {};
 fs.readdir( './interactions', (error, files) => {
 	if ( error ) return error;
 	files.filter( file => file.endsWith('.js') ).forEach( file => {
 		var command = require('./interactions/' + file);
-		slash[command.name] = command.run;
+		if ( command.hasOwnProperty('run') ) slash[command.name] = command.run;
+		if ( command.hasOwnProperty('button') ) buttons[command.name] = command.button;
 	} );
 } );
+
+client.ws.on( 'INTERACTION_CREATE', interaction => {
+	if ( interaction.version !== 1 ) return;
+	interaction.client = client;
+	if ( interaction.guild_id ) {
+		interaction.user = interaction.member.user;
+		interaction.member.permissions = new Discord.Permissions(+interaction.member.permissions);
+	}
+	if ( interaction.type === 2 ) return slash_command(interaction);
+	if ( interaction.type === 3 ) return message_button(interaction);
+} );
+
 /*
 !test eval msg.client.api.applications(msg.client.user.id).commands.post( {
 	data: require('../interactions/commands.json')[0]
 } )
 */
-client.ws.on( 'INTERACTION_CREATE', interaction => {
-	if ( interaction.version !== 1 || interaction.type !== 2 ) return;
+function slash_command(interaction) {
 	if ( !slash.hasOwnProperty(interaction.data.name) ) {
 		console.log( '- Slash: Unknown command: ' + ( isDebug ? JSON.stringify(interaction, null, '\t') : interaction.data.name ) );
 		return client.api.interactions(interaction.id, interaction.token).callback.post( {
@@ -240,13 +253,10 @@ client.ws.on( 'INTERACTION_CREATE', interaction => {
 			}
 		} ).catch(log_error);
 	}
-	interaction.client = client;
 	var channel = client.channels.cache.get(interaction.channel_id);
 	if ( !interaction.guild_id ) {
 		return slash[interaction.data.name](interaction, new Lang(), new Wiki(), channel);
 	}
-	interaction.user = interaction.member.user;
-	interaction.member.permissions = new Discord.Permissions(+interaction.member.permissions);
 	db.query( 'SELECT wiki, lang FROM discord WHERE guild = $1 AND (channel = $2 OR channel = $3 OR channel IS NULL) ORDER BY channel DESC NULLS LAST LIMIT 1', [interaction.guild_id, interaction.channel_id, '#' + channel?.parentID] ).then( ({rows:[row]}) => {
 		return slash[interaction.data.name](interaction, new Lang(( row?.lang || channel?.guild?.preferredLocale )), new Wiki(row?.wiki), channel);
 	}, dberror => {
@@ -264,7 +274,38 @@ client.ws.on( 'INTERACTION_CREATE', interaction => {
 			}
 		} ).catch(log_error);
 	} );
-} );
+}
+
+function message_button(interaction) {
+	if ( interaction.data.component_type === 2 && interaction.data.custom_id === 'verify_again' && interaction.guild_id ) {
+		if ( !interaction?.message?.embeds?.[0]?.title ) {
+			interaction.message.allowed_mentions = {
+				users: [interaction.user.id]
+			};
+			interaction.message.components = [];
+			return client.api.interactions(interaction.id, interaction.token).callback.post( {
+				data: {
+					type: 7,
+					data: interaction.message
+				}
+			} ).catch(log_error);
+		}
+		if ( !interaction.message.content.startsWith( '<@' + ( interaction.member?.nick ? '!' : '' ) + interaction.user.id + '>, ' ) ) {
+			return client.api.interactions(interaction.id, interaction.token).callback.post( {
+				data: {type: 6}
+			} ).catch(log_error);
+		}
+		var channel = client.channels.cache.get(interaction.channel_id);
+		db.query( 'SELECT wiki, lang FROM discord WHERE guild = $1 AND (channel = $2 OR channel = $3 OR channel IS NULL) ORDER BY channel DESC NULLS LAST LIMIT 1', [interaction.guild_id, interaction.channel_id, '#' + channel?.parentID] ).then( ({rows:[row]}) => {
+			return buttons.verify(interaction, new Lang(( row?.lang || channel?.guild?.preferredLocale )), new Wiki(row?.wiki), channel);
+		}, dberror => {
+			console.log( '- Button: Error while getting the wiki: ' + dberror );
+			return client.api.interactions(interaction.id, interaction.token).callback.post( {
+				data: {type: 6}
+			} ).catch(log_error);
+		} );
+	}
+}
 
 client.on( 'message', msg => {
 	if ( isStop || msg.type !== 'DEFAULT' || msg.system || msg.webhookID || msg.author.bot || msg.author.id === msg.client.user.id ) return;

+ 1 - 1
cmds/rcscript.js

@@ -43,7 +43,7 @@ function cmd_rcscript(lang, msg, args, line, wiki) {
 			style: 5,
 			label: lang.get('settings.button'),
 			emoji: {
-				id: '588723748757307403',
+				id: '588723255972593672',
 				name: 'wikibot',
 				animated: false
 			},

+ 1 - 1
cmds/settings.js

@@ -30,7 +30,7 @@ function cmd_settings(lang, msg, args, line, wiki) {
 			style: 5,
 			label: lang.get('settings.button'),
 			emoji: {
-				id: '588723748757307403',
+				id: '588723255972593672',
 				name: 'wikibot',
 				animated: false
 			},

+ 1 - 1
cmds/verification.js

@@ -30,7 +30,7 @@ function cmd_verification(lang, msg, args, line, wiki) {
 			style: 5,
 			label: lang.get('settings.button'),
 			emoji: {
-				id: '588723748757307403',
+				id: '588723255972593672',
 				name: 'wikibot',
 				animated: false
 			},

+ 27 - 11
cmds/verify.js

@@ -40,17 +40,33 @@ function cmd_verify(lang, msg, args, line, wiki) {
 		msg.reactEmoji('⏳').then( reaction => {
 			verify(lang, msg.channel, msg.member, username, wiki, rows).then( result => {
 				if ( result.reaction ) msg.reactEmoji(result.reaction);
-				else msg.replyMsg( result.content, {embed: result.embed}, false, false ).then( message => {
-					if ( !result.logging.channel || !msg.guild.channels.cache.has(result.logging.channel) ) return;
-					if ( message ) {
-						if ( result.logging.embed ) result.logging.embed.addField(message.url, '<#' + msg.channel.id + '>');
-						else result.logging.content += '\n<#' + msg.channel.id + '> – <' + message.url + '>';
-					}
-					msg.guild.channels.cache.get(result.logging.channel).send(result.logging.content, {
-						embed: result.logging.embed,
-						allowedMentions: {parse: []}
-					}).catch(log_error);
-				} );
+				else {
+					var options = {embed: result.embed, components: []};
+					if ( result.add_button ) options.components.push({
+						type: 1,
+						components: [
+							{
+								type: 2,
+								style: 1,
+								label: lang.get('verify.button_again'),
+								emoji: {id: null, name: '🔂'},
+								custom_id: 'verify_again',
+								disabled: false
+							}
+						]
+					});
+					msg.replyMsg( result.content, options, false, false ).then( message => {
+						if ( !result.logging.channel || !msg.guild.channels.cache.has(result.logging.channel) ) return;
+						if ( message ) {
+							if ( result.logging.embed ) result.logging.embed.addField(message.url, '<#' + msg.channel.id + '>');
+							else result.logging.content += '\n<#' + msg.channel.id + '> – <' + message.url + '>';
+						}
+						msg.guild.channels.cache.get(result.logging.channel).send(result.logging.content, {
+							embed: result.logging.embed,
+							allowedMentions: {parse: []}
+						}).catch(log_error);
+					} );
+				}
 				if ( reaction ) reaction.removeEmoji();
 			}, error => {
 				console.log( '- Error during the verifications: ' + error );

+ 11 - 0
functions/verify.js

@@ -20,6 +20,7 @@ function verify(lang, channel, member, username, wiki, rows, old_username = '')
 	var embed = new MessageEmbed().setFooter( lang.get('verify.footer') ).setTimestamp();
 	var result = {
 		content: '', embed,
+		add_button: channel.permissionsFor(channel.guild.me).has('EMBED_LINKS'),
 		reaction: '',
 		logging: {
 			channel: '',
@@ -46,6 +47,7 @@ function verify(lang, channel, member, username, wiki, rows, old_username = '')
 				embed.setColor('#000000').setDescription( lang.get('verify.error') );
 				result.content = lang.get('verify.error_reply');
 			}
+			result.add_button = false;
 			return;
 		}
 		wiki.updateWiki(body.query.general);
@@ -56,6 +58,7 @@ function verify(lang, channel, member, username, wiki, rows, old_username = '')
 			username = ( body.query.users.length === 1 ? queryuser.name : username );
 			embed.setTitle( escapeFormatting( old_username || username ) ).setColor('#0000FF').setDescription( lang.get('verify.user_missing', escapeFormatting( old_username || username )) ).addField( lang.get('verify.notice'), lang.get('verify.help_missing') );
 			result.content = lang.get('verify.user_missing_reply', escapeFormatting( old_username || username ));
+			result.add_button = false;
 			if ( wiki.isFandom() && !old_username ) return got.get( wiki + 'api/v1/User/UsersByName?limit=1&query=' + encodeURIComponent( username ) + '&format=json' ).then( wsresponse => {
 				var wsbody = wsresponse.body;
 				if ( wsresponse.statusCode !== 200 || wsbody?.exception || wsbody?.users?.[0]?.name?.length !== username.length ) {
@@ -74,6 +77,7 @@ function verify(lang, channel, member, username, wiki, rows, old_username = '')
 		if ( queryuser.blockexpiry ) {
 			embed.setColor('#FF0000').setDescription( lang.get('verify.user_blocked', '[' + escapeFormatting(username) + '](' + pagelink + ')', queryuser.gender) );
 			result.content = lang.get('verify.user_blocked_reply', escapeFormatting(username), queryuser.gender);
+			result.add_button = false;
 			return;
 		}
 		
@@ -251,6 +255,7 @@ function verify(lang, channel, member, username, wiki, rows, old_username = '')
 							if ( verifynotice.onsuccess ) text += '\n\n**' + lang.get('verify.notice') + '** ' + verifynotice.onsuccess;
 						}
 						result.content = text;
+						result.add_button = false;
 					}, log_error );
 				}
 				
@@ -275,10 +280,12 @@ function verify(lang, channel, member, username, wiki, rows, old_username = '')
 				if ( error ) console.log( '- Error while getting the Discord tag: ' + error );
 				embed.setColor('#000000').setDescription( lang.get('verify.error') );
 				result.content = lang.get('verify.error_reply');
+				result.add_button = false;
 			} );
 		}, error => {
 			embed.setColor('#FF0000').setDescription( error.desc );
 			result.content = error.reply;
+			result.add_button = false;
 		} );
 		
 		return got.get( wiki + 'api.php?action=query' + ( wiki.hasCentralAuth() ? '&meta=globaluserinfo&guiprop=groups&guiuser=' + encodeURIComponent( username ) : '' ) + '&prop=revisions&rvprop=content|user&rvslots=main&titles=User:' + encodeURIComponent( username ) + '/Discord&format=json' ).then( mwresponse => {
@@ -294,6 +301,7 @@ function verify(lang, channel, member, username, wiki, rows, old_username = '')
 				if ( mwbody.query.globaluserinfo.locked !== undefined ) {
 					embed.setColor('#FF0000').setDescription( lang.get('verify.user_gblocked', '[' + escapeFormatting(username) + '](' + pagelink + ')', queryuser.gender) );
 					result.content = lang.get('verify.user_gblocked_reply', escapeFormatting(username), queryuser.gender);
+					result.add_button = false;
 					return;
 				}
 				queryuser.groups.push(...mwbody.query.globaluserinfo.groups);
@@ -410,6 +418,7 @@ function verify(lang, channel, member, username, wiki, rows, old_username = '')
 						if ( verifynotice.onsuccess ) text += '\n\n**' + lang.get('verify.notice') + '** ' + verifynotice.onsuccess;
 					}
 					result.content = text;
+					result.add_button = false;
 				}, log_error );
 			}
 			
@@ -433,11 +442,13 @@ function verify(lang, channel, member, username, wiki, rows, old_username = '')
 			console.log( '- Error while getting the Discord tag: ' + error );
 			embed.setColor('#000000').setDescription( lang.get('verify.error') );
 			result.content = lang.get('verify.error_reply');
+			result.add_button = false;
 		} );
 	}, error => {
 		console.log( '- Error while getting the user: ' + error );
 		embed.setColor('#000000').setDescription( lang.get('verify.error') );
 		result.content = lang.get('verify.error_reply');
+		result.add_button = false;
 	} ).then( new_username => {
 		if ( !new_username ) return result;
 		return verify(lang, channel, member, new_username, wiki, rows, username);

+ 1 - 0
i18n/en.json

@@ -786,6 +786,7 @@
     },
     "verify": {
         "audit_reason": "Verified as \"$1\"",
+        "button_again": "Check again",
         "discord": "Discord user:",
         "empty": "*empty*",
         "error": "The verification failed due to an error.",

+ 107 - 3
interactions/verify.js

@@ -3,7 +3,7 @@ var verify = require('../functions/verify.js');
 const {sendMessage} = require('../util/functions.js');
 
 /**
- * Post a message with inline wiki links.
+ * Wiki user verification.
  * @param {Object} interaction - The interaction.
  * @param {import('discord.js').Client} interaction.client - The client of the interaction.
  * @param {import('../util/i18n.js')} lang - The user language.
@@ -94,8 +94,22 @@ function slash_verify(interaction, lang, wiki, channel) {
 					var message = {
 						content: reply + result.content,
 						embeds: [result.embed],
-						allowed_mentions
+						allowed_mentions,
+						components: []
 					};
+					if ( result.add_button ) message.components.push({
+						type: 1,
+						components: [
+							{
+								type: 2,
+								style: 1,
+								label: lang.get('verify.button_again'),
+								emoji: {id: null, name: '🔂'},
+								custom_id: 'verify_again',
+								disabled: false
+							}
+						]
+					});
 					if ( result.reaction ) {
 						if ( result.reaction === 'nowiki' ) message.content = lang.get('interaction.nowiki');
 						else message.content = reply + lang.get('verify.error_reply');
@@ -142,7 +156,97 @@ function slash_verify(interaction, lang, wiki, channel) {
 	} );
 }
 
+/**
+ * Wiki user verification.
+ * @param {Object} interaction - The interaction.
+ * @param {import('discord.js').Client} interaction.client - The client of the interaction.
+ * @param {import('../util/i18n.js')} lang - The user language.
+ * @param {import('../util/wiki.js')} wiki - The wiki for the interaction.
+ * @param {import('discord.js').TextChannel} [channel] - The channel for the interaction.
+ */
+ function button_verify(interaction, lang, wiki, channel) {
+	return db.query( 'SELECT role, editcount, postcount, usergroup, accountage, rename FROM verification WHERE guild = $1 AND channel LIKE $2 ORDER BY configid ASC', [interaction.guild_id, '%|' + interaction.channel_id + '|%'] ).then( ({rows}) => {
+		if ( !rows.length || !channel.guild.me.permissions.has('MANAGE_ROLES') ) {
+			return interaction.client.api.interactions(interaction.id, interaction.token).callback.post( {
+				data: {type: 6}
+			} ).catch(log_error);
+		}
+		var reply = '<@' + ( interaction.member?.nick ? '!' : '' ) + interaction.user.id + '>, ';
+		var allowed_mentions = {
+			users: [interaction.user.id]
+		};
+		interaction.message.allowed_mentions = allowed_mentions;
+		interaction.message.components[0].components[0].disabled = true;
+		return interaction.client.api.interactions(interaction.id, interaction.token).callback.post( {
+			data: {
+				type: 7,
+				data: interaction.message
+			}
+		} ).then( () => {
+			return channel.guild.members.fetch(interaction.user.id).then( member => {
+				var username = interaction.message.embeds[0].title;
+				console.log( interaction.guild_id + ': Button: ' + interaction.data.custom_id + ' ' + username );
+				return verify(lang, channel, member, username, wiki, rows).then( result => {
+					var message = {
+						content: reply + result.content,
+						embeds: [result.embed],
+						allowed_mentions,
+						components: []
+					};
+					if ( result.add_button ) message.components.push({
+						type: 1,
+						components: [
+							{
+								type: 2,
+								style: 1,
+								label: lang.get('verify.button_again'),
+								emoji: {id: null, name: '🔂'},
+								custom_id: 'verify_again',
+								disabled: false
+							}
+						]
+					});
+					if ( result.reaction ) {
+						if ( result.reaction === 'nowiki' ) message.content = lang.get('interaction.nowiki');
+						else message.content = reply + lang.get('verify.error_reply');
+						message.embeds = [];
+					}
+					return sendMessage(interaction, message, channel, false).then( msg => {
+						if ( !result.logging.channel || !channel.guild.channels.cache.has(result.logging.channel) ) return;
+						if ( msg ) {
+							if ( result.logging.embed ) result.logging.embed.addField(msg.url, '<#' + channel.id + '>');
+							else result.logging.content += '\n<#' + channel.id + '> – <' + msg.url + '>';
+						}
+						channel.guild.channels.cache.get(result.logging.channel).send(result.logging.content, {
+							embed: result.logging.embed,
+							allowedMentions: {parse: []}
+						}).catch(log_error);
+					} );
+				}, error => {
+					console.log( '- Error during the verifications: ' + error );
+					return sendMessage(interaction, {
+						content: reply + lang.get('verify.error_reply'),
+						allowed_mentions
+					}, channel);
+				} );
+			}, error => {
+				console.log( '- Error while getting the member: ' + error );
+				return sendMessage(interaction, {
+					content: reply + lang.get('verify.error_reply'),
+					allowed_mentions
+				}, channel);
+			} );
+		} ).catch(log_error);
+	}, dberror => {
+		console.log( '- Error while getting the verifications: ' + dberror );
+		return interaction.client.api.interactions(interaction.id, interaction.token).callback.post( {
+			data: {type: 6}
+		} ).catch(log_error);
+	} );
+}
+
 module.exports = {
 	name: 'verify',
-	run: slash_verify
+	run: slash_verify,
+	button: button_verify
 };