-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
146 lines (117 loc) · 3.75 KB
/
server.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
const Long = require('long')
const fs = require('fs')
// Import external modules
const colors = require('colors/safe')
const { translate } = require('google-translate-api-browser')
const express = require('express')
// Import modules
const { chatHandler } = require('./modules/chatHandler')
const { vend } = require('./modules/vend')
const { getMap } = require('./modules/getMap')
const { getTeamInfo } = require('./modules/getTeamInfo')
const { getRustServerInfo } = require('./modules/getRustServerInfo')
const { getRustServerTime } = require('./modules/getRustServerTime')
// Initialize rustplusjs and discordjs
const config = require('./connection-files/rustplus.config.json')
const serverAnswer = require('./connection-files/server-answer.json')
const RustPlus = require('@liamcottle/rustplus.js')
const rustplus = new RustPlus(
serverAnswer.ip,
serverAnswer.port,
serverAnswer.playerId,
serverAnswer.playerToken
)
const { token } = require('./connection-files/config.json')
const { Client, GatewayIntentBits } = require('discord.js')
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
})
// Initialize express
const app = express()
const PORT = process.env.PORT || 5002
// Set default route
app.get('/', (req, res) => res.send('api running'))
app.listen(PORT, () =>
console.log(colors.brightGreen(`server started on port ${PORT}`))
)
// Discord bot connection
client.once('ready', () => {
console.log(`Ready! Logged in as ${client.user.tag}`)
const channelId = '1204125775515746324'
const channel = client.channels.cache.get(channelId)
if (channel) {
channel.send('Server running')
} else {
console.error(`Channel with ID ${channelId} not found.`)
}
})
client.login(token)
// Initialize rustplus global variables
let rustServerInfo
let rustServerTime
let mapInfo
let teamInfo
rustplus.on('connected', () => {
console.log(colors.brightGreen('connected to app'))
setInterval(() => {
getRustServerInfo(rustplus)
getRustServerTime(rustplus)
getMap(rustplus)
getTeamInfo(rustplus)
rustServerInfo = JSON.parse(
fs.readFileSync('./rust-json-info/rust-server-info.json')
).response
rustServerTime = JSON.parse(
fs.readFileSync('./rust-json-info/rust-server-time.json')
).response
mapInfo = JSON.parse(fs.readFileSync('./rust-json-info/map-info.json'))
.response.mapMarkers
teamInfo = JSON.parse(fs.readFileSync('./rust-json-info/team-info.json'))
.response.teamInfo
console.log(colors.brightBlue('server running'))
}, 3000)
// rustplus.getEntityInfo(1782934631, (message) => {
// console.log('getEntityInfo response message: ' + JSON.stringify(message))
// return true
// })
// rustplus.getEntityInfo(1634088408, (message) => {
// console.log('getEntityInfo response message: ' + JSON.stringify(message))
// console.log(message.response.entityInfo.payload)
// return true
// })
})
rustplus.on('message', (msg) => {
if (msg.hasOwnProperty('broadcast')) {
if (msg.broadcast.hasOwnProperty('teamMessage')) {
chatHandler(
rustplus,
msg,
rustServerInfo,
rustServerTime,
mapInfo,
client
)
}
}
})
// rustplus.on('message', (message) => {
// if (message.broadcast && message.broadcast.entityChanged) {
// var entityChanged = message.broadcast.entityChanged
// var entityId = entityChanged.entityId
// var value = entityChanged.payload.value
// console.log(
// 'entity ' +
// colors.blue(entityId) +
// ' is now ' +
// (value ? colors.green('active') : colors.red('inactive'))
// )
// }
// })
rustplus.on('error', (err) => {
console.log(err)
})
rustplus.connect()