Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 2024-05-17 - O(N) lookup optimizations for Discord.js
**Learning:** Found two O(N) operations in bot.js and module Tipper scripts when looking up members or guild count.
**Action:** Replaced `bot.guilds.array().length` with `bot.guilds.size` (O(1)) and `message.guild.members.find('id', recipient)` with `message.guild.members.get(recipient)` (O(1)).
## 2024-06-27 - Optimize message token extraction to reduce array allocations
**Learning:** Parsing Discord messages using chained string methods like `.trim().split(' ').filter(...)` repeatedly creates and discards temporary arrays for every message, unnecessarily increasing memory pressure.
**Action:** Replaced `.trim().split(' ').filter(...)` in bot/bot.js and all `*Tipper.js` modules with regex-based token extraction `.match(/\S+/g) || []`. This natively finds non-whitespace token sequences, drastically reducing intermediate object creations per message event.
129 changes: 65 additions & 64 deletions bot/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,27 @@ config = config.get('bot');
var aliases;
// check if any aliases are defined
try {
var time = moment()
.tz('America/Los_Angeles')
.format('MM-DD-YYYY hh:mm a');
var time = moment().tz('America/Los_Angeles').format('MM-DD-YYYY hh:mm a');
aliases = require('./alias.json');
console.log('[' + time + ' PST][' + pm2Name + '] ' + Object.keys(aliases).length + ' aliases Loaded!');
console.log(
'[' +
time +
' PST][' +
pm2Name +
'] ' +
Object.keys(aliases).length +
' aliases Loaded!',
);
} catch (e) {
var time = moment()
.tz('America/Los_Angeles')
.format('MM-DD-YYYY hh:mm a');
var time = moment().tz('America/Los_Angeles').format('MM-DD-YYYY hh:mm a');
console.log('[' + time + ' PST][' + pm2Name + '] No aliases defined');
}
var commands = {};

var bot = new Discord.Client();

