-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.js
78 lines (67 loc) · 2.17 KB
/
bot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { Bot, GrammyError, HttpError } from 'grammy'
import { autoQuote } from '@roziscoding/grammy-autoquote'
import { config } from 'dotenv'
import fs from 'fs'
import './webserver.js'
import composer from './modules/mod.js'
if (fs.existsSync('.env')) {
config()
}
const logchannel = process.env.LOG_CHANNEL
if (!logchannel) {
throw new Error('LOG_CHANNEL not set in environment variables! Exiting...')
}
const botToken = process.env.BOT_TOKEN
if (!botToken) {
throw new Error('BOT_TOKEN not set in environment variables! Exiting...')
}
async function start() {
const bot = new Bot(botToken)
bot.use(autoQuote())
bot.use(composer)
bot.catch((err) => {
const ctx = err.ctx
console.log(`Error while handling update ${ctx.update.update_id}:`)
const e = err.error
const commandText = ctx.message.text
const chatTitle = ctx.chat.title
const errorMessage = `Command: <code>${commandText}</code>\n\nUser: ${ctx.from.first_name} ${ctx.from.last_name} (${ctx.from.username})\n\nGroup: <code>${chatTitle}</code>\n\nError: <code>${e}</code>`
if (e instanceof GrammyError) {
ctx.api.sendMessage(logchannel, `Error in request:\n\n${errorMessage}`, {
parse_mode: 'HTML',
chat_id: logchannel,
})
ctx.reply('Error in request:', e.description)
} else if (e instanceof HttpError) {
ctx.api.sendMessage(
logchannel,
`Could not contact Telegram:\n\n${errorMessage}`,
{ parse_mode: 'HTML', chat_id: logchannel }
)
ctx.reply('Could not contact Telegram:', e)
} else {
ctx.reply(`Oops, an error occurred!\n\n${errorMessage}`, {
parse_mode: 'HTML',
chat_id: logchannel,
})
ctx.reply('Oops, an error occurred!\n\n' + e)
}
})
process.on('uncaughtException', (err) => {
console.error(err)
})
process.on('unhandledRejection', (err) => {
console.error(err)
})
process.on('SIGINT', () => {
console.log('Stopping @RealmeInfoBot...')
bot.stop()
process.exit(0)
})
console.log('Starting @RealmeInfoBot...')
await bot.start()
}
start().catch((error) => {
console.error('Error occurred during bot startup:', error)
process.exit(1)
})