Why telegram bot don't work if participants left group or new one add to group? - node.js

When I run my app and someone in group left or new participant add to group, my bot stop working.
Below you can see code of my app:
const TelegramBot = require('node-telegram-bot-api');
const token = '1928095766:AAGf5aU7E1_FAM_fWzQs3SSGy-1AJS0DjcY';
const bot = new TelegramBot(token, { polling: true });
bot.on('message', async msg => {
const reg = /\d\d\-\d\d$/;
if (msg.text.match(reg)) {
bot.sendMessage(msg.chat.id, 'Gh')
}
})
And what I received in terminal if someone left group or new one add into, when my app is running:
(node:21155) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'match' of undefined
at TelegramBot.<anonymous> (/Users/romansavka/Telegram-Bot-Node/telegram-bot/reminder.js:11:30)
at TelegramBot.emit (/Users/romansavka/Telegram-Bot-Node/telegram-bot/node_modules/eventemitter3/index.js:182:35)
at TelegramBot.processUpdate (/Users/romansavka/Telegram-Bot-Node/telegram-bot/node_modules/node-telegram-bot-api/src/telegram.js:634:12)
at /Users/romansavka/Telegram-Bot-Node/telegram-bot/node_modules/node-telegram-bot-api/src/telegramPolling.js:110:22
at Array.forEach (<anonymous>)
at /Users/romansavka/Telegram-Bot-Node/telegram-bot/node_modules/node-telegram-bot-api/src/telegramPolling.js:106:17
at tryCatcher (/Users/romansavka/Telegram-Bot-Node/telegram-bot/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/Users/romansavka/Telegram-Bot-Node/telegram-bot/node_modules/bluebird/js/release/promise.js:547:31)
at Promise._settlePromise (/Users/romansavka/Telegram-Bot-Node/telegram-bot/node_modules/bluebird/js/release/promise.js:604:18)
at Promise._settlePromise0 (/Users/romansavka/Telegram-Bot-Node/telegram-bot/node_modules/bluebird/js/release/promise.js:649:10)
at Promise._settlePromises (/Users/romansavka/Telegram-Bot-Node/telegram-bot/node_modules/bluebird/js/release/promise.js:729:18)
at _drainQueueStep (/Users/romansavka/Telegram-Bot-Node/telegram-bot/node_modules/bluebird/js/release/async.js:93:12)
at _drainQueue (/Users/romansavka/Telegram-Bot-Node/telegram-bot/node_modules/bluebird/js/release/async.js:86:9)
at Async._drainQueues (/Users/romansavka/Telegram-Bot-Node/telegram-bot/node_modules/bluebird/js/release/async.js:102:5)
at Immediate.Async.drainQueues (/Users/romansavka/Telegram-Bot-Node/telegram-bot/node_modules/bluebird/js/release/async.js:15:14)
at processImmediate (internal/timers.js:462:21)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:21155) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:21155) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

It's because the message received has no attribute text, so the error raised UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'match' of undefined

Related

bot won't delete message after let it go for 5 sec

