-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexec.js
executable file
·105 lines (100 loc) · 3.9 KB
/
exec.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
registerPlugin({
name: "Exec",
version: "0.1.0",
description: "Evaluates chat commands",
author: "Multivitamin <[email protected]",
backends: ["ts3", "discord"],
requiredModules: ["http"],
vars: [{
name: "admins",
title: "UIDs of users which have access to admin commands",
type: "strings",
default: []
}]
}, (_, config) => {
const engine = require("engine")
const event = require("event")
const format = require("format")
// import modules for quick use in exec:
/* eslint-disable */
const backend = require("backend")
const helpers = require("helpers")
const media = require("media")
const audio = require("audio")
const store = require("store")
const http = require("http")
/* eslint-enable */
const codeBlockPattern = /^ *```(javascript *\r?\n?)?(?<code>.*)``` *$/si
/**
* Checks if a client is allowed to use admin commands.
* @param {Client} client
* @returns {boolean}
*/
function allowAdminCommands(client) {
switch (engine.getBackend()) {
case "discord":
return config.admins.includes(client.uid().split("/")[1])
case "ts3":
return config.admins.includes(client.uid())
default:
throw new Error(`Unknown backend ${engine.getBackend()}`)
}
}
// eslint-disable-next-line no-unused-vars
function evaluate(code, ev, reply) {
const start = Date.now()
let data = null
let error = null
try {
const client = ev.client
// eslint-disable-next-line no-eval
data = eval(code)
} catch (e) {
error = e
}
return {
error,
data,
duration: Date.now() - start
}
}
event.on("load", () => {
const command = require("command")
if (!command) {
engine.log('command.js library not found! Please download command.js to your scripts folder and restart the SinusBot, otherwise this script will not work.');
engine.log('command.js can be found here: https://github.com/Multivit4min/Sinusbot-Command/blob/master/command.js');
return;
}
const {createCommand} = command
createCommand("exec")
.alias('eval', 'run')
.help("Executes a raw command within Sinusbot")
.addArgument(arg => arg.rest.setName("code"))
.checkPermission(allowAdminCommands)
.exec((client, {code}, reply, ev) => {
if (engine.getBackend() === "discord") {
const match = code.match(codeBlockPattern)
if (match) code = match.groups.code
const res = evaluate(code, ev, reply)
const duration = `Duration: ${res.duration}ms`
if (res.error) return reply(`Error:\n${format.code(res.error.stack)}\n${duration}`)
if (res.data !== null) {
if (res.data === '') return reply(`Empty string returned.\n${duration}`)
let msg = `${format.code(res.data)}\nType: ${typeof res.data}, ${duration}`
if (msg.length >= 2000) {
reply(`Data is too long to post, see log.\n${duration}`)
engine.log(res.data)
return;
}
return reply(msg)
}
reply(`No data returned.\n${duration}`)
} else {
const res = evaluate(code, ev, reply)
const duration = `Duration: ${res.duration}ms`
if (res.error) return reply(`Error:\n${res.error.stack}\n${duration}`)
if (res.data) reply(`${res.data}\n${duration}`)
}
})
})
})