Jump to content

Discord.js: How to make a message happen randomly, how to make an active timer in an embed, and more

SUPR3ME

I am working on a discord bot and just started 2 days ago. I've done all the basic stuff and set up some basic commands, but now i'm getting to the main part of my bot and i'm finding myself helpless. What do I do??? I want my bot to randomly send out an embed every week or so in a channel where people are actively talking. The embed has a timer on it, and anyone who responds with a certain message before the timer ends joins a giveaway of sorts in which the winner receives a certain role. The winner of the giveaway keeps the role until the next giveaway, and so on. I have no idea how to do anything, but for starters, can someone tell me how to make a timer that makes the embed disappear when it's done, and to actively update the timer as it ticks down on the embed? I can go from there. Or, if someone can fully tell me how to execute all this, it would be appreciated.

Link to comment
Share on other sites

Link to post
Share on other sites

Personally I would generate giveaways in and store them in a database and then create a script that will load these and post as needed and close as needed then use a CRON job to run this script.


your bot can then update who entered the competition and the CRON can post who won when its complete. 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

Okay, first piece of advice...NEVER tie timers to the process if it's crucial like a giveaway. If your bot restarts then the timer is screwed. Have some of timestamp saved with the giveaway information somewhere. That could be a database, file, something. Then you can save the embed's message id and tie it with that. That way you can edit it at any time based on the ID of the message. After that, you can just have timers internally that check the messages or you can have a CRON job that checks timestamps and if something is ready to be cleared then it will clear it. You can do the same for posting. Just check every so many minutes for a given timestamp (you can use MomentJS for this) or just use CRON job (my preference).

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, MatthewSH said:

NEVER tie timers to the process if it's crucial like a giveaway

this response if from my experience with discord.py, not js 
 

I learned this one the hard way. 
 

I also tried using time.sleep() and it didn’t let me use any command until the timer ended 

✧・゚: *✧・゚:*  Quote for a reply  *:・゚✧*:・゚✧

 

✧・゚: *✧・゚:*   Ask for discord   *:・゚✧*:・゚✧

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, iLostMyXbox21 said:

I also tried using time.sleep() and it didn’t let me use any command until the timer ended 

That's cause sleep is a blocking function. It won't let anything else run until it finished. In JS setInterval is not blocking like any pause/sleep/wait functions. That's where you went wrong with that one, but hey. We all learn. I used to make so same exact mistakes years ago when I first started. Timers are tricky things to do for a lot of people. That's why I always try to default to timestamps and logic to determine if the item should be handled or not.

Link to comment
Share on other sites

Link to post
Share on other sites

35 minutes ago, MatthewSH said:

That's cause sleep is a blocking function. It won't let anything else run until it finished.

unless you wrote an event loop in python then it wouldn't be blocking. This is one reason i both love and hate nodeJS a async operations are great and also annoying.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

  • 1 month later...

this can be done, but preferably not, coz if your bot gets activated for no reason, like no mesage, no webhooks there is a chance it will be reported for spamming and get blocked, if its for personal level use, if the bot is in more than one server, heres a little workaround, just make it send a message to other server whenever a message is recieved in this server with like 25% chance of message to otheer server

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×