Creating Discord Token Price & Countdown Timer bots that update every minute, with free hosting.
Replit url: https://replit.com/@MitchSchwartz/Rai...
Using Python, Replit, cronjob.org, & uptime robot, you can create and host discord (or other) bots for free.
See parts 1 & 2:https://www.youtube.com/watch?v=j-GJc...https://www.youtube.com/watch?v=ddiy2...
Transcript
Hey, welcome back, everyone. All right. So as you might have seen from the last video at the time of recording, I didn't fully understand funny enough, the script that I had created, but I pushed it a little bit further and figured, Hey, I, I can not only figure this out, but you know, Integra was able to do updates every hour.
And I mean, it could actually do every five minutes, but that would get really expensive. So how could we make a bot that updates every minute now? One of the projects was to create a countdown timer bot that resets at the same time each week. And so in order to do that, yeah, having a timer that updates every half an hour is not as good as a timer, that updates every minute.
And then the second I figured that out. Well, why wouldn't we just take that and apply it to the token update bots as well, and then have minute by minute pricing. And let me. I guess it would've been a good idea to start with this, but let me pull it up now and show you the end results of all three bots.
So here, take a look. If we switch over discord, which , let's see what page it loads on. Maybe I'll edit this out. Maybe I won. All right. So hopefully my face isn't covering these, but you can see the three bots over here. So this is the timer bot. These are the token bots and they do update every minute.
So now let us switch back over. Tolet where I'm still hosting this. Now relet rather explicitly does mention when you started up, this is not a production server. Now I'm only running these bots on a test server and in one. Other discord server. So I feel I'll get away with it for now. Soon. I will probably migrate it to somewhere a little bit more permanent, like our Pragma flow servers for now.
Well, here it is. So, so let's just jump in. So we've made a couple of updates to the main function. And I don't think I will start with that quite yet. So let's number one, just jump into the updates that we made to the token bot. So you can see I've done a little bit of splitting out the code.
And right here we have, and I've already gotten some really great suggestions on how this can be refactored and I, I, I just haven't done it yet. So I think I'll do that in the next version. But basically, so what we're doing here is we wrote a function to make the call to Nomics, to get the token price every minute.
So basically what I did, Inma in the first video I've now just migrated over here. So it took a little bit of effort. Here is a kind of error handling that I haven't figured out yet. So I just commented out. But basically we define the call. We send it off. We check if it's okay. And if not, we raise the exception and I'll make all this code actually available.
And then what we're doing is So, because I don't actually know what order Nomics was gonna send me the results in. I thought I would know that, but it didn't stay consistent. So I just put some basic conditions here. Now I'd probably do this with a loop if it was a bit more elaborate. And I started trying that since I couldn't get it going.
And since the point is really to deliver. and just get to that next step. What I decided was an if function would do just as well because again, I know the two tokens that I'm looking for and it's pretty straightforward and, you know, I don't need to scale it up in a crazy way. So here, if you know, the first entry is sorry, if the currency name of the first entry is equal to Orum Orum two.
Then assign that to the variable AP and otherwise, if the currency is equal to rate, then we'll assign it to the other. So once we've looped through the two responses that we're gonna get. You know, basically by the end of it, I have my two values assigned and then we do a little formatting here.
I think there's probably a better way I could do that. And then, you know, just a bunch of diagnostics and, and so I added in these line breaks here when I print, just cuz when it, when it's all a blob of text, I have a lot of trouble reading it. It, maybe it's a little bit of an add thing, but I, I find like the structure and visual structure things really matters to me.
So I just added these line breaks in everywhere to make it a bit easier to read. And I also added in these that three chevrons, because again, it just made it easier for my eye to see, you know, what's happening and, and pick out the parts that I've intentionally printed. So here we go. I update. I call this function bot name update, which we'll take a look at in a second.
And I pass it, the variables you know, this is what the nickname is gonna be. This is the token, and this is the server I'm going to, so let's take a look at update bot name. Now, so we have the new name that we wanna send. We have the token name that we're updating sorry, the token name. So that's the token for the bot so that we can authenticate and then we have the server that I want update to.
So here, I said, if the server explicitly, if I've defined it as the crypto Raider server otherwise the default or fallback is my test server. It felt cautious to have things fall back to the test server rather than the other way around. And then here, so we have the authentication, which is the string bot plus the token, which you would put those in the secrets environmental variables there, and then just reference it using OS Byron dot get.
And then the string version of the token name. And of course you have to import OS in order to use them to send the requests. And this is on both pages, you do need to import requests. So those are the libraries that are being used. All right. So this is the, the, you know, the call out to the bot and we update the name.
Over here and then we just check to make sure that we got a 200 success code and we print out the response and that's just again for debugging. So that is how the. Token. Yeah, that is how the token updating is happening. And we can take a look at the timer bot as well and see how they're all using the same call out function.
So now if we take a look, I've imported a few things, date, time date parser, although I've seen versions that don't use date Parr and actually date. Parr's really cool. Cuz it'll do kind of a plain language. Like I actually wrote. You can see it down here, midnight CST, and then it parses that I was originally saying Thursday, cuz that's sorry, Wednesday.
Cause that's when the reset was, but it was like not picking the right Wednesday. It was either picking the Wednesday in the past and, and it wasn't very reliable. So I'll probably work that out at PYT Z helps us do time zone. And then of course I'm importing my update bot name function. So the timer update function what it's gonna do is it's defining CST.
It's getting the time now in CST because this, this is going to reset at midnight on Wednesday in CST. And it just often the advice is to work, you know, do the time zone conversion last. I tried that in this case, especially cuz we're exactly on midnight. It just was so much easier to start in CST and stay there.
So moving along here what is next? Oh yeah. This is a little thing I put in. I wanted to test the changeover. Cause the trickiest part is when the timer hits zero, it needs to reset to seven days. And rather than waiting and being able to test once a week, I just made it so that I could see, Hey, if the day is tomorrow then I should see zero days and then this lets me swap basically my variable you know, tomorrow equals now.
One day. So basically I'm changing my time from now to tomorrow. I'm basically making the system think that it's tomorrow. So I can see what will happen tomorrow. Hope that made sense. It was a little wordy maybe. Okay. Moving on. So yeah, we want to find the next reset date, right? So that's where I'm using date Parr, pars, midnight CST.
So that's giving me that value and again, a lot of just debugging and printing here because. This was really tricky. Date math is frankly, a pain in the ass. And so with that, I just really needed to cut as many kind of holes into this black box as I could. So then this is really simple time till reset equals find next reset minus now.
So that's the difference in the days? So that, so the time is working, but the day wasn't really working in that. And so what I just did is I decided to figure out the date logic separately, which, which felt like the right call. So here, if there's you know, because the weekday that I'm resetting on is Wednesday, which is two.
So I need to handle things differently if we're before Wednesday or if we're after Wednesday now I got a, a really awesome code. And what was suggested, there was basically first doing this math and then saying if the day is less than zero, add. So that's a different way of doing it. I tried to add that in, but I had to change too many things and I just didn't get back to it yet, but definitely, definitely something I want to add in although this is working really reliably anyways.
And then yeah, we just compose the timer string. So this is ultimately the name of the bot. So our days left variable assigned here, plus the. Time till reset dot seconds. This is divided by 360, but the double slash gets rid of the remainder. And then again, over here, we're dealing with the, the remainder from that using the modular function and just putting this all together.
So we print out the timer string. And we do the bot name reset. So we send it to the crypto Raiders server and we send it to the test server. Whew. Right. That's a whole lot of stuff. And we're not even done. So there's still a couple of other pieces. So if we go to endpoints.py this is, I dunno if this was called server originally or something like that, but I renamed it to endpoint because essentially that's what it's doing.
I'm not using this guy down here, but the app route, just whenever this is pinged to the route URL, it's gonna run the following. So it's running the home function. And that is going to print running home. Then it's gonna run the timer update function. Then it's gonna run the get token values function.
Right? So if we, we do a run through of this, every time it gets pinged, it's going to go into timer, update, run all of this math, and then it's gonna call bot name update twice, and then it's gonna go. Call get token values. And so it's gonna run through all of this and it's gonna call bot name updates for each token for each server.
Now there's definitely ways I could loop through this instead. And, and definitely that would be something worth doing in the future. Okay. Finally, if we go to the main function what's happening when we boot this up is first of all already, it's saying I'm in. So it's just giving that confirmation.
It's also over here. Getting the token and, and running the timer bot. It's doing client.run. It's also creating the new client instance at the top. Now this is where I'm still a little bit unclear on exactly how this works, because I don't know the discord dot PPY library that well, in fact, I largely haven't used it because it's asynchronous Python and it's just a little bit outta reach.
Still as you see, I actually started this in Integra, so I'm pretty proud of where we got to already. The, what I've noticed is the client.run. That seems to be pretty important for keeping the bot online. When I commented that out, it stopped working. I was a little bit surprised because the thing that's getting pinged over here is you know, as I showed in the other video, we set up a recurring call with uptime robot though.
I'll show you. We actually here, I. CR job.org so I can get pinged every minute. And also I'm using. Alongside Chrome job.org, I'm using uptime robot. And so what that means is there's two services pinging this and keeping it alive. And so that's prevented it from going down, whereas on uptime, robot alone this was going down every day or two, and then, you know, I'd have to run in and just hit the start button.
So again, taking a look here when we run these functions, there's actually nothing here. I'm not sure what actually keeps the bottle alive. What I noticed Is that. So I showed the, you know, in part two, I showed you how I was keeping the Raider bot alive and how I was keeping the Orum bot alive.
And those. Servers are actually still running just to keep the bots online. I haven't figured out how to combine all of that into this one routine. So all the updates are coming through here and if the bot goes offline, you'll still see the name update. The problem is that the bot will drop to the bottom of the membership list.
So it becomes much less useful. So let's just head over to Chron jobb.org.
And we can see right here. So I have these pointed at all three servers. It's pretty easy to create a CR job and you can set it for every minute. So once you do that, you have your minute by minute updates. And again, using uptime robot as a fallback is a pretty good idea. It's worked out well. The good thing about it, Is that if the server stops responding for three minutes, for whatever reason say relet is overloaded or, or there's some other kind of thing going on then CRO jobb.org is gonna switch.
So that's where it's nice to have uptime robot running at either every five minutes or at this point, you, you know, every half an hour, just so that there's kind of a long wave like a lower frequency ping going. That's less likely to get shut down and then it'll give you the opportunity to put Chron Jobb back up before they both go down.
And for all this effort about keeping the server alive what's kind of maybe funny and silly is that. You can just upgrade and, and the always on thing, it costs five bucks a month. So I'm doing this more out of pride than anything else, because I mean, the amount of time I've spent saving $5 a month is, is much more than that.
But you know, it's just fun to figure out how to do things for free and have that option. So yeah, this is it. This is the next evolution of the discord bots. This is how to. Bots that update every minute for free which is pretty cool. And in some upcoming videos, I think I'm gonna start to make bots with actions.
So we'll show how to handle those, but I'm pretty sure we can create new app routes here. Basically construct our API. And, you know, start pinging different commands to these different routes and then writing the custom functions for them. So I'm almost certain, that's how it works. Maybe there's another way, but I guess we'll find out soon and hopefully the next video won't start out with, oh yeah, I guess I was wrong in that last one.
So here anyways either way it'll show I learned something. So we're done here. Thank you guys for watching. I hope you learned something. I'll share this in the links and remember to subscribe and give a likes and a comment. If you have any questions, happy to answer them. And we'll be coming out with more of these if you're finding them useful.
So thank you very much.