-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.js
152 lines (131 loc) · 4.28 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const { Bot, GrammyError, HttpError } = require('grammy')
const { autoQuote } = require('@roziscoding/grammy-autoquote')
const fs = require('fs')
const path = require('path')
if (fs.existsSync('.env')) {
require('dotenv').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...')
}
const ownerId = process.env.OWNERID
if (!ownerId) {
throw new Error('OWNERID not set in environment variables! Exiting...')
}
function logCommand(ctx) {
if (process.env.LOG_COMMANDS === 'true') {
const commandText = ctx.message.text
const chatTitle = ctx.chat.title
const userFirstName = ctx.from.first_name
const userLastName = ctx.from.last_name || ''
const username = ctx.from.username || ''
const logMessage = `Command: <code>${commandText}</code>\n\nGroup: <code>${chatTitle}</code>\n\nUser: ${userFirstName} ${userLastName} (@${username})`
ctx.api.sendMessage(logchannel, logMessage, {
parse_mode: 'HTML',
chat_id: logchannel,
})
}
}
async function start() {
const bot = new Bot(botToken)
bot.use(autoQuote())
const blockedUserIds = process.env.BLOCK_ID
? process.env.BLOCK_ID.split(',').map((id) => id.trim())
: []
const commandFilesDir = path.resolve(__dirname, 'commands')
const commandFiles = fs
.readdirSync(commandFilesDir)
.filter((file) => file.endsWith('.js'))
for (const file of commandFiles) {
const command = require(path.join(commandFilesDir, file))
bot.command(command.name, async (ctx) => {
if (blockedUserIds.includes(ctx.from.id.toString())) {
await ctx.reply('Sorry, you are not allowed to use this bot.')
return
}
logCommand(ctx)
if (command.ownercmd == true && ctx.from.id.toString() !== ownerId) {
await ctx.reply('Sorry, this command is only available to the owner.')
return
}
await command.handler(ctx)
})
if (command.alias) {
for (const alias of command.alias) {
bot.command(alias, async (ctx) => {
if (blockedUserIds.includes(ctx.from.id.toString())) {
await ctx.reply('Sorry, you are not allowed to use this bot.')
return
}
logCommand(ctx)
if (command.ownercmd && ctx.from.id.toString() !== ownerId) {
await ctx.reply(
'Sorry, this command is only available to the owner.'
)
return
}
await command.handler(ctx)
})
}
}
}
bot.command('start', (ctx) =>
ctx.reply(
"Hi! I'm CannyBot!\n\n" + 'Run the /help command to see what I can do!'
)
)
bot.catch((err) => {
const ctx = err.ctx
const e = err.error
const commandText = ctx.message.text
const chatTitle = ctx.chat.title
const errorMessage = `Command: <code>${commandText}</code>\n\nGroup: <code>${chatTitle}</code>\n\nUser: ${
ctx.from.first_name
} ${ctx.from.last_name || ''} (@${
ctx.from.username || ''
})\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('Oops, an error occurred!')
} 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.description)
} else {
ctx.reply(`Oh no, an error occurred!\n\n${errorMessage}`, {
parse_mode: 'HTML',
chat_id: logchannel,
})
ctx.reply('Oops, an error occurred!')
}
})
process.on('uncaughtException', (err) => {
console.error(err)
})
process.on('unhandledRejection', (err) => {
console.error(err)
})
process.on('SIGINT', () => {
console.log('Stopping CannyBot...')
bot.stop()
process.exit(0)
})
require('./webserver')
console.log('Starting CannyBot...')
await bot.start()
}
start().catch((error) => {
console.error('Error occurred during bot startup:', error)
process.exit(1)
})