Building a Discord bot using Node.js

Discussion in 'Ask any question!' started by Stockaholic, Jun 16, 2018.

  1. Stockaholic

    Stockaholic Content Manager

    Joined:
    Mar 29, 2016
    Messages:
    13,767
    Likes Received:
    7,050
    So, I wasn't sure if posting this thread in here would be worth it, but what the hey might as well give it a try...I never know who is lurking out there who might be in the know and would be able to chime in here to help out.

    Anyway, I'm not sure how many of you guys actively participate on our Stockaholics Discord Chatroom (aside from @JerryM)? Lately I've been needed in there as we had recently lost our charting bot for the channel.

    I'm essentially attempting to remake this lost bot and have it self hosted locally.

    Node.js isn't completely foreign to me. I've actually successfully built and run a discord charting bot from scratch here. That being said, I in no way would consider myself any kind of real "coder" if you will. :p

    I know enough to get the hang of it. But, there is still a ton out there that I just don't know about.

    For example, I know how to scrape charts from a site like Finviz.com and have the bot post them to the Discord room on a given command. Ex., if someone posted $AAPL into the chat, the bot would post up the daily chart of aapl from finviz.

    Instead, what I'm needing help with is having the bot pull some text from a website and have it posted to the room.

    So, for example if someone posted !nexter AAPL into the chatroom, the bot would pull the text from the earnings site and have it posted into the channel.

    Is this confusing you? I don't blame you. Some of this techie shit is way over my head sometimes.

    Anyway, I've been spending many hours reading up and watching how-to vids on YT and just flat out getting my hands on some of this. Testing functionality out, etc. Will continue looking around and see what I can find.

    But, in the meantime I just thought I would post this in here in case someone out there happened to have any experience with coding discord bots (particularly in javascript) and could maybe lend a little helping hand here or perhaps direct me to some resources that I may have missed in my searches.

    EDIT: Oh and BTW, I probably should post up the link to the bot that I am attempting to reproduce if you will.

    https://github.com/GoldenChrysus/rstocksbot

    This is going to be quite the project for me, but I feel like I'm up to the task.

    If all goes well, I may even decide to open source my bot at some point in the future for others to use.

    Appreciate any help with this from anyone in the advance! ;)

    EDIT #2: Just to clarify in here, I need to self host the bot! I already know that there are public charting bots I can use. But, that's not what I'm after here.

    I know how to host, just needing some help with the code.
     
  2. Gray Wolf

    Gray Wolf Well-Known Member

    Joined:
    Apr 3, 2016
    Messages:
    728
    Likes Received:
    398
    Cy have you talked with Andrew #6440 on the Discord channel? He had a bot at one time and I think he coded it but not positive. I'll check around and see what I can come up with for a helper.
     
  3. Stockaholic

    Stockaholic Content Manager

    Joined:
    Mar 29, 2016
    Messages:
    13,767
    Likes Received:
    7,050
    Hey @JerryM appreciate you taking the time to chime in here.

    No, I haven't seen or heard from Andrew in super long time. Not really sure what happened to him? I had assumed he disappeared from Discord or something as his bot went offline recently as well.

    Anyway, I'm not too sure he would be much help since I'm looking to self host the bot. Pretty sure he runs his bot off of Python, whereas I need the bot to be coded in JavaScript...and anyway I'm really doubtful he would even share his code if was in javascript. I wouldn't blame him though as I probably would do the same as well. :p

    Anyhow, I was fortunate enough to get in contact with the guy who developed this bot here-
    https://github.com/GoldenChrysus/rstocksbot

    He gave me this piece of code and I have been attempting to play around with it w/o much luck as of yet.

    Code:
    win.loadURL("http://finviz.com/map.ashx?t=" + finvizType);
    
    if (win.webContents) {
        win.webContents.once("did-finish-load", function() {
            win.webContents.executeJavaScript(
                "(function() { return document.getElementsByClassName('chart')[0].toDataURL(); })();",
                false,
                function(result) {
                    var image = new Buffer(result.replace("data:image/png;base64,", ""), "base64");
    
                    msg.channel.sendFile(image, "map.png");
    
                    win = null;
                }
            )
        });
    } else {
        win = null;
    
        msg.channel.sendMessage("Error fetching map.");
    }
    This code would essentially pull the current market heat map off of Finviz and post it into the Discord channel. Would look like this-

    [​IMG]

    It uses the module called "electron". I've installed the module also included it into my bot index.js file however when I attempt the run the script via node (npm start), it doesn't do anything. I don't get any script errors or nothing, but there is no response from the bot.

    I gotta think I am missing something in my script.

    I'll see if I can get more out of GoldenChrysus this week (that is if he is willing to share more of his code lol).

    I would also like to add a command that would pull upcoming earnings reports into the bot as well. I haven't the time to investigate into that one yet though!

    Would really love to get this Finviz market heat map function working first!

    If anyone has any pointers on how this could be achieved I'd greatly appreciate it!

    Anyway, the charts seem to be pulling up just fine. No issues there. I'm gonna work on this heat map stuff until I've finally got it nailed down! :D
     
  4. Stockaholic

    Stockaholic Content Manager

    Joined:
    Mar 29, 2016
    Messages:
    13,767
    Likes Received:
    7,050
    whelp, i pretty much gave up on that heat map code for the bot...had worked on it for several days but to no avail. pretty disappointed. :(

    i'm not leaving it off for good because i really love the idea of having a command for posting the market heat map into the channel, but i'll get back to it at a later time. right now i'd like to focus my efforts on another code and see if i can get a handle on that.

    i'm now looking into creating a command that would fetch/retrieve data from websites and have it posted into the channel.

    here is an example of what i'm aiming for here.

    someone types in !calendar into the discord channel and the bot spits something like this out-

    [​IMG]

    i think that looks absolutely amazing! i haven't dug deep into this one yet. but, if anyone has any ideas how one would achieve something like this let me know.

    perhaps there is already a bot on github that would make this process a bit easier? :p

    anyway, going to try spending some time over the coming days/weeks ahead and see what i can manage.

    this is going to be quite the project for me!
     
    OldFart likes this.
  5. woodywoodpicker

    woodywoodpicker New Member

    Joined:
    Aug 24, 2018
    Messages:
    2
    Likes Received:
    0
    @bigbear0083 thanks for maintaining this really useful bot. Is the code opened?
     
  6. Stockaholic

    Stockaholic Content Manager

    Joined:
    Mar 29, 2016
    Messages:
    13,767
    Likes Received:
    7,050
    hey @woodywoodpicker sorry but no my code is not open source at the moment...maybe at some point in the future i will throw the repo onto github, but not as of now.

    it took me a good long month to finally nail down much of the code for the bots on our discord server. albeit, i still never got to figure out how to get the market heatmap stuff in there which admittedly i'm not too happy about...but i haven't completely given up on it yet but for the time being i have taken a bit of a break from all that coding :p
     
  7. woodywoodpicker

    woodywoodpicker New Member

    Joined:
    Aug 24, 2018
    Messages:
    2
    Likes Received:
    0
    @bigbear0083 ok I understand. I may be able to help with heatmap stuff if I have access to the code. Let me know if you change idea.
     

Share This Page