-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
236 lines (223 loc) · 6.15 KB
/
index.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
const VERSION = '3.2.0'
const { Telegraf, Telegram } = require('telegraf')
const { createBot } = require('mineflayer')
const assert = require('assert')
const commands = require('./lib/commands/index')
const callbacks = require('./lib/callbacks/index')
const inventory = require('./lib/inventory')
const minecraft = require('./lib/minecraft')
const { boolOrDefault } = require('./lib/utils')
class Minetelegram {
constructor (options = {}) {
assert.notEqual(
options.token,
undefined,
new Error('Token not found! pm @botfather to get one')
)
assert.notEqual(
options.user,
undefined,
new Error('User id not found! pm @myidbot to get one')
)
this.token = options.token
this.user = parseInt(options.user, 10)
this.commands = Object.assign({}, commands, options.commands)
let plugins = [inventory].concat(options.plugins)
this.plugins = plugins.filter(plugin => typeof plugin === 'function')
this.defaults = {
filters: options.filters || [],
chatEnabled: boolOrDefault(options.chat, true),
whisperEnabled: boolOrDefault(options.whisper, true),
messageEnabled: boolOrDefault(options.message, false),
echoEnabled: boolOrDefault(options.echo, true),
chatExtra: {
disable_web_page_preview: boolOrDefault(
options.disable_web_page_preview,
true
),
disable_notification: boolOrDefault(options.disable_notification, true)
},
telegramSendDelta: 1000
}
this.overide = true
this.instances = {}
this.botOptions = {}
this.current = null
this.ignoredCommands = [
'/start',
'/listen',
'/select',
'/send',
'/status',
'/filter',
'/inventory',
'/equip',
'/consume',
'/use_item',
'/toss',
'/chest',
'/close_chest',
'/deposit_chest',
'/withdraw_chest',
'/helps',
'/quit'
]
this.userCommands = ['User commands :']
const telegraf = new Telegraf(this.token)
const telegram = new Telegram(this.token)
telegraf.context.db = {
getClient: () => {
return telegram
},
getBot: username => {
if (username) return this.instances[username]
return this.bot
},
setBot: bot => {
this.bot = bot
},
createBot: options => {
return this.createBot(options)
},
deleteBot: bot => {
if (this.instances.hasOwnProperty(bot.username)) {
return delete this.instances[bot.username]
}
},
getIgnoredCommands: () => {
return this.ignoredCommands
},
getUserCommans: () => {
return this.userCommands
},
getStatus: bot => {
return this.getStatus(bot)
},
getCurrent: () => {
return this.current
},
getBotOptions: username => {
if (username && username in this.botOptions) {
return this.botOptions[username]
}
},
getUser: () => {
return this.user
}
}
this.telegraf = telegraf
this.telegram = telegram
this.VERSION = VERSION
}
launch () {
this.telegraf.use((ctx, next) => {
if (ctx.chat.id === this.user) {
next()
} else {
ctx.reply('Unauthorized!')
}
})
for (var command in this.commands) {
this.telegraf.command(command, this.commands[command])
}
this.telegraf.on('callback_query', callbacks)
this.telegraf.on('text', this.textFilter)
this.telegraf.launch()
}
get bot () {
if (Object.keys(this.instances).includes(this.current)) {
return this.instances[this.current]
}
// this.telegram.sendMessage(this.user, 'No instance running')
}
set bot (bot) {
let prevBot = this.bot
if (prevBot) prevBot.listen = false
this.current = bot.username
bot.listen = true
this.instances[this.current] = bot
}
createBot (options) {
const bot = createBot(options)
Object.assign(bot, this.defaults)
bot.telegraf = this.telegraf
bot.telegram = this.telegram
bot.listen = false
bot.ignoredCommands = this.ignoredCommands
bot.loadPlugins(this.plugins)
minecraft(bot, {
user: this.user
})
if (this.overide) this.bot = bot
bot.once('login', () => {
bot.lastLoggedIn = new Date()
bot.telegram
.sendMessage(this.user, `Logged in ${bot.username}`)
.catch(err => console.error(err))
})
return bot
}
getStatus (bot) {
function status ({ username, health, food, game }) {
return `
${username}
Health : ${health || '-'}
Food : ${food || '-'}
Dimension : ${game ? game.dimension : '-'}
`
}
if (bot) return status(bot)
if (Object.keys(this.instances).length === 0) {
return 'No minecraft instance running.'
}
let status_ = []
for (var username in this.instances) {
status_.push(status(this.instances[username]))
}
return status_.join('\n\n')
}
textFilter (ctx) {
let bot = ctx.db.getBot()
let ignoredCommands = ctx.db.getIgnoredCommands()
function ignore (message) {
for (var ignoreM in ignoredCommands) {
if (message.startsWith(ignoreM)) return true
}
return false
}
if (!bot) return
if (
bot.listen &&
ctx.message &&
ctx.message.text &&
!ignore(ctx.message.text)
) {
bot.chat(ctx.message.text)
}
if (bot.echoEnabled) {
console.log(`<- ${ctx.message.text}`)
}
}
addPlugin (plugin) {
return this.plugins.push(plugin)
}
addCommand (name, command, info = 'no info') {
if (typeof name !== 'string') {
return Error('name should be a string (telegraf.context as arg)')
}
if (typeof command !== 'function') {
return Error('command should be a function (telegraf.context as arg)')
}
this.commands[name] = command
this.userCommands.push(`${name} - ${info}`)
}
}
function createMinetelegram (options = {}) {
return new Minetelegram(options)
}
module.exports = { createMinetelegram, Minetelegram, VERSION }
if (require.main === module) {
if ('TOKEN' in process.env && 'USER' in process.env) {
require('./heroku')
} else require('./cli')
}