Skip to content

Commit

Permalink
make embed limits settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus-Rost committed Nov 16, 2022
1 parent 657b0e1 commit ba2fe13
Show file tree
Hide file tree
Showing 26 changed files with 366 additions and 186 deletions.
25 changes: 20 additions & 5 deletions bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Discord from 'discord.js';
import db from './util/database.js';
import Lang from './util/i18n.js';
import Wiki from './util/wiki.js';
import newMessage from './util/newMessage.js';
import { default as newMessage, defaultSettings } from './util/newMessage.js';
import { breakOnTimeoutPause, allowDelete } from './util/functions.js';

const client = new Discord.Client( {
Expand Down Expand Up @@ -222,13 +222,21 @@ client.on( Discord.Events.InteractionCreate, interaction => {
else return;

if ( !interaction.inGuild() ) {
return cmd(interaction, new Lang(interaction.guildLocale), new Wiki());
interaction.embedLimits = {...defaultSettings.embedLimits};
return cmd(interaction, new Lang(interaction.locale), new Wiki());
}
let sqlargs = [interaction.guildId];
if ( interaction.channel?.isThread() ) sqlargs.push(interaction.channel.parentId, '#' + interaction.channel.parent?.parentId);
else sqlargs.push(interaction.channelId, '#' + interaction.channel?.parentId);
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', sqlargs ).then( ({rows:[row]}) => {
db.query( 'SELECT wiki, lang, desclength, fieldcount, fieldlength, sectionlength, sectiondesclength FROM discord WHERE guild = $1 AND (channel = $2 OR channel = $3 OR channel IS NULL) ORDER BY channel DESC NULLS LAST LIMIT 1', sqlargs ).then( ({rows:[row]}) => {
if ( !row ) interaction.defaultSettings = true;
interaction.embedLimits = {
descLength: row.desclength ?? defaultSettings.embedLimits.descLength,
fieldCount: row.fieldcount ?? defaultSettings.embedLimits.fieldCount,
fieldLength: row.fieldlength ?? defaultSettings.embedLimits.fieldLength,
sectionLength: row.sectionlength ?? defaultSettings.embedLimits.sectionLength,
sectionDescLength: row.sectiondesclength ?? Math.min(row.desclength ?? defaultSettings.embedLimits.sectionDescLength, defaultSettings.embedLimits.sectionDescLength)
};
return cmd(interaction, new Lang(( row?.lang || interaction.guildLocale )), new Wiki(row?.wiki));
}, dberror => {
console.log( '- Interaction: Error while getting the wiki: ' + dberror );
Expand Down Expand Up @@ -296,13 +304,20 @@ function messageCreate(msg) {
}
return;
}
db.query( 'SELECT wiki, lang, role, inline, (SELECT array_agg(ARRAY[prefixchar, prefixwiki] ORDER BY prefixchar) FROM subprefix WHERE guild = $1) AS subprefixes FROM discord WHERE guild = $1 AND (channel = $2 OR channel = $3 OR channel IS NULL) ORDER BY channel DESC NULLS LAST LIMIT 1', sqlargs ).then( ({rows:[row]}) => {
db.query( 'SELECT wiki, lang, role, inline, desclength, fieldcount, fieldlength, sectionlength, sectiondesclength, (SELECT array_agg(ARRAY[prefixchar, prefixwiki] ORDER BY prefixchar) FROM subprefix WHERE guild = $1) AS subprefixes FROM discord WHERE guild = $1 AND (channel = $2 OR channel = $3 OR channel IS NULL) ORDER BY channel DESC NULLS LAST LIMIT 1', sqlargs ).then( ({rows:[row]}) => {
if ( row ) {
if ( msg.guild.roles.cache.has(row.role) && msg.guild.roles.cache.get(row.role).comparePositionTo(msg.member.roles.highest) > 0 && !msg.isAdmin() ) {
msg.onlyVerifyCommand = true;
}
let subprefixes = ( row.subprefixes?.length ? new Map(row.subprefixes) : undefined );
newMessage(msg, new Lang(row.lang), row.wiki, patreonGuildsPrefix.get(msg.guildId), row.inline, subprefixes);
let embedLimits = {
descLength: row.desclength ?? defaultSettings.embedLimits.descLength,
fieldCount: row.fieldcount ?? defaultSettings.embedLimits.fieldCount,
fieldLength: row.fieldlength ?? defaultSettings.embedLimits.fieldLength,
sectionLength: row.sectionlength ?? defaultSettings.embedLimits.sectionLength,
sectionDescLength: row.sectiondesclength ?? Math.min(row.desclength ?? defaultSettings.embedLimits.sectionDescLength, defaultSettings.embedLimits.sectionDescLength)
};
newMessage(msg, new Lang(row.lang), row.wiki, patreonGuildsPrefix.get(msg.guildId), row.inline, subprefixes, embedLimits);
}
else {
msg.defaultSettings = true;
Expand Down
6 changes: 3 additions & 3 deletions cmds/eval.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default async function cmd_eval(lang, msg, args, line, wiki) {
msg.wikiPrefixes.forEach( (prefixchar, prefixwiki) => {
if ( prefixchar ) subprefixes.set(prefixchar, prefixwiki);
} );
newMessage(msg, lang, wiki, patreonGuildsPrefix.get(msg.guildId), msg.noInline, subprefixes, cmdline);
newMessage(msg, lang, wiki, patreonGuildsPrefix.get(msg.guildId), msg.noInline, subprefixes, {...msg.embedLimits}, cmdline);
return cmdline;
}
}
Expand Down Expand Up @@ -179,12 +179,12 @@ function removePatreons(guild, msg) {
}
return db.connect().then( client => {
var messages = [];
return client.query( 'SELECT lang, role, inline FROM discord WHERE guild = $1 AND channel IS NULL', [guild] ).then( ({rows:[row]}) => {
return client.query( 'SELECT lang, role, inline, desclength, fieldcount, fieldlength, sectionlength, sectiondesclength FROM discord WHERE guild = $1 AND channel IS NULL', [guild] ).then( ({rows:[row]}) => {
if ( !row ) {
messages.push('The guild doesn\'t exist!');
return Promise.reject();
}
return client.query( 'UPDATE discord SET lang = $1, role = $2, inline = $3, prefix = $4, patreon = NULL WHERE guild = $5', [row.lang, row.role, row.inline, process.env.prefix, guild] ).then( ({rowCount}) => {
return client.query( 'UPDATE discord SET lang = $1, role = $2, inline = $3, desclength = $4, fieldcount = $5, fieldlength = $6, sectionlength = $7, sectiondesclength = $8, prefix = $9, patreon = NULL WHERE guild = $10', [row.lang, row.role, row.inline, row.desclength, row.fieldcount, row.fieldlength, row.sectionlength, row.sectiondesclength, process.env.prefix, guild] ).then( ({rowCount}) => {
if ( rowCount ) {
console.log( '- Guild successfully updated.' );
messages.push('Guild successfully updated.');
Expand Down
24 changes: 18 additions & 6 deletions cmds/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,26 @@ export default async function cmd_get(lang, msg, args, line, wiki) {
var guildchannel = ['Updates channel:', '`' + guild.channel + '`'];
var guildsettings = ['Settings:', '*unknown*'];

return db.query( 'SELECT channel, wiki, lang, role, inline, prefix, (SELECT array_agg(ARRAY[prefixchar, prefixwiki] ORDER BY prefixchar) FROM subprefix WHERE guild = $1) AS subprefixes FROM discord WHERE guild = $1 ORDER BY channel ASC NULLS FIRST', [guild.id] ).then( ({rows}) => {
return db.query( 'SELECT channel, wiki, lang, role, inline, desclength, fieldcount, fieldlength, sectionlength, sectiondesclength, prefix, (SELECT array_agg(ARRAY[prefixchar, prefixwiki] ORDER BY prefixchar) FROM subprefix WHERE guild = $1) AS subprefixes FROM discord WHERE guild = $1 ORDER BY channel ASC NULLS FIRST', [guild.id] ).then( ({rows}) => {
if ( rows.length ) {
let row = rows.find( row => !row.channel );
row.patreon = patreonGuildsPrefix.has(guild.id);
let mainRow = rows.find( row => !row.channel );
mainRow.patreon = patreonGuildsPrefix.has(guild.id);
let subprefixes = {};
row.subprefixes?.forEach( subprefix => subprefixes[subprefix[0]] = subprefix[1] );
row.subprefixes = subprefixes;
rows.filter( row => row.channel ).forEach( row => delete row.subprefixes );
mainRow.subprefixes?.forEach( subprefix => subprefixes[subprefix[0]] = subprefix[1] );
mainRow.subprefixes = subprefixes;
rows.filter( row => row.channel ).forEach( row => {
delete row.subprefixes;
if ( !mainRow.patreon ) {
if ( row.lang === mainRow.lang ) delete row.lang;
if ( row.role === mainRow.role ) delete row.role;
if ( row.inline === mainRow.inline ) delete row.inline;
if ( row.desclength === mainRow.desclength ) delete row.desclength;
if ( row.fieldcount === mainRow.fieldcount ) delete row.fieldcount;
if ( row.fieldlength === mainRow.fieldlength ) delete row.fieldlength;
if ( row.sectionlength === mainRow.sectionlength ) delete row.sectionlength;
if ( row.sectiondesclength === mainRow.sectiondesclength ) delete row.sectiondesclength;
}
} );
guildsettings[1] = '```json\n' + JSON.stringify( rows, null, '\t' ) + '\n```';
}
else guildsettings[1] = '*default*';
Expand Down
10 changes: 5 additions & 5 deletions cmds/minecraft/bug.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function minecraft_bug(lang, msg, wiki, args, title, cmd, reaction, spoiler, noE
var embed = null;
if ( !noEmbed ) {
embed = new EmbedBuilder().setAuthor( {name: 'Mojira'} ).setTitle( summary ).setURL( baseBrowseUrl + body.key );
if ( DESC_LENGTH ) embed.setDescription( limitLength(description, DESC_LENGTH, 20) );
if ( msg.embedLimits.descLength ) embed.setDescription( limitLength(description, msg.embedLimits.descLength, 20) );
var links = body.fields.issuelinks.filter( link => link.outwardIssue || ( link.inwardIssue && link.type.name !== 'Duplicate' ) );
if ( links.length ) {
var linkList = lang.get('minecraft.issue_link');
Expand All @@ -66,7 +66,7 @@ function minecraft_bug(lang, msg, wiki, args, title, cmd, reaction, spoiler, noE
var name = ( linkList?.[link.type.name]?.[ward]?.replaceAllSafe( '$1', issue.key ) || link.type[ward] + ' ' + issue.key );
var status = issue.fields.status.name;
var value = ( statusList?.[status] || status ) + ': [' + escapeFormatting(issue.fields.summary) + '](<' + baseBrowseUrl + issue.key + '>)';
if ( ( embed.data.fields?.length ?? 0 ) < FIELD_COUNT && ( getEmbedLength(embed) + name.length + value.length ) < 6000 ) embed.addFields( {name, value} );
if ( ( embed.data.fields?.length ?? 0 ) < msg.embedLimits.fieldCount && ( getEmbedLength(embed) + name.length + value.length ) < 6000 ) embed.addFields( {name, value} );
else extralinks.push({name,value,inline:false});
} );
if ( extralinks.length ) embed.setFooter( {text: lang.get('minecraft.more', extralinks.length.toLocaleString(lang.get('dateformat')), extralinks.length)} );
Expand Down Expand Up @@ -94,7 +94,7 @@ function minecraft_bug(lang, msg, wiki, args, title, cmd, reaction, spoiler, noE
jql: 'fixVersion="' + args.join(' ').replace( /["\\]/g, '\\$&' ) + '" order by key'
});
var uri = 'https://bugs.mojang.com/issues/?' + jql;
return got.get( 'https://bugs.mojang.com/rest/api/2/search?fields=summary,resolution,status&' + jql + '&maxResults=' + FIELD_COUNT, {
return got.get( 'https://bugs.mojang.com/rest/api/2/search?fields=summary,resolution,status&' + jql + '&maxResults=' + msg.embedLimits.fieldCount, {
context: {
guildId: msg.guildId
}
Expand Down Expand Up @@ -128,8 +128,8 @@ function minecraft_bug(lang, msg, wiki, args, title, cmd, reaction, spoiler, noE
var value = ( statusList?.[status] || status ) + ': [' + escapeFormatting(bug.fields.summary) + '](<https://bugs.mojang.com/browse/' + bug.key + '>)';
embed.addFields( {name: bug.key, value} );
} );
if ( body.total > FIELD_COUNT ) {
var extrabugs = body.total - FIELD_COUNT;
if ( body.total > msg.embedLimits.fieldCount ) {
var extrabugs = body.total - msg.embedLimits.fieldCount;
embed.setFooter( {text: lang.get('minecraft.more', extrabugs.toLocaleString(lang.get('dateformat')), extrabugs)} );
}
}
Expand Down
8 changes: 4 additions & 4 deletions cmds/patreon.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export default function cmd_patreon(lang, msg, args, line, wiki) {
if ( !guild ) return msg.replyMsg( 'I\'m not on a server with the id `' + args[1] + '`.', true );
if ( !patreonGuildsPrefix.has(args[1]) ) return msg.replyMsg( '"' + guild + '" doesn\'t have the patreon features enabled.', true );
return db.connect().then( client => {
return client.query( 'SELECT lang, role, inline FROM discord WHERE guild = $1 AND patreon = $2', [args[1], msg.author.id] ).then( ({rows:[row]}) => {
return client.query( 'SELECT lang, role, inline, desclength, fieldcount, fieldlength, sectionlength, sectiondesclength FROM discord WHERE guild = $1 AND patreon = $2', [args[1], msg.author.id] ).then( ({rows:[row]}) => {
if ( !row ) {
msg.replyMsg( 'You didn\'t enable the patreon features for "' + guild + '"!', true );
return Promise.reject();
Expand All @@ -86,7 +86,7 @@ export default function cmd_patreon(lang, msg, args, line, wiki) {
msg.replyMsg( lang.get('general.readonly') + '\n' + process.env.invite, true );
return Promise.reject();
}
return client.query( 'UPDATE discord SET lang = $1, role = $2, inline = $3, prefix = $4, patreon = NULL WHERE guild = $5', [row.lang, row.role, row.inline, process.env.prefix, args[1]] ).then( () => {
return client.query( 'UPDATE discord SET lang = $1, role = $2, inline = $3, desclength = $4, fieldcount = $5, fieldlength = $6, sectionlength = $7, sectiondesclength = $8, prefix = $9, patreon = NULL WHERE guild = $10', [row.lang, row.role, row.inline, row.desclength, row.fieldcount, row.fieldlength, row.sectionlength, row.sectiondesclength, process.env.prefix, args[1]] ).then( () => {
console.log( '- Guild successfully updated.' );
msg.client.shard.broadcastEval( (discordClient, evalData) => {
globalThis.patreonGuildsPrefix.delete(evalData);
Expand Down Expand Up @@ -251,9 +251,9 @@ export default function cmd_patreon(lang, msg, args, line, wiki) {
msg.replyMsg( 'I got an error while deleting <@' + args[1] + '>, please try again later.', true );
return Promise.reject();
} ).then( () => {
return client.query( 'SELECT guild, lang, role, inline FROM discord WHERE guild IN (' + guilds.map( (guild, i) => '$' + ( i + 1 ) ).join(', ') + ') AND channel IS NULL', guilds ).then( ({rows}) => {
return client.query( 'SELECT guild, lang, role, inline, desclength, fieldcount, fieldlength, sectionlength, sectiondesclength FROM discord WHERE guild IN (' + guilds.map( (guild, i) => '$' + ( i + 1 ) ).join(', ') + ') AND channel IS NULL', guilds ).then( ({rows}) => {
return Promise.all(rows.map( row => {
return client.query( 'UPDATE discord SET lang = $1, role = $2, inline = $3, prefix = $4, patreon = NULL WHERE guild = $5', [row.lang, row.role, row.inline, process.env.prefix, row.guild] ).then( () => {
return client.query( 'UPDATE discord SET lang = $1, role = $2, inline = $3, desclength = $4, fieldcount = $5, fieldlength = $6, sectionlength = $7, sectiondesclength = $8, prefix = $9, patreon = NULL WHERE guild = $10', [row.lang, row.role, row.inline, row.desclength, row.fieldcount, row.fieldlength, row.sectionlength, row.sectiondesclength, process.env.prefix, row.guild] ).then( () => {
console.log( '- Guild successfully updated.' );
}, dberror => {
console.log( '- Error while updating the guild: ' + dberror );
Expand Down
Loading

0 comments on commit ba2fe13

Please sign in to comment.