Skip to content

Commit

Permalink
feat: Add scheduled user scripts; add config.app.includeDiscordAuthor…
Browse files Browse the repository at this point in the history
…NameInIRCMessages; fix unicode handling in user-script editor
  • Loading branch information
edfletcher committed Jan 13, 2024
1 parent aeda423 commit 2ca8bc9
Show file tree
Hide file tree
Showing 11 changed files with 362 additions and 121 deletions.
3 changes: 2 additions & 1 deletion config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ const _config = {
mpmPlotOutputPath: path.join(HTTP_STATIC_DIR, MPM_PLOT_FILE_NAME),
mpmPlotTimeLimitHours: 24
},
userScriptsEnabledAtStartup: false
userScriptsEnabledAtStartup: false,
includeDiscordAuthorNameInIRCMessages: false // enable when using DRC as a bridge
},

discord: {
Expand Down
2 changes: 2 additions & 0 deletions discord.js
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,8 @@ client.once('ready', async () => {
isReconnect,
setIsReconnect: (s) => (_isReconnect = s)
};

userCommands('scripts').bindContextForCronRuns(mainContext);
mainSubClient.on('message', ipcMessageHandler.bind(null, mainContext));

const userScriptsSubClient = new Redis(config.redis.url);
Expand Down
4 changes: 4 additions & 0 deletions discord/events/messageCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ module.exports = async (context, data) => {
}));
}

if (config.app.includeDiscordAuthorNameInIRCMessages) {
data.content = `[${data.author.username}] ${data.content}`;
}

await redisClient.publish(PREFIX, JSON.stringify({
type: 'irc:' + subType,
data: {
Expand Down
6 changes: 3 additions & 3 deletions discord/userCommands.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,18 +138,18 @@ resolver.__functions = {
}
};

resolver.init = async function () {
resolver.init = async function (isReload = false, oldScriptsContext) {
return Promise.all(Object.entries(resolver.__functions).map(async ([name, f]) => {
if (f.__init) {
try {
await f.__init();
await f.__init(isReload, oldScriptsContext);
console.info(`userCommand "${name}" initialized`);
} catch (e) {
console.error(`userCommand "${name}" __init failed!`, e);
} finally {
// only let it run once with the (usually correct) assumption that
// it will continue to fail if called repeatedly
delete f.__init();
delete f.__init(isReload, oldScriptsContext);
}
}
}));
Expand Down
4 changes: 1 addition & 3 deletions discord/userCommands/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ function f (context) {

if (toSend.subcommands) {
for (const [name, { header, text }] of Object.entries(toSend.subcommands)) {
toSendEmbed
.addField('Subcommand', '`' + name + '`')
.addField(text && header ? header : 'Usage', text);
toSendEmbed.addField('⁍ `' + name + '`' + (text && header ? ` _${header}_` : ''), text);
}
}

Expand Down
3 changes: 2 additions & 1 deletion discord/userCommands/reload.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ const { scopedRedisClient } = require('../../util');
module.exports = function (context, ...a) {
context.sendToBotChan('Reloading user commands...');
try {
const oldScriptsContext = require('../userCommands')('scripts').reloading();
require('../userCommands').__unresolve();
require('../userCommands').init()
require('../userCommands').init(true, oldScriptsContext)
.then(() =>
context.sendToBotChan(`Reloaded ${Object.keys(require('../userCommands').__functions).length} user commands`)
)
Expand Down
Loading

0 comments on commit 2ca8bc9

Please sign in to comment.