Skip to content

Commit

Permalink
init RcGcDw buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus-Rost committed May 5, 2023
1 parent 9274b2e commit 1d003c6
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ updates:
directory: "/"
schedule:
interval: "daily"
# Maintain dependencies for RcGcDb
# Maintain dependencies for RcGcDb and RcGcDw buttons
- package-ecosystem: "gitsubmodule"
directory: "/"
schedule:
Expand Down
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
path = RcGcDb
url = https://gitlab.com/chicken-riders/RcGcDb.git
branch = master
[submodule "rcgcdw-buttons"]
path = RcGcDw_buttons
url = https://github.com/Markus-Rost/rcgcdw-buttons.git
branch = master
6 changes: 5 additions & 1 deletion bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Lang from './util/i18n.js';
import Wiki from './util/wiki.js';
import { default as newMessage, defaultSettings } from './util/newMessage.js';
import { breakOnTimeoutPause, allowDelete } from './util/functions.js';
import rcscriptButtons from './functions/rcscript_buttons.js';

const client = new Discord.Client( {
makeCache: Discord.Options.cacheWithLimits( {
Expand Down Expand Up @@ -207,7 +208,10 @@ client.on( Discord.Events.InteractionCreate, interaction => {
}
else if ( interaction.isButton() ) {
if ( interaction.customId !== 'verify_again' ) console.log( ( interaction.guildId || '@' + interaction.user.id ) + ': Button: ' + interaction.customId );
if ( !interaction_commands.button.hasOwnProperty(interaction.customId) ) return;
if ( !interaction_commands.button.hasOwnProperty(interaction.customId) ) {
if ( interaction.inGuild() && interaction.customId.startsWith( 'rc_' ) ) rcscriptButtons(interaction);
return;
}
cmd = interaction_commands.button[interaction.customId];
}
else if ( interaction.isModalSubmit() ) {
Expand Down
4 changes: 2 additions & 2 deletions cmds/wiki/discussion.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default function fandom_discussion(lang, msg, wiki, title, sitename, spoi
if ( descresponse.statusCode !== 200 || !descbody ) {
return console.log( '- ' + descresponse.statusCode + ': Error while getting the description.' );
}
var thumbnail = wiki.toLink('Special:FilePath/Wiki-wordmark.png');
var thumbnail = wiki.toLink('Special:FilePath/Site-community-image');
var parser = new HTMLParser( {
onopentag: (tagname, attribs) => {
if ( tagname === 'body' ) parser.pause(); // Prevent the parser from running too long
Expand Down Expand Up @@ -294,7 +294,7 @@ function discussion_send(lang, msg, wiki, discussion, embed, spoiler, noEmbed) {
embed.setThumbnail( discussion._embedded.contentImages[0].url );
}
}
else embed.setThumbnail( wiki.toLink('Special:FilePath/Wiki-wordmark.png') );
else embed.setThumbnail( wiki.toLink('Special:FilePath/Site-community-image') );
}
catch ( jsonerror ) {
console.log( '- Error while getting the formatting: ' + jsonerror );
Expand Down
2 changes: 1 addition & 1 deletion dashboard/functions/rcscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ function createForm($, header, dashboardLang, settings, guildChannels, allWikis)
let feeds = $('<div id="wb-settings-feeds-hide">').append(fieldset.feeds);
feeds.find('label').eq(0).text(dashboardLang.get('rcscript.form.feeds'));
feeds.find('label').eq(1).text(dashboardLang.get('rcscript.form.feeds_only'));
if ( /\.(?:fandom\.com|wikia\.org)$/.test(new URL(settings.wiki).hostname) ) {
if ( new URL(settings.wiki)?.hostname?.endsWith( '.fandom.com' ) ) {
if ( settings.postid !== '-1' ) {
feeds.find('#wb-settings-feeds').attr('checked', '');
if ( settings.rcid === -1 ) feeds.find('#wb-settings-feeds-only').attr('checked', '');
Expand Down
2 changes: 1 addition & 1 deletion dashboard/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ for ( var w = 0; w < wikis.length; w++ ) (function(wiki) {
if ( wikicheck && wikichecknotice ) {
wikicheck.onclick = function() {
var wikinew = wiki.value.replace( /^(?:https?:)?\/\//, '' );
var regex = wikinew.match( /^([a-z\d-]{1,50}\.(?:gamepedia\.com|(?:fandom\.com|wikia\.org)(?:(?!\/(?:wiki|api)\/)\/[a-z-]{2,12})?))(?:\/|$)/ );
var regex = wikinew.match( /^([a-z\d-]{1,50}\.fandom\.com(?:(?!\/(?:wiki|api)\/)\/[a-z-]{2,12})?)(?:\/|$)/ );
if ( regex ) wikinew = regex[1];
else if ( !wiki.validity.valid ) return wiki.reportValidity();
else {
Expand Down
58 changes: 58 additions & 0 deletions functions/rcscript_buttons.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { existsSync } from 'node:fs';
import gotDefault from 'got';
const got = gotDefault.extend( {
throwHttpErrors: true,
timeout: {
request: 5_000
},
headers: {
'user-agent': 'Wiki-Bot/' + ( isDebug ? 'testing' : process.env.npm_package_version ) + ' (Discord; ' + process.env.npm_package_name + ( process.env.invite ? '; ' + process.env.invite : '' ) + ')'
},
responseType: 'json'
} );

const buttonsExists = existsSync('./RcGcDw_buttons/main.js');

/**
* @param {import('discord.js').ButtonInteraction<'cached'|'raw'>} interaction
*/
function rcscript_buttons(interaction) {
got.post( 'http://localhost:8000/interactions', {
json: {
version: interaction.version,
type: interaction.type,
id: interaction.id,
token: interaction.token,
application_id: interaction.applicationId,
guild_id: interaction.guildId,
channel_id: interaction.channelId,
app_permissions: interaction.appPermissions,
locale: interaction.locale,
guild_locale: interaction.guildLocale,
data: {
custom_id: interaction.customId.replace( 'rc_', '' ),
component_type: interaction.componentType
},
message: ( interaction.message ? {
id: interaction.message.id,
content: interaction.message.content,
embeds: interaction.message.embeds,
components: interaction.message.components
} : null ),
member: {
user: {
id: interaction.user.id,
},
}
},
headers: {
authorization: process.env.buttons_token
}
} ).then( response => {
if ( response.body.result !== 'Success' ) console.log( '- RcGcDw buttons: ' + response.statusCode + ': Error while sending the interaction.' );
}, error => {
console.log( '- RcGcDw buttons: Error while sending the interaction: ' + error );
} );
}

export default ( buttonsExists ? rcscript_buttons : () => {} );
5 changes: 3 additions & 2 deletions util/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -399,14 +399,15 @@ function htmlToDiscord(html, pagelink = '', ...escapeArgs) {
},
ontext: (htmltext) => {
if ( !ignoredTag[0] ) {
if ( href && !code ) htmltext = htmltext.replace( /[\[\]]/g, '\\$&' );
if ( code ) text += htmltext.replaceAll( '`', 'ˋ' );
else {
htmltext = htmltext.replace( /[\r\n\t ]+/g, ' ' );
if ( /[\n ]$/.test(text) && htmltext.startsWith( ' ' ) ) {
htmltext = htmltext.replace( /^ +/, '' );
}
text += escapeFormatting(htmltext, ...escapeArgs);
htmltext = escapeFormatting(htmltext, ...escapeArgs);
if ( href ) htmltext = htmltext.replace( /[\[\]]/g, '\\$&' );
text += htmltext;
}
}
},
Expand Down
2 changes: 0 additions & 2 deletions util/wiki.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,6 @@ export default class Wiki extends URL {
try {
if ( input instanceof URL ) return new Wiki(input);
input = input.replace( /^(?:https?:)?\/\//, 'https://' );
let regex = input.match( /^(?:https:\/\/)?([a-z\d-]{1,50}\.(?:gamepedia\.com|(?:fandom\.com|wikia\.org)(?:(?!\/(?:wiki|api)\/)\/[a-z-]{2,12})?))(?:\/|$)/ );
if ( regex ) return new Wiki('https://' + regex[1] + '/');
let project = inputToWikiProject(input);
if ( project ) return new Wiki(project.fullScriptPath);
let proxy = inputToFrontendProxy(input);
Expand Down

0 comments on commit 1d003c6

Please sign in to comment.