I am using Discord.js Node V12 I am currently trying to find out how to say time elapsed in the status to show how long the bot has been playing game or any activity. But i cannot find anyone who has asked or answered any of these questions.
#client.event
async def on_connect():
await client.change_presence(status=discord.Status.dnd,activity = discord.Game(name = "VALORANT"))
I would like to break this answer into a few significant points:
• The sample code provided is from discord.py ( a discontinued python based library to interact with the API ) which is totally out of context of the whole question itself since you're asking it for discord.js
• There is no actual way to find the time elapsed since a particular activity as of now but you may resort to this:
var uptime = client.uptime; // returns uptime in milliseconds
var hours = uptime / 1000 / 60 / 60 // converts it to hours
/*
Then you may apply the following approach to change your status every hour passes
*/
setInterval(() => {
client.user.setActivity(`Valorant since ${hours} hour(s)`);
}, 3600000); // this would show up as Playing Valorant since 1 hour(s) and subsequently would change every hour if your bot isn't being restarted continuously.
I took a look at the discord.js documentation to examine setting activities and found no such information about timestamps. However, there was a section in the ActivityType that led me to the discord developers documentation and it indicates:
Bots are only able to send name, type, and optionally url.
So it doesn't seem as though you will be able to set the start or end timestamps for an activity as a bot.
Related
I'm making a command .report #user that creates a simple poll with 2 buttons added "agree" and "disagree". After certain time i want the bot to register user if agree's is more than disagree.
How can i make my bot count the results of voting after 7 days and then based on this either register on DB an user or send "Report Voting Failed".
What I hope is to be able to store the expiration date of the voting and that on that date the voting stops working and the corresponding action is taken.
You can use setTimeout() for this. setTimeout() takes in two parameters. One, a function which holds your code that will be ran after the time has passed. Two, the amount of time in milliseconds to wait before executing your code.
Here is an example of how to use this.
console.log('1') // will console.log 1 instantly
setTimeout(() => {
console.log('2') // will console.log 2 after 1 second.
},
1000) // 1s = 1000ms
Furthermore, you can use buttons to accomplish the task of reporting. Check out the guide on buttons for more info. Once a button is pushed, see whether it is an Agree or Disagree then do what you must from there. After the seven days is up, make the buttons unable to press, or whatever you want.
You might want to employ a node.js module such as ms to convert 7d into ms. This could be useful as 7 days in milliseconds is 604,800,000. Using ms,
you could do
const sevenDays = ms('7d') // returns 604800000
and then pass that into your setTimeout() function.
like so:
setTimeout(() => {
console.log('The seven days has passed')
}, sevenDays)
Note however your bot will need to stay online for the duration of these 7 days, otherwise it will not run as it has forgotten that the setTimeout() function was used in an earlier instance.
I've been having this issue for months and I've finally made some headway. I'm writing an app the sends me a message at specific times, 9 am and 9 pm eastern time. When I ran it locally it worked perfectly but when I deploy it, I get nothing. I was messing around and then I saw this Heroku Logs. My guess is that my app is located on a server that is in a different time zone and when this code below runs. The conditions are never met and nothing gets sent. My question now is, is there a way I can get the current time of and compare regardless of what time zone the server is located?
const sendMessage = require('./sms-api.js');
const send = () =>{
setInterval(()=>{
var x = new Date().toLocaleTimeString();
console.log(x);
if(x === '11:00:10 AM')
{
console.log('match');
return sendMessage('6178032176', 'Good Morning');
}
else if(x === '9:50:20 PM')
{
console.log('match');
sendMessage('6178032176', 'Good Evening');
}
},1000)
}
send();
When working with different timezones, it is better to work in UTC and then offset it according to required timezone.
Get the time in UTC and then offset it according to required timezone.
You can also use dedicated libraries like moment-timezone.
https://momentjs.com/timezone/docs/
Like Suyash said above, your best option is to work entirely in UTC, and only convert when displaying times to users. Rather than dealing with offsets, you can append your dates and times with a 'Z' to indicate they are universal.
The best way I've found to do that is with moment.js and moment-timezone.js. Here is an example of an implementation that will allow you to convert times and dates: https://github.com/aidanjrauscher/browser-timezone-conversions. These libraries also make it very convenient to convert any date or time related user input back from their local time zone to UTC.
thank you for your help. I ended up figuring it out. I used this instead const time = new Date().toLocaleTimeString('en-US', { timeZone: 'America/New_York' });.
This is my first discord bot I have created, and it is for an idea that is complicated but I find as fun. I was wanting to create a discord bot that can send private messages to random users at a certain time (in my case, once every 24 hours). Is there a way to code this? I am using Node.js/nodemon to run the code. I already have the bot running and online, I just need a way to do what I was hoping to accomplish! Thank you to anyone who could help me out with this!
I would do setTimeout of some sort if you have your bot hosted 24/7.... If not I would find some sort of npm package that shows the time and then do
if(npmpackagetime == 'Whatever time you want here') return user.send('Your message here')
And if you want to chose a random user, make an array of all the guild's users and then do let newUser = Math.floor(Math.random() * arrayName.length); and then newUser.send('Your message here');
I am using bot framework where I proactively start a quiz game every 24 hours. The code for this looks like
bot.beginDialog(user.address, '/runChallenge', args, (error) => {
if (error) {
// error ocurred while starting new conversation. Channel not supported?
console.log(JSON.stringify(error), user.address)
bot.send(new builder.Message()
.text("The rat nibbled the wires before we could start the game for you. Sorry about that :(")
.address(user.address));
}
})
A couple of questions
How do i count the number of players in the game?
I could make a global variable called players inside my bot.js file
and increment it by 1 each time the bot.dialog('/quiz') is called
inside its 1st waterfall step. Given the file is included once at
the beginning, once I increment it, I guess i ll have to reset it
back to 0 somewhere.
Or I could use a local variable inside the cron job function which
can pass another parameter called playersCount as args to the dialog
where it will be a local variable but I am not sure if this is the
right approach
Everytime, a person answers incorrectly, they are out of the
challenge. I would like to count how many people answered
incorrectly. What would be a good way to go about this?
I would also like to declare the results of the quiz after say
everyone is done with it, The maximum time taken by anyone and
everyone is 15 mins for the round. I am planning to use a setTimeout
after 15 mins of initiating the game since I already use a CronJob
every 23 hours to trigger the quiz. Any suggestions?
I have been very disappointed to find that now that I am ready to migrate my html5 app onto phones using phonegap the ability to play audio is like perpetual motion. It is almost there, but not quite.
The answer for Androids (pre ice cream sandwich at least) is the Phonegap media api. According to everyone.
The problem is that I REALLY care about what the current time is while playing back my audio. On my Mac running Chrome I timed how long it takes to do:
pos=audio.currentTime
and it is less than a millisecond.
With the Phonegap media api I must call an asynchronous function, WAIT till it calls back and then I get the current media position. This takes anywhere from 5 to over 150 milliseconds. It is not consistent and it is distressingly slow.
It also begs the question: what time is it? If it takes 150ms to get the time, did I get the time at the beginning of those 150ms or the end? I really cant be sure that the time I got is really that close to the current time.
Its a good thing I am not still writing submarine navigation software, because with such a horrible source of time I would never be able to determine where I am.
I am now using the system clock to keep track of time, and using getCurrentPosition() to re-sync from time to time. However, I can still be off by over a hundred ms.
My question: Anyone got a better approach?
EDIT:
Looking at the code for Cordova:
/**
* Get current position of playback.
*
* #return position in msec or -1 if not playing
*/
public long getCurrentPosition() {
if ((this.state == STATE.MEDIA_RUNNING) || (this.state == STATE.MEDIA_PAUSED)) {
int curPos = this.player.getCurrentPosition();
this.handler.webView.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', " + MEDIA_POSITION + ", " + curPos / 1000.0f + ");");
return curPos;
}
else {
return -1;
}
}
It looks to me like this should execute pretty fast...except for that call to sendJavascript(). I wonder if that is where the mysterious delay is originating?
For my purposes I don't need an onStatus event: I need to get the current position as quickly as I can. So I wonder if I can remove that line of code... I guess that would necessitate a change to the Cordova source itself. :(