How do I define the guild I want to use ".channels" on? - javascript

I'm setting up a bot analytics staff channel for my discord bot's support server.
I want the bot to be able to send a message and then edit it every minute. But, when I try to do this, I get an error saying that I didn't define the guild that I want to use .channels on. The exact error is TypeError: Cannot read property 'channels' of undefined.
I've tried defining my guild in many different ways, including making it into a variable and combining it with the function that finds the guild itself. But, none of these possible solutions, some of which coming from some related Stack Overflow posts, have worked for me.
I've also tried looking at some tutorials, but they also haven't worked either: the code always says that I'm not defining my guild correctly.
//Stats For Staff Channel
let serverchannel = bot.guilds.get("498548588318556210").channels.get("528288924917825556");
let servercount = bots.guilds.size
let uptimetotalseconds = (bot.uptime / 1000);
let uptimehours = Math.floor(uptimetotalseconds / 360);
uptimeseconds %= 3600;
let uptimeminutes = Math.floor(uptimetotalseconds / 60);
let uptimeseconds = uptimetotalseconds % 60;
let uptime = "Total Uptime: \n \n Hours: " + uptimehours + "\n Minutes: " + uptimeminutes + "\n Seconds: " + uptimeseconds + "\n Total Uptime In Seconds: " + uptimetotalseconds + "\n Total Uptime In MiliSeconds " + bot.upTime + "\n \n \n \n"
let messagetobesent = "**Bot Status** \n \n" + "Server Count: **" + servercount + " guilds** \n \n" + "Total Uptime: \n Hours: **" + uptimehours + "** \n" + "Minutes: **" + uptimeminutes + "** \n \n **Thats it!These stats are updated every minute and more are being added soon. ** "
serverchannel.send(messagetobesent)
while (true) {
setTimeout(function() {
message.edit(messagetobesent)
}, 60000);
}
The bot should grab the support server and channel that I want the message to be sent to by using their ids. Then, it should send a message stored as a variable in that channel. Finally, every minute it will edit that message with the new value of the variable.

TypeError: Cannot read property 'channels' of undefined.
This error actually means that it couldn't find the guild you're trying to get, Are you sure that bot.guilds.get("498548588318556210") actually returns a guild? Maybe bot is defined by another Discord.js client inside your code that's not in the guild you're trying to get? Or is there actually a guild with that ID? Or is your client actually a member of that guild?

Related

How to get data from a especif Time period in InfluxDB Node-RED

I'm trying to read data from InfluxDB v1.x but only in a especific time period, Example:
Get the values of temper01 from 2021-11-09/14:00:00 to 2021-11-10/14:00:00.
i know its possible with this code :
"WHERE time >= '2021-11-09T14:00:00Z' AND time <= '2021-11-10T14:00:00Z'" But i wanna know if it is possible to use a variable instead of the direct timestamp.
i've tried this code :
msg.query = "SELECT time , Temper01, Temper02, Temper03 FROM "
+ global.get("ID_device1")
+ " WHERE time >= "
+ msg.payload;
+ " AND time <= '2021-11-18T00:00:00.000Z' " ;
but dosent matter the msg.payload value, it just get data from the very first data stored, and when i try to single quote msg.payload i get the invalid operation: time and *influxql.VarRef are not compatible" error message.
Assuming that msg.paylaod looks something like 2020-11-18T00:00:00.000Z then you have not quoted the time string you are inserting into the query.
It should look like this:
msg.query = "SELECT time , Temper01, Temper02, Temper03 FROM "
+ global.get("ID_device1")
+ " WHERE time >= '"
+ msg.payload;
+ "' AND time <= '2021-11-18T00:00:00.000Z' " ;
The difference is the ' at the end of the string on the 3rd line and again at the start of the string on the last line.
Directly comparing a working version with the version that didn't work would have shown this.

How to get valid result from table.get using Dexie/IndexDB

I'm a PHP coder so not fully in the know with Javascript so possibly making a real schoolboy error here. I'm trying to facilitate some client side DB using Dexie & IndexDB. I can get values in using db.table.put and retrieve them all using db.table.toArray. However I seem to be struggling with getting individual records back out again. I'm trying to query for Keydata based on a form input. Heres my code, but it returns a TypeError cannot read property of undefined.
Here I can confirm in line 4 that my 'search2' variable contacts keyData (e.g. 1). But when used in line 5 it doesn't return a valid result. Can anyone see the issue?
function searchData(){
var search2;
search2 = document.getElementById('search2').value;
alert ("Id " + search2);
db.contacts.get(search2).then
(function (contact) {
alert ("Id " + contact.id + " : " + contact.title + " " + contact.forename + " " + contact.surname);
}).catch(function(error) {
alert ("Ooops: " + error);
});
}

