-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
145 lines (132 loc) · 6.6 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
const Discord = require('discord.js');
const { execSync, exec } = require('child_process');
const client = new Discord.Client({ intents: [Discord.Intents.FLAGS.GUILDS, Discord.Intents.FLAGS.GUILD_MESSAGES] });
const token = 'token';
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
const prefix = '!';
client.on('messageCreate', msg => {
if (msg.content[0] !== prefix ) {
// console.log('no prefix');
return;
}
if (msg.channelId !== '892835196079046708') {
// dont listen to other channels
return;
}
if (msg.member.user.id === '894646765993734245') {
// dont listen to other channels
return;
}
const args = msg.content.slice(prefix.length).trim().split(' ');
const command = args.shift().toLowerCase();
if(command === 'help') {
msg.reply(
"!officialbuild - prints the build number from valheim official repo, stable branch\n" +
"!localbuild - prints the build currently installed\n" +
"!builds - prints both official and local builds\n" +
"!info - prints general info about the server\n" +
"!status - prints the status of the server (if its up or down)\n" +
"!upgrade - upgrades the server if there is an update (special privilege needed)"
)
}
if(command === 'officialbuild') {
const buildnoOrError = printValheimOfficialBuild();
msg.reply(buildnoOrError);
}
if(command === 'localbuild') {
const buildnoOrError = printValheimLocalBuild();
msg.reply(buildnoOrError);
}
if(command === 'builds') {
msg.reply(printBuildNumbers())
}
if(command === 'info') {
const officialBuild = printValheimOfficialBuild()
const localBuild = printValheimLocalBuild()
const worldName = execSync('cat /home/steam/worlds.txt')
const worldNameStr = worldName.toString("utf-8", 0, worldName.length).trim().split(' ').shift();
const serverName = execSync('perl -n -e \'/\-name "?([^"]+)"? \-port/ && print "$1\n"\' /home/steam/valheimserver/HomeyAndBuilds/start_valheim_HomeyAndBuilds.sh')
const serverNameStr = serverName.toString("utf-8", 0, serverName.length).trim().split(' ').shift();
const externalIP = execSync('curl -s ipecho.net/plain;echo')
const externalIPStr = externalIP.toString("utf-8", 0, externalIP.length).trim().split(' ').shift();
const port = execSync('perl -n -e \'/\-port "?([^"]+)"? \-nographics/ && print "$1\n"\' /home/steam/valheimserver/HomeyAndBuilds/start_valheim_HomeyAndBuilds.sh')
const portStr = port.toString("utf-8", 0, port.length).trim().split(' ').shift()
const displayPublic = execSync('perl -n -e \'/\-public "([0-1])"? \-savedir/ && print "$1\n"\' /home/steam/valheimserver/HomeyAndBuilds/start_valheim_HomeyAndBuilds.sh')
const displayPublicStr = displayPublic.toString("utf-8", 0, displayPublic.length).trim().split(' ').shift();
const isPublic = displayPublicStr === '1' ? 'On' : 'Off'
const status = getValheimServerStatus()
const substate = getValheimServerSubstate()
let statusStr = status.charAt(0).toUpperCase() + status.substring(1, status.length) + ' and ' + substate.charAt(0).toUpperCase() + substate.substring(1, substate.length)
const infoEmbed = new Discord.MessageEmbed()
.setTitle('Server Info')
.addFields(
{ name: 'Valheim Official Build', value: officialBuild, inline: true },
{ name: 'Local Valheim Install', value: localBuild, inline: true },
)
.addFields(
{ name: 'Worldname', value: worldNameStr },
{ name: 'Server name', value: serverNameStr },
{ name: 'Server URL', value: 'URL' },
{ name: 'External IP', value: externalIPStr },
{ name: 'Server Port', value: portStr },
{ name: 'Public Listing', value: isPublic },
{ name: 'Server Status', value: statusStr },
)
msg.channel.send({ embeds: [infoEmbed] })
}
if(command === 'status') {
const status = getValheimServerStatus()
const substate = getValheimServerSubstate()
let statusStr = status.charAt(0).toUpperCase() + status.substring(1, status.length) + ' and ' + substate.charAt(0).toUpperCase() + substate.substring(1, substate.length)
let statusText = new Discord.MessageEmbed()
if(status === "active" && substate === "running") {
statusText = new Discord.MessageEmbed().setDescription(statusStr).setColor('GREEN')
} else {
statusText = new Discord.MessageEmbed().setDescription(statusStr).setColor('RED')
}
msg.channel.send({ embeds: [statusText] })
}
if(command === 'upgrade') {
if(msg.member.user.id === '126751120054943744') {
const officialBuild = printValheimOfficialBuild();
const localBuild = printValheimLocalBuild();
if(officialBuild === localBuild) {
msg.reply("Officalbuild: " + officialBuild + "LocalBuild: " + localBuild + "No new updates found")
} else {
msg.reply("Update found, Using Thor's Hammer to apply Official Updates!")
exec('steamcmd +login anonymous +force_install_dir /home/steam/valheimserver/HomeyAndBuilds +app_update 896660 validate +exit')
exec('sudo chown -R steam:steam /home/steam/valheimserver/HomeyAndBuilds')
msg.reply('Update added, kicking process to Odin for restarting!')
exec('sudo systemctl restart valheimserver_HomeyAndBuilds.service')
}
} else {
msg.reply("You do not have permission to do this command.")
}
}
});
function getValheimServerStatus() {
const status = execSync('systemctl is-active valheimserver_HomeyAndBuilds.service')
const statusStr = status.toString("utf-8", 0, status.length).trim().split(' ').shift().toLowerCase();
return statusStr;
}
function getValheimServerSubstate() {
const substate = execSync('systemctl show -p SubState valheimserver_HomeyAndBuilds.service | cut -d\'=\' -f2')
const stateStr = substate.toString("utf-8", 0, substate.length).trim().split(' ').shift().toLowerCase();
return stateStr;
}
function printValheimOfficialBuild() {
const officialBuild = execSync('steamcmd +login anonymous +app_info_update 1 +app_info_print 896660 +quit | grep -A10 branches | grep -A2 public | grep buildid | cut -d\'"\' -f4')
return officialBuild.toString("utf-8", 0, officialBuild.length)
}
function printValheimLocalBuild() {
const localBuild = execSync('grep buildid /home/steam/valheimserver/HomeyAndBuilds/steamapps/appmanifest_896660.acf | cut -d\'"\' -f4')
return localBuild.toString("utf-8", 0, localBuild.length)
}
function printBuildNumbers() {
const officialBuild = printValheimOfficialBuild()
const localBuild = printValheimLocalBuild();
return 'OfficialBuild: ' + officialBuild + 'LocalBuild: ' + localBuild;
}
client.login(token);