.jaqua.

i want a bot that is used for stock purposes, it will emebd news to a channel about stock news, or any news relaed to fiance, ie: oil, politics affecting fiances, etc

Kodari AI
KodariJun 23 at 7:13 am 10

Iโ€™ll create a financial news bot that automatically posts market updates to Discord.

FinanceNewsBot
A Discord bot that delivers automated financial market news and updates. Posts embedded news articles about stocks, commodities (oil, gold), economic policy, and political events affecting markets. Features scheduled news fetching from multiple sources, customizable news categories, and rich embed formatting. Includes commands to fetch specific market news on-demand and configure update intervals.

Finance News Bot created!

The bot includes:

  • Scheduled news posting every 2 hours (configurable)

  • On-demand commands for finance news, stock searches, and oil updates

  • NewsAPI integration for real-time financial news

  • Rich embeds with thumbnails, sources, and timestamps

Setup steps:

  1. Run npm install

  2. Copy .env.example to .env and add your Discord token and NewsAPI key

  3. Get a free NewsAPI key at https://newsapi.org/

  4. Set your channel ID in .env

  5. Run npm start

Commands: !financenews, !stocknews [keyword], !oilnews, !help

Kodari can make mistakes. Review important code before using.
Files
.gitignore
bot.js
config.json
embedBuilder.js
newsService.js
package.json
README.md
Code
7 400
bot.js
const { Client, GatewayIntentBits } = require('discord.js');
const cron = require('node-cron');
require('dotenv').config();

const NewsService = require('./newsService');
const NewsEmbedBuilder = require('./embedBuilder');
const config = require('./config.json');

const client = new Client({
    intents: [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildMessages,
        GatewayIntentBits.MessageContent
    ]
});

const newsService = new NewsService(process.env.NEWS_API_KEY);

client.once('ready', () => {
    console.log(`โœ… Bot is ready! Logged in as ${client.user.tag}`);
    console.log(`๐Ÿ“ฐ Monitoring financial news...`);
    
    scheduleNewsUpdates();
});

function scheduleNewsUpdates() {
    cron.schedule(config.updateInterval, async () => {
        console.log('๐Ÿ“Š Fetching latest financial news...');
        await postFinanceNews();
    });
    
    console.log(`โฐ Scheduled news updates with interval: ${config.updateInterval}`);
}

async function postFinanceNews() {
    const channelId = process.env.NEWS_CHANNEL_ID;
    const channel = await client.channels.fetch(channelId);
    
    if (!channel) {
        console.error('โŒ Could not find news channel');
        return;
    }

    try {
        const articles = await newsService.getFinanceNews(config.searchKeywords, config.newsLimit);
        
        if (articles.length === 0) {
            console.log('โš ๏ธ No news articles found');
            return;
        }

        const headerEmbed = NewsEmbedBuilder.createInfoEmbed(
            '๐Ÿ“ˆ Latest Finance & Market News',
            `Updated: ${new Date().toLocaleString()}`
        );
        
        await channel.send({ embeds: [headerEmbed] });

        for (const article of articles) {
            const embed = NewsEmbedBuilder.createNewsEmbed(article);
            await channel.send({ embeds: [embed] });
            await new Promise(resolve => setTimeout(resolve, 1000));
        }

        console.log(`โœ… Posted ${articles.length} news articles`);
    } catch (error) {
        console.error('โŒ Error posting news:', error.message);
    }
}

client.on('messageCreate', async (message) => {
    if (message.author.bot) return;

    if (message.content === '!financenews') {
        await message.reply('๐Ÿ“Š Fetching latest financial news...');
        
        const articles = await newsService.getFinanceNews(config.searchKeywords, config.newsLimit);
        
        if (articles.length === 0) {
            const errorEmbed = NewsEmbedBuilder.createErrorEmbed('No news articles found.');
            await message.channel.send({ embeds: [errorEmbed] });
            return;
        }

        for (const article of articles) {
            const embed = NewsEmbedBuilder.createNewsEmbed(article);
            await message.channel.send({ embeds: [embed] });
        }
    }

    if (message.content.startsWith('!stocknews ')) {
        const keyword = message.content.replace('!stocknews ', '').trim();
        
        if (!keyword) {
            await message.reply('โŒ Please provide a keyword. Example: `!stocknews tesla`');
            return;
        }

        await message.reply(`๐Ÿ“Š Searching news for: **${keyword}**...`);
        
        const articles = await newsService.getNewsByKeyword(keyword, 3);
        
        if (articles.length === 0) {
            const errorEmbed = NewsEmbedBuilder.createErrorEmbed(`No news found for "${keyword}"`);
            await message.channel.send({ embeds: [errorEmbed] });
            return;
        }

        for (const article of articles) {
            const embed = NewsEmbedBuilder.createNewsEmbed(article);
            await message.channel.send({ embeds: [embed] });
        }
    }

    if (message.content === '!oilnews') {
        await message.reply('๐Ÿ›ข๏ธ Fetching latest oil market news...');
        
        const articles = await newsService.getNewsByKeyword('oil prices', 3);
        
        if (articles.length === 0) {
            const errorEmbed = NewsEmbedBuilder.createErrorEmbed('No oil news found.');
            await message.channel.send({ embeds: [errorEmbed] });
            return;
        }

        for (const article of articles) {
            const embed = NewsEmbedBuilder.createNewsEmbed(article);
            await message.channel.send({ embeds: [embed] });
        }
    }

    if (message.content === '!help') {
        const helpEmbed = NewsEmbedBuilder.createInfoEmbed(
            '๐Ÿ“ฐ Finance News Bot - Commands',
            '**!financenews** - Get latest finance news\n' +
            '**!stocknews [keyword]** - Search news by keyword (e.g., `!stocknews tesla`)\n' +
            '**!oilnews** - Get latest oil market news\n' +
            '**!help** - Show this help message'
        );
        await message.channel.send({ embeds: [helpEmbed] });
    }
});

client.on('error', (error) => {
    console.error('โŒ Discord client error:', error);
});

client.login(process.env.DISCORD_TOKEN);