How to detect if the bot has been mentioned?

How can I detect if my bot is mentioned?
I tried these thus far:
if (msg.content.toLowerCase().includes('#The Guardian of The Bar#5180')) {
msg.channel.send("My prefix here is" + prefix + "\n You can start with ``" + prefix + "help``");
}
if (msg.content.toLowerCase().includes('#The Guardian of The Bar')) {
msg.channel.send("My prefix here is" + prefix + "\n You can start with ``" + prefix + "help``");
}
if (msg.content.includes('#The Guardian of The Bar#5180')) {
msg.channel.send("My prefix here is" + prefix + "\n You can start with ``" + prefix + "help``");
}
if (msg.content.includes('#The Guardian of The Bar#5180')) {
msg.channel.send("My prefix here is" + prefix + "\n You can start with ``" + prefix + "help``");
}
I'm not experienced in discord.js, but according to this in the documentation, a message object has a method message.isMemberMentioned(User or GuildMember). Using this method, one can use
client.on('message', message => { //this event is fired, whenever the bot sees a new message
if (message.isMemberMentioned(client.user)) { //we check, whether the bot is mentioned, client.user returns the user that the client is logged in as
//this is where you put what you want to do now
}
});
I would like to recommend, that you read the documentation fully, before you try to make a bot.
On v12 discord.js you now have to use message.mentions.has(bot.user)

Guild join date is showing as today's date when it isn't?

I created a profile command in my discord bot which returns detailed info. The "Server Join Date:" is incorrect.
What am I doing wrong and how can I fix it? Thanks in advance
let member = message.mentions.members.first() || message.member,
user = member.user;
Part of my embed:
"**Server join Date: **" + `${moment.utc(member.JoinedAt).format('DD/MM/YY')}` + "\n" +
I expected this to show the join date of the member, instead it shows today's date. I tried this with several users, they all joined the server at a different date.
You used:
"**Server join Date: **" + `${moment.utc(member.JoinedAt).format('DD/MM/YY')}` + "\n" +
But the mistake is, that the member property is called .joinedAt and not .JoinedAt!
The corrected version is:
"**Server join Date: **" + `${moment.utc(member.joinedAt).format('DD/MM/YY')}` + "\n" +
Still
.addField("Joined Server :", `** \`${moment(user.joinedTimestamp).format("D/M/YYYYh:mm")}\` \n ${moment(user.joinedTimestamp).fromNow()}**`,true)
Show Today

Appending messages to chatbox

So I am currently busy with a chatbox, which stores all the messages in Firebase and immediately appends the message into the messagebox. The problem is that Firebase obtains messages in a certain order, I append to the chatbox in a certain way, and when I refresh it, it is different again.
function init_chatbox() {
$('.empty-chat').show();
ref.child("/chatboxes/{{ chatbox.pk }}/messages/").on("child_added", function (snapshot) {
$('.empty-chat').hide();
var object = snapshot.val();
var key = snapshot.key();
var name = "";
ref.child("/users/" + object['user_id'] + "/name").once('value', function(snapshot) {
name = snapshot.val();
var timestamp = object['timestamp'];
var message = object['message'];
extra_html = ' (remove)(edit)';
$('ul.chat-messages').append('<li class="' + key +'"><p class="author"><span>' + name + '</span><span></span><span class="time" data-livestamp="' + timestamp + '"></span>' + extra_html + '</p><p class="message">' + message + '</p></li>');
});
});
When I have the code like this (with .append), it shows the code like this immediately:
Kevin 2 hours ago (remove)(edit)
some message here
John 10 minutes ago (remove)(edit)
test
John 2 minutes ago (remove)(edit)
msg
Kevin few seconds ago (remove)(edit)
test
And when i refresh the page, the message will be at the top, like the order should be.
Here is the thing, I also tried to .prepend, but it just gives another other, which isn't right either (posts the new message on top, but when refreshing it displays the good order, but then the lastest message on top).
I hope someone can help me with this, have been trying to solve this
You should take a look at orderByChild() in the firebase docs.
Basically you can order the messages by their timestamp. Like this:
ref.child("/chatboxes/{{ chatbox.pk }}/messages/")
.orderByChild("timestamp")
.on("child_added", function (snap) { ... });
Messages with the lowest timestamp will come first.

Categories

Resources