UnhandledPromiseRejectionWarning: DiscordAPIError: Unknown Message - runtime-error

I'm making a clear command in discord.js AND I DO KNOW THERE IS ANOTHER DUPLICATE but for some reason that DUPLICATE question doesn't solve my answer
else if(command == "clear")
{
async function clear(){
let fetched;
do {
fetched = await message.channel.messages.fetch({limit: 100});
message.channel.bulkDelete(fetched);
message.delete()
}
while(fetched.size >= 2)
}
clear();
}
and the title is the error I got

To delete messages no need to fetch them.
You can just write
message.channel.bulkDelete(100,true).then(msg=>{
console.log(`${msg.size} is deleted!`)
}).catch(err=>{
console.log(err)
})
The second parameter is to filter the 2 weeks old messages. More can be found here

Related

Return value of updateEmail() - firebase

I want to check if updateEmail() goes through or not. If it changes an email or not and I don't know what is the return value of that function so I can check it.
I tried something like this
if(user.updateEmail(email) != null) {
msg = "Success";
}
And I am never getting a null value if it is good or not.
The return values of the User.updateEmail method is Future<void>. So while the future has no resulting value, you can check whether it succeeded or failed by checking the result of the future itself. From there, it looks like it should be something like this:
user.updateEmail(email).then(() {
msg = "Success";
})
.catchError(handleError);

SQLITE_ERROR: near "?" when adding a column in ExpressJS

this is my first question here. I've been trying to get a column added to a database, using ExpressJS and sqlite3. Normally I use a question mark as a placeholder/parameter, but in this case I get an error. I've been struggling with this for hours but I can't get it to work. This is the error I get:
[Error: SQLITE_ERROR: near "?": syntax error] {
errno: 1,
code: 'SQLITE_ERROR'
}
Hope you guys can help me!
var scoreOfGame = req.body.game + "_Score";
db.all("ALTER TABLE Users ADD COLUMN ? INT", [scoreOfGame], (error, results) => {
if (error) {
console.log(error)
}
else {
console.log(results)
}
})
Instead of using ? as a placeholder, I rewrote it to this:
db.all(`ALTER TABLE Users ADD COLUMN ${scoreOfGame} INT`)

Telegram bot - answer inline query with plain text?

I'm trying to make an inline Telegram bot that would modify user input in a certain way. Because of that I wanted to answer the query with simple text, but that doesn't seem possible and I'm wondering if it's really not or I'm missing something.
According to Telegram, there's 20 handy result types, yet there doesn't seem to be simple plain text. Is that really the case? How can I achieve my desired result then?
I had the same exact issue and solved it with the InlineQueryResultArticle.
Example code for your OnInlineQuery Method:
// Check for invalid queries
if (e.InlineQuery.Query == null)
return;
if (e.InlineQuery.Query == "")
return;
InlineQueryResultBase[] results = {
new InlineQueryResultArticle(
// id's should be unique for each type of response
id: "1",
// Title of the option
title: "sample title",
// This is what is returned
new InputTextMessageContent("text that is returned") {ParseMode = Telegram.Bot.Types.Enums.ParseMode.Default })
{
// This is just the description shown for the option
Description = "You could also put your output text for a preview here too."
}
};
// Send the response
try
{
// If your method is not async you have to remove the await
await client.AnswerInlineQueryAsync(e.InlineQuery.Id, results);
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Error responding to Inline Query! {ex.Message}");
}
Use "InlineQueryResultArticle" and either set the value of "url" to undefined or do not set "url" fileld.

Meteor publish/subscribe not working

