Discord.JS Giveaway Timer - timer

I am trying to make a giveaway system in Discord.JS but am having trouble on setting the timer for it. I would use a setTimeout function or component collector but the problem with that is when the bot turns off, and turns back on again the timer fails and stops. Some packages make it so if the bot is turned back on, the timer resumes, which is what I want to do. Can someone help me with this?
I tried doing this:
setTimeout(() => { // code })

To answer the question proposed in the comment thread on the question "Ok, thank you! One more thing, I am using the MS package to convert the users input to miliseconds, how can I check if the time is up? Or will it be a timestamp? Then how would I do it?"
All a unix timestamp is is an integer, which you can check like so:
const over = endDate <= Date.now();

Related

Nextcord: I want to mention the sender of the slash command I made. How would I go about that?

I'm trying to make this command notify my admins of a bot test and need it to mention the user who called the command. How would I go about that? I don't fully understand how to get that information with slash commands.
#client.slash_command(name= "test", description="(For Deputies or Leader only) Checks the operational state of the client.", guild_ids=[806043206030589952])
#has_any_role(leader_id, deputy_id)
async def test(interaction:Interaction):
bot_log = channel_up(940125176723554394)
await bot_log.send(f'<#&806045254834847776>,{} has started diagnostics for the bot. Please ignore any possible disturbances for the next minute or so.')
Thanks in advance for the advice, it's the first discord bot I've ever created.
EDIT:
In documentation, I found the solution. I have to use interaction.user.mention to get it to mention the user who sent the command. Or at least in theory, I'm dealing with a different issue now. Hopefully this helps people who also were as confused as me out.
You can use the interaction.user.name function with an # at the beginning.
The code should look something like this:
await interaction.response.send_message("#"+str(interaction.user.name))

What is a Timer ID?

I feel like this is a dumb question. But I've been struggling to understand some concepts and examples because they implement a timer ID in them and I don't really get what it is. Is it just a variable you assign a timer to?
Assuming JavaScript because of the "settimeout" tag you added, setTimeout and setInterval return an ID if they succeeded in being setup. This ID can be used to refer to that timeout or interval. Generally, this is used to cancel a timeout before it finishes, or stop an interval from running again. You can do this with the clearTimeout and clearInterval functions.
More information: https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout
In the future, you can find information like this very easily with a search engine.

Is it possible to detect the removal of a reaction?

I know you can detect the addition of a new reaction but using a ReactionCollector. But is it possible to detect the removal of a reaction?
I know i could keeping track of all reactions in a DB, and continuously poll the discord api and compare the DB version to the live version. But I'd rather not do this if there's a better solution out there.
Yes, the discord.js API has an event you can listen for: messageReactionRemove. You have to maintain a message cache, because it only fires for messages in your cache. You fill out your cache using fetchMessages.
You can make a event for this, called 'messageReactionRemove' to see if someone removes a reaction.
This is a simple example for a reaction role bot i have:
const events = {
MESSAGE_REACTION_REMOVE: 'messageReactionRemove',
};
Then you can make for instance an if statement, and ask for the MESSAGE_REACTION_REMOVE
if (event.t === "MESSAGE_REACTION_REMOVE") member.addRole(guildRole.id);

How to automate topic posting and to clear the chat before hand

I am trying to use discord.js to post a topic at 3 different intervals: every day, every week and every month.
I will have a different file with the topics for each day. I also want each chat room for the separate topics to clear before posting the new topic.
I am using the code below to automate the interval at which it posts but I need the clear function and also maybe a more streamlined way to post it, or a way to post at a specific time.
bot.on('message', function(message) {
// Now, you can use the message variable inside
if (message.content === "$Next") {
var interval = setInterval(function() {
// use the message's channel (TextChannel) to send a new message
message.channel.send("Test Topic")
.catch(console.error); // add error handling here
}, 1 * 10);
}
});
I need to automate these functions and to clear the chat before it posts the new topic. My current code works for automatically posting at the given interval. I need help clearing the channels chat and automatically posting a topic from a saved file.
Use the node-schedule package to help with the defined intervals or specific time.
As for pulling the topic from a file, please take a look at Node's native fs module.

Multiple Instance of AudioStreamer if tap too fast in tableview

I'm having issues with the AudioStreamer which is driving me crazy. I have a tableview with songs from a remote server. When I click on a song it stream fine, but if I tap too fast I get multiple instance of AudioStreamer playing different songs.
I heard that making AudioStreamer a singleton will fix this issue, but I tried that and it crashes too often. Does anybody had faced this issue and founded a solution?
Thanks.
UPDATED:
The solution to this problem was to remove this line from the -(void)stop method. Also change anything that says state to self.state
if (state == AS_WAITING_FOR_DATA || state == AS_STARTING_FILE_THREAD)
return;
The solution to this problem was to remove this line from the -(void)stop method. Also change anything that says state to self.state
if (state == AS_WAITING_FOR_DATA || state == AS_STARTING_FILE_THREAD)
return;
I founded the solution in another post. But just in case, here is what I did wrong.
Another post here at stackoverflow pointed to the following link. I follow the solution in that link before but not completely. I missed the most important part which was below the code.
https://github.com/mattgallagher/AudioStreamer/issues/26
"The variable pausedByInterruption is needed since we only want to resume playing after an interruption iff audio was paused by an interruption. So, the ivar pausedByInterruption needs to be set to NO in the following methods: pause, start, stop and initWithUrl."
Let me repeated, make sure that you do this part "the ivar pausedByInterruption needs to be set to NO in the following methods: pause, start, stop and initWithUrl."
After doing this, my issue was resolved.

Resources