I have a problem : my bot won't delete the message after letting it go for 5 sec.
Here is my code :
client.on('message', message => {
if (message.content.toLowerCase().includes(",")) {
message.delete(); // You can put it here.
}
});
The error :
log (node:492) UnhandledPromiseRejectionWarning: TypeError [INVALID_TYPE]: Supplied options is not an object.
at Message.delete (/home/runner/Boorubot-2/node_modules/discord.js/src/structures/Message.js:577:60)
at Client.<anonymous> (/home/runner/Boorubot-2/index.js:40:17)
at Client.emit (events.js:326:22)
at MessageCreateAction.handle (/home/runner/Boorubot-2/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (/home/runner/Boorubot-2/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (/home/runner/Boorubot-2/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
at WebSocketShard.onPacket (/home/runner/Boorubot-2/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
at WebSocketShard.onMessage (/home/runner/Boorubot-2/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
at WebSocket.onMessage (/home/runner/Boorubot-2/node_modules/ws/lib/event-target.js:132:16)
at WebSocket.emit (events.js:314:20)
(node:492) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 3)

Discord.js bot errors involving embedded images

So I programmed a discord bot to, on command, send a randomly selected gif from a folder in an embedded message. It worked fine until I started trying to host the bot from Heroku. Now the command does nothing but displays an error in the log saying something like "could not find file extension", "deprecation error", or, just now, "unhandled promise rejection". My main issue is that I'm very new to programming in general, so I have no clue what exactly my problem is, let alone how to resolve it.
This is the current code I have for this particular section:
case '//command':
const Embed = new MessageEmbed()
var number = 43;
var imageNumber = Math.floor (Math.random()*(number-1+1))+1
Embed.setTitle("//message")
Embed.setDescription("//message")
Embed.attachFiles({files: ["./images/" + imageNumber + ".gif"]})
Embed.setImage("attachment://" + imageNumber + ".gif")
Embed.setColor(0x9B00C3)
msg.channel.send(Embed)
break;
Edit: as requested, here is the error message that appears:
2020-07-11T17:23:22.063132+00:00 app[web.1]: (node:23) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'path' of undefined
2020-07-11T17:23:22.063143+00:00 app[web.1]: at findName (/app/node_modules/discord.js/src/structures/APIMessage.js:273:17)
2020-07-11T17:23:22.063144+00:00 app[web.1]: at Function.resolveFile (/app/node_modules/discord.js/src/structures/APIMessage.js:289:31)
2020-07-11T17:23:22.063145+00:00 app[web.1]: at /app/node_modules/discord.js/src/structures/APIMessage.js:224:72
2020-07-11T17:23:22.063147+00:00 app[web.1]: at Array.map (<anonymous>)
2020-07-11T17:23:22.063147+00:00 app[web.1]: at APIMessage.resolveFiles (/app/node_modules/discord.js/src/structures/APIMessage.js:224:46)
2020-07-11T17:23:22.063148+00:00 app[web.1]: at TextChannel.send (/app/node_modules/discord.js/src/structures/interfaces/TextBasedChannel.js:172:46)
2020-07-11T17:23:22.063149+00:00 app[web.1]: at Client.<anonymous> (/app/index.js:76:25)
2020-07-11T17:23:22.063149+00:00 app[web.1]: at Client.emit (events.js:315:20)
2020-07-11T17:23:22.063149+00:00 app[web.1]: at MessageCreateAction.handle (/app/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
2020-07-11T17:23:22.063150+00:00 app[web.1]: at Object.module.exports [as MESSAGE_CREATE] (/app/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
2020-07-11T17:23:22.063226+00:00 app[web.1]: (node:23) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
2020-07-11T17:23:22.063337+00:00 app[web.1]: (node:23) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I'm trying to make an announcement command but getting an error

So basically I'm trying to send an announcement to specific channel using message.mentions.channels
ex !announce #announcements Today is a great day!
Error:
(node:5516) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'channels' of undefined
at Object.module.exports.execute (C:\Users\Zarko\Desktop\Stackoverflow\commands\Answers\announce.js:3:36)
at Client.<anonymous> (C:\Users\Zarko\Desktop\Stackoverflow\events\message.js:20:30)
at Client.emit (events.js:223:5)
at MessageCreateAction.handle (C:\Users\Zarko\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (C:\Users\Zarko\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (C:\Users\Zarko\node_modules\discord.js\src\client\websocket\WebSocketManager.js:386:31)
at WebSocketShard.onPacket (C:\Users\Zarko\node_modules\discord.js\src\client\websocket\WebSocketShard.js:436:22)
at WebSocketShard.onMessage (C:\Users\Zarko\node_modules\discord.js\src\client\websocket\WebSocketShard.js:293:10)
at WebSocket.onMessage (C:\Users\Zarko\node_modules\discord.js\node_modules\ws\lib\event-target.js:125:16)
at WebSocket.emit (events.js:223:5)
(node:5516) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:5516) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Code:
let channel = message.mentions.channels.first();
if (!channel) return
let announcement = args.slice(1).join(" ");
channel.send(announcement)
Error was how you passed in variables:
in one of your other command files you had:
execute(client, message, args) {
//this works
}
And in your announce command file you had:
execute(message, args){
console.log(message, args);
/* =>
Client {
...
}
Message {
...
}
*/
}
So in this instance, message is the actual client object, and args is the actual message object, so simply just fix your variable spots,
Your new execute function in announce.js
execute(client, message, args) {
//rest of code here
}

Promise callback could not be found

I am trying to use chainpoint. I have a few hashes and am trying to submit them to chainpoint to create a timestamp proof and then anchor them to a public blockchain.
I am using the github tutorial https://github.com/chainpoint/chainpoint-js
But I am getting an error as
(node:8796) UnhandledPromiseRejectionWarning: Error: Argument must be
a non-empty Array
at S (C:\Users\Unmesha Banarjee\node_modules\chainpoint-js\dist\bundle.js:1:6002)
at H (C:\Users\Unmesha Banarjee\node_modules\chainpoint-js\dist\bundle.js:1:6533)
at de (C:\Users\Unmesha Banarjee\node_modules\chainpoint-js\dist\bundle.js:1:15276)
at Module. (C:\Users\Unmesha Banarjee\node_modules\chainpoint-js\dist\bundle.js:1:15596)
at tryCatch (C:\Users\Unmesha Banarjee\node_modules\regenerator-runtime\runtime.js:45:40)
at Generator.invoke [as _invoke] (C:\Users\Unmesha Banarjee\node_modules\regenerator-runtime\runtime.js:274:22)
at Generator.prototype.(anonymous function) [as next] (C:\Users\Unmesha
Banarjee\node_modules\regenerator-runtime\runtime.js:97:21)
at me (C:\Users\Unmesha Banarjee\node_modules\chainpoint-js\dist\bundle.js:1:15336)
at a (C:\Users\Unmesha Banarjee\node_modules\chainpoint-js\dist\bundle.js:1:16954)
at C:\Users\Unmesha Banarjee\node_modules\chainpoint-js\dist\bundle.js:1:17015 (node:8796)
UnhandledPromiseRejectionWarning: Unhandled promise rejection. This
error originated either by throwing inside of an async function
without a catch block, or by rejecting a promise which was not handled
with .catch(). (rejection id: 2) (node:8796) [DEP0018]
DeprecationWarning: Unhandled promise rejections are deprecated. In
the future, promise rejections that are not handled will terminate the
Node.js process with a non-zero exit code.
Can anyone help me...

How to make my bot send a message to a certain channel in my Discord

I want my bot to send a message to a certain channel in my discord server.
So I would like to run a command like -announce [my message here] and it would send it to a channel in my server called Announcements.
I have already tried something like this return message.channels.get("336313710106902529").send(announceembed) but it just gives me an error in my console.
` const msg = message.content
const announcechannel = bot.channels.get("336313710106902529");
if(cmd === `${prefix}announce`){
let announceembed = new Discord.RichEmbed()
.setTitle(":flag_jp: **Announcement** :flag_jp:")
.setDescription(msg)
.setColor("#ff0000");
return message.channels.get("336313710106902529").send(announceembed)
}
`
My expected result is for my bot to send a message to the announce channel.
Except it gives me the following error:
at Client.bot.on (C:\Users\craig\OneDrive\Documents\Visual Studio Code\bot\index.js:65:33)
at Client.emit (events.js:189:13)
at MessageCreateHandler.handle (C:\Users\craig\OneDrive\Documents\Visual Studio Code\bot\node_modules\discord.js\src\client\websocket\packets\handlers\MessageCreate.js:9:34)
at WebSocketPacketManager.handle (C:\Users\craig\OneDrive\Documents\Visual Studio Code\bot\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:103:65)
at WebSocketConnection.onPacket (C:\Users\craig\OneDrive\Documents\Visual Studio Code\bot\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:333:35)
at WebSocketConnection.onMessage (C:\Users\craig\OneDrive\Documents\Visual Studio Code\bot\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:296:17)
at WebSocket.onMessage (C:\Users\craig\OneDrive\Documents\Visual Studio Code\bot\node_modules\ws\lib\event-target.js:120:16)
at WebSocket.emit (events.js:189:13)
at Receiver._receiver.onmessage (C:\Users\craig\OneDrive\Documents\Visual Studio Code\bot\node_modules\ws\lib\websocket.js:137:47)
at Receiver.dataMessage (C:\Users\craig\OneDrive\Documents\Visual Studio Code\bot\node_modules\ws\lib\receiver.js:409:14)
(node:896) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:896) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.```
<Message> has no channels property, only channel which is the channel the message came from.
What you are looking for is <Client>.channels that you would use like so:
const msg = message.content;
const announceChannel = bot.channels.get("336313710106902529");
if(cmd === `${prefix}announce`){
let announceEmbed = new Discord.RichEmbed()
.setTitle(":flag_jp: **Announcement** :flag_jp:")
.setDescription(msg)
.setColor("#ff0000");
return announceChannel.send(announceEmbed);
}

Resources