I'm working in meteor 1.7, and my publish/subscribe is returning only empty arrays.
file structure:
-all/
-shared.js
-client/
-main.js
-imports/
-Elsewhere.js
-server/
-main.js
shared.js:
Chats = new Mongo.Collection('chats')
client/main.js:
Template.main.onCreated(()=>{
Meteor.subscribe('chats')
});
server/main.js
Meteor.publish('chats', function(){
console.log(Chats.find().fetch()) //Shows that I have documents in the collection
return Chats.find();
});
Elsewhere.js
Template.Elsewhere.helpers({
chats(){
console.log(Chats.find().fetch()) //[]
return Chats.find().fetch()
}
})
Why don't I get what I'm publishing?
-------------------------------------New stuff-----------------------------------
I'm now unsure if it's a load order issue, reactivity issue, pub/sub issue, or a mix of them. I have this snippet
search(collection, where, id, part, callback){
var result
if(id){
console.log(Meteor.users.find().fetch()) //[]
result = Collections[collection].findOne(id)
}else{
result = Collections[collection].find(where ? where : {}, {many:true}).fetch()
}
if(result){
if(callback){
callback(result)
return
}else{
if(part){
return result[part]
}else{
return result
}
}
}
}
I'm also noticing that the output from this log is happening BEFORE my subscriptions. This file is located in /imports/scripts/tools.js
Inside an autorun block subscribe to 'chats' and check if handler is ready. Then find and fetch.
this.autorun(() => {
let handler = Meteor.subscribe('chats');
if(handler.ready()) {
console.log(Chats.find().fetch())
}
});
I realized that I was not using a reactive data source for my helpers, I was setting a Session variable when I called the tool.search function, which only ran once.

Sporadic behaviour with Session variables in Meteor

I've been scratching my head as to why this code will work some of the time, but not all (or at least most of the time). I've found that it actually does run displaying the correct content in the browser some of the time, but strangely there will be days when I'll come back to the same code, run the server (as per normal) and upon loading the page will receive an error in the console: TypeError: 'undefined' is not an object (evaluating 'Session.get('x').html')
(When I receive that error there will be times where the next line in the console will read Error - referring to the err object, and other times when it will read Object - referring the data object!?).
I'm obviously missing something about Session variables in Meteor and must be misusing them? I'm hoping someone with experience can point me in the right direction.
Thanks, in advance for any help!
Here's my dummy code:
/client/del.html
<head>
<title>del</title>
</head>
<body>
{{> hello}}
</body>
<template name="hello">
Hello World!
<div class="helloButton">{{{greeting}}}</div>
</template>
My client-side javascript file is:
/client/del.js
Meteor.call('foo', 300, function(err, data) {
err ? console.log(err) : console.log(data);
Session.set('x', data);
});
Template.hello.events = {
'click div.helloButton' : function(evt) {
if ( Session.get('x').answer.toString() === evt.target.innerHTML ) {
console.log('yay!');
}
}
};
Template.hello.greeting = function() {
return Session.get('x').html;
};
And my server-side javascript is:
/server/svr.js
Meteor.methods({
doubled: function(num) {
return num * 2;
},
foo: function(lmt) {
var count = lmt,
result = {};
for ( var i = 0; i < lmt; i++ ) {
count++;
}
count = Meteor.call('doubled', count);
result.html = "<em>" + count + "</em>";
result.answer = count;
return result;
}
});
I think it's just that the session variable won't be set yet when the client first starts up. So Session.get('x') will return undefined until your method call (foo) returns, which almost certainly won't happen before the template first draws.
However after that it will be in the session, so things will probably behave right once you refresh.
The answer is to just check if it's undefined before trying to access the variable. For example:
Template.hello.greeting = function() {
if (Session.get('x')) return Session.get('x').html;
};
One of the seven principles of Meteor is:
Latency Compensation. On the client, use prefetching and model simulation to make it look like you have a zero-latency connection to the database.
Because there is latency, your client will first attempt to draw the lay-out according to the data it has at the moment your client connects. Then it will do the call and then it will update according to the call. Sometimes the call might be able to respond fast enough to be drawn at the same time.
As now there is a chance for the variable to not be set, it would throw an exception in that occasion and thus break down execution (as the functions in the call stack will not continue to run).
There are two possible solutions to this:
Check that the variable is set when using it.
return Session.get('x') ? Session.get('x').html : '';
Make sure the variable has an initial value by setting it at the top of the script.
Session.set('x', { html = '', answer = ''});
Another approach would be to add the templates once the call responds.
Meteor.call('foo', 300, function(err, data) {
Session.set('x', data);
$('#page').html(Meteor.ui.render(function() {
return Template.someName();
}));
});

Resources