bot.on('ready', function() {
var time = moment()
.tz('America/Los_Angeles')
.format('MM-DD-YYYY hh:mm a');
bot.on('ready', function () {
var time = moment().tz('America/Los_Angeles').format('MM-DD-YYYY hh:mm a');
console.log(
'[' +
time +
Expand All @@ -40,21 +42,19 @@ bot.on('ready', function() {
bot.user.username +
'Logged in! Serving in ' +
bot.guilds.size + // ⚡ Bolt: O(1) property lookup vs O(N) array creation
' servers'
' servers',
);
bot.channels.get(logChannel).send(
'[' +
time +
' PST][' +
pm2Name +
'] ' +
bot.user.username +
'Logged in! Serving in ' +
bot.guilds.size + // ⚡ Bolt: O(1) property lookup vs O(N) array creation
' servers',
);
bot.channels
.get(logChannel)
.send(
'[' +
time +
' PST][' +
pm2Name +
'] ' +
bot.user.username +
'Logged in! Serving in ' +
bot.guilds.size + // ⚡ Bolt: O(1) property lookup vs O(N) array creation
' servers'
);
require('./plugins.js').init();
console.log(
'[' +
Expand All @@ -63,7 +63,7 @@ bot.on('ready', function() {
pm2Name +
'] type ' +
config.prefix +
'tiphelp in Discord for a commands list.'
'tiphelp in Discord for a commands list.',
);
bot.channels
.get(logChannel)
Expand All @@ -74,10 +74,19 @@ bot.on('ready', function() {
pm2Name +
'] type ' +
config.prefix +
'tiphelp in Discord for a commands list.'
'tiphelp in Discord for a commands list.',
);
bot.user.setActivity(config.prefix + 'Intialized!');
var text = ['tiprvn', 'tipdoge', 'tiplbc', 'tipufo', 'tipproton', 'tippxc', 'tipftc', 'tiphelp'];
var text = [
'tiprvn',
'tipdoge',
'tiplbc',
'tipufo',
'tipproton',
'tippxc',
'tipftc',
'tiphelp',
];
var counter = 0;
setInterval(change, 10000);

Expand All @@ -90,40 +99,32 @@ bot.on('ready', function() {
}
});

process.on('uncaughtException', err => {
var time = moment()
.tz('America/Los_Angeles')
.format('MM-DD-YYYY hh:mm a');
process.on('uncaughtException', (err) => {
var time = moment().tz('America/Los_Angeles').format('MM-DD-YYYY hh:mm a');
console.log('[' + time + ' PST][' + pm2Name + '] uncaughtException: ' + err);
bot.channels
.get(logChannel)
.send('[' + time + ' PST][' + pm2Name + '] uncaughtException: ' + err);
process.exit(1); //exit node.js with an error
});

process.on('unhandledRejection', err => {
var time = moment()
.tz('America/Los_Angeles')
.format('MM-DD-YYYY hh:mm a');
process.on('unhandledRejection', (err) => {
var time = moment().tz('America/Los_Angeles').format('MM-DD-YYYY hh:mm a');
console.log('[' + time + ' PST][' + pm2Name + '] unhandledRejection: ' + err);
bot.channels
.get(logChannel)
.send('[' + time + ' PST][' + pm2Name + '] unhandledRejection: ' + err);
process.exit(1); //exit node.js with an error
});

bot.on('disconnected', function() {
var time = moment()
.tz('America/Los_Angeles')
.format('MM-DD-YYYY hh:mm a');
bot.on('disconnected', function () {
var time = moment().tz('America/Los_Angeles').format('MM-DD-YYYY hh:mm a');
console.log('[' + time + ' PST][' + pm2Name + '] Disconnected!');
process.exit(1); //exit node.js with an error
});

bot.on('error', function(error) {
var time = moment()
.tz('America/Los_Angeles')
.format('MM-DD-YYYY hh:mm a');
bot.on('error', function (error) {
var time = moment().tz('America/Los_Angeles').format('MM-DD-YYYY hh:mm a');
console.log('[' + time + ' PST][' + pm2Name + '] error: ' + error);
process.exit(1); //exit node.js with an error
});
Expand All @@ -139,29 +140,33 @@ function checkMessageForCommand(msg, isEdit) {
) {
msg.author
.send('Please set your Discord Presence to Online to talk to the bot!')
.catch(function(error) {
.catch(function (error) {
msg.channel
.send(
msg.author +
', Please enable Direct Messages from server members to communicate fully with our bot, it is located in the user setting area under Privacy & Safety tab, select the option allow direct messages from server members'
', Please enable Direct Messages from server members to communicate fully with our bot, it is located in the user setting area under Privacy & Safety tab, select the option allow direct messages from server members',
)
.then(
msg.channel.send(
'Please set your Discord Presence to Online to talk to the Bot!'
)
'Please set your Discord Presence to Online to talk to the Bot!',
),
);
return;
});
}
var cmdTxt = msg.content.split(' ')[0].substring(config.prefix.length);
// ⚡ Bolt: Optimize token extraction to avoid array allocations (O(1) memory instead of O(N))
var cmdTxt = (msg.content.match(/\S+/g) || [''])[0].substring(
config.prefix.length,
);
var suffix = msg.content.substring(
cmdTxt.length + config.prefix.length + 1
cmdTxt.length + config.prefix.length + 1,
); //add one for the ! and one for the space
if (msg.isMentioned(bot.user)) {
try {
cmdTxt = msg.content.split(' ')[1];
// ⚡ Bolt: Optimize token extraction
cmdTxt = (msg.content.match(/\S+/g) || [''])[1];
suffix = msg.content.substring(
bot.user.mention().length + cmdTxt.length + config.prefix.length + 1
bot.user.mention().length + cmdTxt.length + config.prefix.length + 1,
);
} catch (e) {
//no command
Expand All @@ -182,7 +187,7 @@ function checkMessageForCommand(msg, isEdit) {
msg.content +
' from ' +
msg.author.username +
' as command'
' as command',
);
try {
cmd.process(bot, msg, suffix, isEdit);
Expand Down Expand Up @@ -214,37 +219,33 @@ function checkMessageForCommand(msg, isEdit) {
}
}

bot.on('message', msg => checkMessageForCommand(msg, false));
bot.on('message', (msg) => checkMessageForCommand(msg, false));

exports.addCommand = function(commandName, commandObject) {
exports.addCommand = function (commandName, commandObject) {
try {
commands[commandName] = commandObject;
} catch (err) {
var time = moment()
.tz('America/Los_Angeles')
.format('MM-DD-YYYY hh:mm a');
var time = moment().tz('America/Los_Angeles').format('MM-DD-YYYY hh:mm a');
console.log('[' + time + ' PST][' + pm2Name + '] Error addCommand: ' + err);
bot.channels
.get(logChannel)
.send('[' + time + ' PST][' + pm2Name + '] Error addCommand: ' + err);
}
};
exports.addCustomFunc = function(customFunc) {
exports.addCustomFunc = function (customFunc) {
try {
customFunc(bot);
} catch (err) {
var time = moment()
.tz('America/Los_Angeles')
.format('MM-DD-YYYY hh:mm a');
var time = moment().tz('America/Los_Angeles').format('MM-DD-YYYY hh:mm a');
console.log(
'[' + time + ' PST][' + pm2Name + '] Error addCustomFunc: ' + err
'[' + time + ' PST][' + pm2Name + '] Error addCustomFunc: ' + err,
);
bot.channels
.get(logChannel)
.send('[' + time + ' PST][' + pm2Name + '] Error addCustomFunc: ' + err);
}
};
exports.commandCount = function() {
exports.commandCount = function () {
return Object.keys(commands).length;
};

Expand Down
8 changes: 2 additions & 6 deletions bot/modules/dogeTipper.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,9 @@ exports.tipdoge = {
'**\n **!tipdoge** : Displays This Message\n **!tipdoge balance** : get your balance\n **!tipdoge deposit** : get address for your deposits\n **!tipdoge withdraw <ADDRESS> <AMOUNT>** : withdraw coins to specified address\n **!tipdoge <@user> <amount>** :mention a user with @ and then the amount to tip them\n **!tipdoge private <user> <amount>** : put private before Mentioning a user to tip them privately.\n\n has a default txfee of ' +
paytxfee,
process: async function (bot, msg, suffix) {
// ⚡ Bolt: Use .match(/\S+/g) to prevent unnecessary array allocations vs .trim().split(' ').filter(...)
let tipper = msg.author.id.replace('!', ''),
words = msg.content
.trim()
.split(' ')
.filter(function (n) {
return n !== '';
}),
words = msg.content.match(/\S+/g) || [],
subcommand = words.length >= 2 ? words[1] : 'help',
helpmsg =
'__**Dogecoin (DOGE) Tipper**__\nTransaction Fees: **' +
Expand Down
8 changes: 2 additions & 6 deletions bot/modules/exampleTipper.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,9 @@ exports.tipltc = {
description:
'__**Litecoin (LTC) Tipper**__\nTransaction Fees: **' + paytxfee + '**\n **!tipltc** : Displays This Message\n **!tipltc balance** : get your balance\n **!tipltc deposit** : get address for your deposits\n **!tipltc withdraw <ADDRESS> <AMOUNT>** : withdraw coins to specified address\n **!tipltc <@user> <amount>** :mention a user with @ and then the amount to tip them\n **!tipltc private <user> <amount>** : put private before Mentioning a user to tip them privately.\n\n has a default txfee of ' + paytxfee,
process: async function(bot, msg, suffix) {
// ⚡ Bolt: Use .match(/\S+/g) to prevent unnecessary array allocations vs .trim().split(' ').filter(...)
let tipper = msg.author.id.replace('!', ''),
words = msg.content
.trim()
.split(' ')
.filter(function(n) {
return n !== '';
}),
words = msg.content.match(/\S+/g) || [],
subcommand = words.length >= 2 ? words[1] : 'help',
helpmsg =
'__**Litecoin (LTC) Tipper**__\nTransaction Fees: **' + paytxfee + '**\n **!tipltc** : Displays This Message\n **!tipltc balance** : get your balance\n **!tipltc deposit** : get address for your deposits\n **!tipltc withdraw <ADDRESS> <AMOUNT>** : withdraw coins to specified address\n **!tipltc <@user> <amount>** :mention a user with @ and then the amount to tip them\n **!tipltc private <user> <amount>** : put private before Mentioning a user to tip them privately.\n\n **<> : Replace with appropriate value.**',
Expand Down
8 changes: 2 additions & 6 deletions bot/modules/ftcTipper.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,9 @@ exports.tipftc = {
'**\n **!tipftc** : Displays This Message\n **!tipftc balance** : get your balance\n **!tipftc deposit** : get address for your deposits\n **!tipftc withdraw <ADDRESS> <AMOUNT>** : withdraw coins to specified address\n **!tipftc <@user> <amount>** :mention a user with @ and then the amount to tip them\n **!tipftc private <user> <amount>** : put private before Mentioning a user to tip them privately.\n\n has a default txfee of ' +
paytxfee,
process: async function (bot, msg, suffix) {
// ⚡ Bolt: Use .match(/\S+/g) to prevent unnecessary array allocations vs .trim().split(' ').filter(...)
let tipper = msg.author.id.replace('!', ''),
words = msg.content
.trim()
.split(' ')
.filter(function (n) {
return n !== '';
}),
words = msg.content.match(/\S+/g) || [],
subcommand = words.length >= 2 ? words[1] : 'help',
helpmsg =
'__**Feathercoin (FTC) Tipper**__\nTransaction Fees: **' +
Expand Down
8 changes: 2 additions & 6 deletions bot/modules/lbcTipper.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,9 @@ exports.tiplbc = {
'**\n **!tiplbc** : Displays This Message\n **!tiplbc balance** : get your balance\n **!tiplbc deposit** : get address for your deposits\n **!tiplbc withdraw <ADDRESS> <AMOUNT>** : withdraw coins to specified address\n **!tiplbc <@user> <amount>** :mention a user with @ and then the amount to tip them\n **!tiplbc private <user> <amount>** : put private before Mentioning a user to tip them privately.\n\n has a default txfee of ' +
paytxfee,
process: async function (bot, msg, suffix) {
// ⚡ Bolt: Use .match(/\S+/g) to prevent unnecessary array allocations vs .trim().split(' ').filter(...)
let tipper = msg.author.id.replace('!', ''),
words = msg.content
.trim()
.split(' ')
.filter(function (n) {
return n !== '';
}),
words = msg.content.match(/\S+/g) || [],
subcommand = words.length >= 2 ? words[1] : 'help',
helpmsg =
'__**LBRY Credit (LBC) Tipper**__\nTransaction Fees: **' +
Expand Down
8 changes: 2 additions & 6 deletions bot/modules/protonTipper.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,9 @@ exports.tipproton = {
'**\n **!tipproton** : Displays This Message\n **!tipproton balance** : get your balance\n **!tipproton deposit** : get address for your deposits\n **!tipproton withdraw <ADDRESS> <AMOUNT>** : withdraw coins to specified address\n **!tipproton <@user> <amount>** :mention a user with @ and then the amount to tip them\n **!tipproton private <user> <amount>** : put private before Mentioning a user to tip them privately.\n\n has a default txfee of ' +
paytxfee,
process: async function (bot, msg, suffix) {
// ⚡ Bolt: Use .match(/\S+/g) to prevent unnecessary array allocations vs .trim().split(' ').filter(...)
let tipper = msg.author.id.replace('!', ''),
words = msg.content
.trim()
.split(' ')
.filter(function (n) {
return n !== '';
}),
words = msg.content.match(/\S+/g) || [],
subcommand = words.length >= 2 ? words[1] : 'help',
helpmsg =
'__**Proton (PROTON) Tipper**__\nTransaction Fees: **' +
Expand Down
8 changes: 2 additions & 6 deletions bot/modules/pxcTipper.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,9 @@ exports.tippxc = {
'**\n **!tippxc** : Displays This Message\n **!tippxc balance** : get your balance\n **!tippxc deposit** : get address for your deposits\n **!tippxc withdraw <ADDRESS> <AMOUNT>** : withdraw coins to specified address\n **!tippxc <@user> <amount>** :mention a user with @ and then the amount to tip them\n **!tippxc private <user> <amount>** : put private before Mentioning a user to tip them privately.\n\n has a default txfee of ' +
paytxfee,
process: async function (bot, msg, suffix) {
// ⚡ Bolt: Use .match(/\S+/g) to prevent unnecessary array allocations vs .trim().split(' ').filter(...)
let tipper = msg.author.id.replace('!', ''),
words = msg.content
.trim()
.split(' ')
.filter(function (n) {
return n !== '';
}),
words = msg.content.match(/\S+/g) || [],
subcommand = words.length >= 2 ? words[1] : 'help',
helpmsg =
'__**Phoenixcoin (PXC) Tipper**__\nTransaction Fees: **' +
Expand Down
8 changes: 2 additions & 6 deletions bot/modules/rvnTipper.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,9 @@ exports.tiprvn = {
'**\n **!tiprvn** : Displays This Message\n **!tiprvn balance** : get your balance\n **!tiprvn deposit** : get address for your deposits\n **!tiprvn withdraw <ADDRESS> <AMOUNT>** : withdraw coins to specified address\n **!tiprvn <@user> <amount>** :mention a user with @ and then the amount to tip them\n **!tiprvn private <user> <amount>** : put private before Mentioning a user to tip them privately.\n\n has a default txfee of ' +
paytxfee,
process: async function (bot, msg, suffix) {
// ⚡ Bolt: Use .match(/\S+/g) to prevent unnecessary array allocations vs .trim().split(' ').filter(...)
let tipper = msg.author.id.replace('!', ''),
words = msg.content
.trim()
.split(' ')
.filter(function (n) {
return n !== '';
}),
words = msg.content.match(/\S+/g) || [],
subcommand = words.length >= 2 ? words[1] : 'help',
helpmsg =
'__**Ravencoin (RVN) Tipper**__\nTransaction Fees: **' +
Expand Down
8 changes: 2 additions & 6 deletions bot/modules/ufoTipper.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,9 @@ exports.tipufo = {
'**\n **!tipufo** : Displays This Message\n **!tipufo balance** : get your balance\n **!tipufo deposit** : get address for your deposits\n **!tipufo withdraw <ADDRESS> <AMOUNT>** : withdraw coins to specified address\n **!tipufo <@user> <amount>** :mention a user with @ and then the amount to tip them\n **!tipufo private <user> <amount>** : put private before Mentioning a user to tip them privately.\n\n has a default txfee of ' +
paytxfee,
process: async function (bot, msg, suffix) {
// ⚡ Bolt: Use .match(/\S+/g) to prevent unnecessary array allocations vs .trim().split(' ').filter(...)
let tipper = msg.author.id.replace('!', ''),
words = msg.content
.trim()
.split(' ')
.filter(function (n) {
return n !== '';
}),
words = msg.content.match(/\S+/g) || [],
subcommand = words.length >= 2 ? words[1] : 'help',
helpmsg =
'__**Uniform Fiscal Object (UFO) Tipper**__\nTransaction Fees: **' +
Expand Down
8 changes: 2 additions & 6 deletions bot/modules/vtlTipper.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,9 @@ exports.tipvtl = {
'**\n **!tipvtl** : Displays This Message\n **!tipvtl balance** : get your balance\n **!tipvtl deposit** : get address for your deposits\n **!tipvtl withdraw <ADDRESS> <AMOUNT>** : withdraw coins to specified address\n **!tipvtl <@user> <amount>** :mention a user with @ and then the amount to tip them\n **!tipvtl private <user> <amount>** : put private before Mentioning a user to tip them privately.\n\n has a default txfee of ' +
paytxfee,
process: async function (bot, msg, suffix) {
// ⚡ Bolt: Use .match(/\S+/g) to prevent unnecessary array allocations vs .trim().split(' ').filter(...)
let tipper = msg.author.id.replace('!', ''),
words = msg.content
.trim()
.split(' ')
.filter(function (n) {
return n !== '';
}),
words = msg.content.match(/\S+/g) || [],
subcommand = words.length >= 2 ? words[1] : 'help',
helpmsg =
'__**Vertical (VTL) Tipper**__\nTransaction Fees: **' +
Expand Down