Discord Notification System Examples

This page demonstrates how to use the Discord notification system in your InvestAware application.

1. Basic Setup

First, include the DiscordNotifier utility and initialize it with your API key:

// Include the DiscordNotifier utility <script src="discordNotifier.js"></script> // Initialize the notifier const notifier = new DiscordNotifier('your-api-key-here', 'https://your-domain.com');

2. Simple Text Notifications

Send basic text notifications with different types:

// Info notification await notifier.sendText('User logged in successfully', 'info'); // Success notification await notifier.sendText('Payment processed successfully', 'success'); // Warning notification await notifier.sendText('API rate limit approaching', 'warning'); // Error notification await notifier.sendText('Database connection failed', 'error'); // Plain text (no embed) await notifier.sendText('Simple message', 'text');

3. User Action Notifications

Track user actions in your application:

// User creates a prediction await notifier.sendUserAction('Created prediction', 'AAPL - Bullish', 'success'); // User updates profile await notifier.sendUserAction('Updated profile', 'Changed email address', 'info'); // User cancels subscription await notifier.sendUserAction('Cancelled subscription', 'Premium plan', 'warning');

4. System Event Notifications

Monitor system events and processes:

// Daily price update completed await notifier.sendSystemEvent('Daily price update', 'Updated 500+ stocks', 'success'); // Batch prediction processing await notifier.sendSystemEvent('Batch prediction', 'Processed 100 predictions', 'info'); // System maintenance await notifier.sendSystemEvent('Maintenance', 'Database backup completed', 'info');

5. Prediction Notifications

Notify about new predictions and their accuracy:

// New prediction generated await notifier.sendPrediction('AAPL', 'Bullish prediction with 85% confidence', '85%'); // Prediction result await notifier.sendPrediction('TSLA', 'Prediction was correct! Stock moved 5.2% up', '100%');

6. Market Alert Notifications

Send alerts for significant market movements:

// Stock moved up significantly await notifier.sendAlert('AAPL', 5.2, 'up'); // Stock moved down significantly await notifier.sendAlert('TSLA', -3.8, 'down');

7. Subscription Notifications

Track subscription events:

// New subscription await notifier.sendSubscriptionEvent('created', 'Premium Plan', 'user@example.com'); // Subscription cancelled await notifier.sendSubscriptionEvent('cancelled', 'Basic Plan', 'user@example.com'); // Payment failed await notifier.sendSubscriptionEvent('payment_failed', 'Premium Plan', 'user@example.com');

8. Error Notifications

Send detailed error reports:

// Application error await notifier.sendError('Database connection timeout', 'User dashboard loading', 'Error stack trace here...'); // API error await notifier.sendError('Yahoo Finance API rate limit exceeded', 'Stock price fetching');

9. Custom Embed Notifications

Create custom Discord embeds for complex notifications:

const customEmbed = { title: '🎯 Portfolio Update', description: 'Your portfolio has been updated with new predictions', color: 0x9b59b6, fields: [ { name: 'Total Predictions', value: '25', inline: true }, { name: 'Average Accuracy', value: '87%', inline: true }, { name: 'Top Performer', value: 'AAPL (+12.5%)', inline: true } ], timestamp: new Date().toISOString() }; await notifier.sendEmbed('Portfolio analysis completed', customEmbed);

10. Integration Examples

How to integrate with existing code:

// In your prediction generation function async function generatePrediction(symbol) { try { // Your prediction logic here const prediction = await createPrediction(symbol); // Send notification await notifier.sendPrediction(symbol, prediction.description, prediction.accuracy); return prediction; } catch (error) { // Send error notification await notifier.sendError(error.message, 'Prediction generation', error.stack); throw error; } } // In your subscription webhook async function handleSubscriptionWebhook(event) { try { // Process subscription event await processSubscription(event); // Send notification await notifier.sendSubscriptionEvent( event.type, event.data.plan, event.data.customer.email ); } catch (error) { await notifier.sendError(error.message, 'Subscription webhook', error.stack); } }