Add !edit_snippet for editing snippets. Add !snippets to list all available snippets.
parent
27f8ca56dc
commit
094f490357
51
src/index.js
51
src/index.js
|
@ -405,18 +405,13 @@ bot.registerCommand('logs', (msg, args) => {
|
||||||
return `\`${formattedDate}\`: <${info.url}>`;
|
return `\`${formattedDate}\`: <${info.url}>`;
|
||||||
}).join('\n');
|
}).join('\n');
|
||||||
|
|
||||||
// Send list of logs in chunks of 15 lines per message
|
// Send the list of logs in chunks of 15 lines per message
|
||||||
const lines = message.split('\n');
|
const lines = message.split('\n');
|
||||||
const chunks = [];
|
const chunks = utils.chunk(lines, 15);
|
||||||
const chunkSize = 15;
|
|
||||||
|
|
||||||
for (let i = 0; i < lines.length; i += chunkSize) {
|
|
||||||
chunks.push(lines.slice(i, i + chunkSize).join('\n'));
|
|
||||||
}
|
|
||||||
|
|
||||||
let root = Promise.resolve();
|
let root = Promise.resolve();
|
||||||
chunks.forEach(chunk => {
|
chunks.forEach(lines => {
|
||||||
root = root.then(() => msg.channel.createMessage(chunk));
|
root = root.then(() => msg.channel.createMessage(lines.join('\n')));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -465,7 +460,7 @@ bot.registerCommand('snippet', async (msg, args) => {
|
||||||
if (snippet) {
|
if (snippet) {
|
||||||
if (text) {
|
if (text) {
|
||||||
// If the snippet exists and we're trying to create a new one, inform the user the snippet already exists
|
// If the snippet exists and we're trying to create a new one, inform the user the snippet already exists
|
||||||
msg.channel.createMessage(`Snippet "${shortcut}" already exists! You can delete it with ${prefix}delete_snippet.`);
|
msg.channel.createMessage(`Snippet "${shortcut}" already exists! You can edit or delete it with ${prefix}edit_snippet and ${prefix}delete_snippet respectively.`);
|
||||||
} else {
|
} else {
|
||||||
// If the snippet exists and we're NOT trying to create a new one, show info about the existing snippet
|
// If the snippet exists and we're NOT trying to create a new one, show info about the existing snippet
|
||||||
msg.channel.createMessage(`\`${snippetPrefix}${shortcut}\` replies ${snippet.isAnonymous ? 'anonymously ' : ''}with:\n${snippet.text}`);
|
msg.channel.createMessage(`\`${snippetPrefix}${shortcut}\` replies ${snippet.isAnonymous ? 'anonymously ' : ''}with:\n${snippet.text}`);
|
||||||
|
@ -503,6 +498,42 @@ bot.registerCommand('delete_snippet', async (msg, args) => {
|
||||||
});
|
});
|
||||||
bot.registerCommandAlias('ds', 'delete_snippet');
|
bot.registerCommandAlias('ds', 'delete_snippet');
|
||||||
|
|
||||||
|
bot.registerCommand('edit_snippet', async (msg, args) => {
|
||||||
|
if (! msg.channel.guild) return;
|
||||||
|
if (msg.channel.guild.id !== utils.getModmailGuild(bot).id) return;
|
||||||
|
if (! msg.member.permission.has('manageRoles')) return;
|
||||||
|
|
||||||
|
const shortcut = args[0];
|
||||||
|
const text = args.slice(1).join(' ').trim();
|
||||||
|
|
||||||
|
if (! shortcut) return;
|
||||||
|
if (! text) return;
|
||||||
|
|
||||||
|
const snippet = await snippets.get(shortcut);
|
||||||
|
if (! snippet) {
|
||||||
|
msg.channel.createMessage(`Snippet "${shortcut}" doesn't exist!`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await snippets.del(shortcut);
|
||||||
|
await snippets.add(shortcut, text, snippet.isAnonymous);
|
||||||
|
|
||||||
|
msg.channel.createMessage(`Snippet "${shortcut}" edited!`);
|
||||||
|
});
|
||||||
|
bot.registerCommandAlias('es', 'edit_snippet');
|
||||||
|
|
||||||
|
bot.registerCommand('snippets', async msg => {
|
||||||
|
if (! msg.channel.guild) return;
|
||||||
|
if (msg.channel.guild.id !== utils.getModmailGuild(bot).id) return;
|
||||||
|
if (! msg.member.permission.has('manageRoles')) return;
|
||||||
|
|
||||||
|
const allSnippets = await snippets.all();
|
||||||
|
const shortcuts = Object.keys(allSnippets);
|
||||||
|
shortcuts.sort();
|
||||||
|
|
||||||
|
msg.channel.createMessage(`Available snippets (prefix ${snippetPrefix}):\n${shortcuts.join(', ')}`);
|
||||||
|
});
|
||||||
|
|
||||||
bot.connect();
|
bot.connect();
|
||||||
restBot.connect();
|
restBot.connect();
|
||||||
webserver.run();
|
webserver.run();
|
||||||
|
|
|
@ -25,7 +25,7 @@ function getSnippet(shortcut) {
|
||||||
* @returns {Promise}
|
* @returns {Promise}
|
||||||
*/
|
*/
|
||||||
function addSnippet(shortcut, text, isAnonymous = false) {
|
function addSnippet(shortcut, text, isAnonymous = false) {
|
||||||
return jsonDb.get('snippets', []).then(snippets => {
|
return jsonDb.get('snippets', {}).then(snippets => {
|
||||||
snippets[shortcut] = {
|
snippets[shortcut] = {
|
||||||
text,
|
text,
|
||||||
isAnonymous,
|
isAnonymous,
|
||||||
|
@ -41,14 +41,19 @@ function addSnippet(shortcut, text, isAnonymous = false) {
|
||||||
* @returns {Promise}
|
* @returns {Promise}
|
||||||
*/
|
*/
|
||||||
function deleteSnippet(shortcut) {
|
function deleteSnippet(shortcut) {
|
||||||
return jsonDb.get('snippets', []).then(snippets => {
|
return jsonDb.get('snippets', {}).then(snippets => {
|
||||||
delete snippets[shortcut];
|
delete snippets[shortcut];
|
||||||
jsonDb.save('snippets', snippets);
|
jsonDb.save('snippets', snippets);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getAllSnippets() {
|
||||||
|
return jsonDb.get('snippets', {});
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
get: getSnippet,
|
get: getSnippet,
|
||||||
add: addSnippet,
|
add: addSnippet,
|
||||||
del: deleteSnippet,
|
del: deleteSnippet,
|
||||||
|
all: getAllSnippets,
|
||||||
};
|
};
|
||||||
|
|
17
src/utils.js
17
src/utils.js
|
@ -83,6 +83,22 @@ function getMainRole(member) {
|
||||||
return roles.find(r => r.hoist);
|
return roles.find(r => r.hoist);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Splits array items into chunks of the specified size
|
||||||
|
* @param {Array} items
|
||||||
|
* @param {Number} chunkSize
|
||||||
|
* @returns {Array}
|
||||||
|
*/
|
||||||
|
function chunk(items, chunkSize) {
|
||||||
|
const result = [];
|
||||||
|
|
||||||
|
for (let i = 0; i < items.length; i += chunkSize) {
|
||||||
|
result.push(items.slice(i, i + chunkSize));
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
getModmailGuild,
|
getModmailGuild,
|
||||||
getMainGuild,
|
getMainGuild,
|
||||||
|
@ -91,4 +107,5 @@ module.exports = {
|
||||||
disableLinkPreviews,
|
disableLinkPreviews,
|
||||||
getSelfUrl,
|
getSelfUrl,
|
||||||
getMainRole,
|
getMainRole,
|
||||||
|
chunk,
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue