From e6fea3c52b6b064241c54b4bce858ef2a7488ae9 Mon Sep 17 00:00:00 2001 From: daniel Date: Sun, 12 Apr 2020 00:28:25 -0700 Subject: [PATCH] (1.2.0) Error handling Added error handling for exec command, should prevent most crashes. Crashes from other commands should be fixed through code. Also added semicolons for consistency --- index.js | 55 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 30 insertions(+), 25 deletions(-) diff --git a/index.js b/index.js index 7617f3f..abcc760 100644 --- a/index.js +++ b/index.js @@ -3,18 +3,18 @@ // Call in dependencies / set up global variables const { Client, MessageEmbed } = require('discord.js'); const client = new Client(); -const version = '1.1.9'; +const version = '1.2.0'; var token = process.argv[2]; // Bootup sequence client.on('ready', () => { - console.log('╔════════════════════════════════════╗') - console.log('║ ║') - console.log('║ Tweeter by cyckl ║') - console.log('║ Running version ' + version + ' ║') - console.log('║ RIP 3-inch-largest ║') - console.log('║ ║') - console.log('╚════════════════════════════════════╝\n') + console.log('╔════════════════════════════════════╗'); + console.log('║ ║'); + console.log('║ Tweeter by cyckl ║'); + console.log('║ Running version ' + version + ' ║'); + console.log('║ RIP 3-inch-largest ║'); + console.log('║ ║'); + console.log('╚════════════════════════════════════╝\n'); client.user.setActivity('.t msg'); }); @@ -33,20 +33,16 @@ function getRandomInt(x) { // Commands function tweet(message) { // Handles removing nickname if no nickname - if (message.member.nickname == null) { - var tweetUserString = message.author.username - } - else { - var tweetUserString = message.member.nickname + ' (@' + message.author.username + ')' - } + if (message.member.nickname == null) tweetUserString = message.author.username; + else tweetUserString = message.member.nickname + ' (@' + message.author.username + ')'; - var content = message.content.replace('.t ', '') + var content = message.content.replace('.t ', ''); var embed = new MessageEmbed() .setAuthor(tweetUserString, message.author.displayAvatarURL(), 'https://www.youtube.com/watch?v=dQw4w9WgXcQ') .setColor(0x1DA1F2) .setDescription(content) - .addField('Retweets', getRandomInt(10000), true) - .addField('Likes', getRandomInt(5000000), true) + .addField('Retweets', getRandomInt(50000), true) + .addField('Likes', getRandomInt(1000000), true) .setFooter('Twitter', 'https://abs.twimg.com/icons/apple-touch-icon-192x192.png'); // Send tweet and log message.channel.send(embed); @@ -56,13 +52,22 @@ function tweet(message) { function exec(message) { // User ID hardcode if (message.author.id != '248948821881520138' && message.author.id != '249052912762880000') return; - var runCode = message.content.replace('.run ', ''); - var result = ''; - result = eval(runCode); - // Send result and log - message.channel.send('**Result:** ```js\n' + result + '```'); - console.log('[Exec] (' + message.author.tag + ') ' + runCode); - console.log('[Exec Result] ' + result); + + // Try code, if broken, move to catch() + try { + var runCode = message.content.replace('.run ', ''); + var result = eval(runCode); + // Send result and log + message.channel.send('**Result:** ```js\n' + result + '```'); + console.log('[Exec] (' + message.author.tag + ') ' + runCode); + console.log('[Exec Result] ' + result); + } + + // If error, handle it instead of crashing. + catch (e) { + console.log('[Exec Error] ' + e); + message.channel.send('**Error:** ```js\n' + e + '```'); + } } function about(message) { @@ -74,7 +79,7 @@ function about(message) { .addField('Version', version, true) .addField('Build date', '2020-04-11', true) .setFooter('This is alpha software. Please be patient!') - .setThumbnail('https://github.com/cyckl/tweeter/raw/master/img/tweeter.png') + .setThumbnail('https://github.com/cyckl/tweeter/raw/master/img/tweeter.png'); // Send tweet and log message.channel.send(embed); console.log('[About] (' + message.author.tag + ') ' + 'About dialogue triggered.');