/
app.ts
134 lines (117 loc) · 4.59 KB
/
app.ts
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
import { readFileSync } from 'node:fs';
import process from 'node:process';
import { resolve } from 'node:path';
import util from 'node:util';
import { CommandContext, GatewayServer, InteractionRequestData, SlashCreator, Response, ApplicationCommandType } from "slash-create";
import { Client } from 'eris';
import R from 'ramda';
import CommandService from './services/command';
import TemplateEngine from './util/template-engine';
const config = JSON.parse(readFileSync('./config.json', 'utf8'));
const client = new Client(config.token);
const creator = new SlashCreator({
applicationID: config.applicationID,
publicKey: config.publicKey,
token: config.token,
handleCommandsManually: true
})
const service = new CommandService(creator);
const engine = new TemplateEngine();
creator
// .on('debug', console.log)
.registerCommandsIn(resolve(process.cwd(), './dist/commands'))
.withServer(new GatewayServer((handler) => {
client.on('rawWS', (event) => {
if (event.t === 'INTERACTION_CREATE')
handler(event.d as InteractionRequestData);
});
}));
creator.on('commandInteraction', async (interaction, respond, webserverMode) => {
const ctx = new CommandContext(creator, interaction, respond, webserverMode);
await ctx.defer();
// determine if a custom command can be used for this interaction
const hasCoreCommand = creator.commands.has(`${ctx.commandType}:global:${ctx.commandName}`);
const hasGuildContext = await service.hasOne(ctx.commandID);
if (hasCoreCommand || !hasGuildContext) { // skip if a custom command is available - when the id is found, it will be used
const command = creator.commands.get(`${ctx.commandType}:global:${ctx.commandName}`);
await command!.run(ctx);
return;
}
try {
// require guild context
if (!ctx.guildID) {
await ctx.send(':x: Custom commands can only be used in a guild.');
return;
}
const command = await service.getOne(ctx.guildID, ctx.commandID);
if (!command) {
return ctx.send(`:x: Command not found.`);
}
// console.log(command._id, command.key, command.description);
// omit methods that are not meant to be used by the user
let data: Record<string, any> = {
...R.omit(['token'], ctx.data),
interactionID: ctx.interactionID,
guildID: ctx.guildID,
channelID: ctx.channelID,
commandID: ctx.commandID,
users: Object.fromEntries(ctx.users),
members: Object.fromEntries(ctx.members),
roles: Object.fromEntries(ctx.roles),
channels: Object.fromEntries(ctx.channels),
messages: Object.fromEntries(ctx.messages),
command: command,
member: ctx.member,
user: ctx.user,
targetMessage: ctx.targetMessage || null,
targetUser: ctx.targetUser || null,
targetMember: ctx.targetMember || null,
channel: ctx.channels.get(ctx.channelID) || null,
}
const expect = `{{#expect ${Object.keys(data).filter(key => data[key] !== undefined)} }}`;
const content = await engine.render(expect + command.content, data);
// console.log(content);
await ctx.send(content);
console.log(`[${ctx.guildID}/${ctx.commandID}] ${command.name} was run by ${ctx.user.username}#${ctx.user.discriminator} (${ctx.user.id})`);
} catch (e) {
// console.log(e);
await ctx.send(':x: Sorry, an error occurred.\n```' + e + '```');
}
});
client.on('messageCreate', async (msg) => {
if (msg.content.startsWith('!eval') && config.admins.includes(msg.author.id)) {
const code = msg.content.substring(6);
try {
let start = msg.createdAt;
let ev = await new Promise<any>((resolve) => {
resolve(eval(code));
});
let end = Date.now();
if (typeof ev !== 'string')
ev = util.inspect(ev, {
depth: 2,
showHidden: true
})
ev = ev.replace(config.token, '1n-r1sk-w3-tru5t');
if (ev.length > 1000) {
client.createMessage(msg.channel.id, '**Output:** Success *with file upload*\nTime: ' + (end - start) / 1000, {
file: ev,
name: "evalresult.log"
});
} else {
client.createMessage(msg.channel.id, '**Output:** Success\nTime: ' + (end - start) / 1000 + '\n```' + ev + '```');
}
} catch (err: any) {
if ((err.stack?.length || 1337) > 1000) {
client.createMessage(msg.channel.id, '**Output:** Error *with file upload*', {
file: err.stack || err,
name: "evalresult.log"
});
} else {
client.createMessage(msg.channel.id, '**Output:** Failure\n```js\n' + (err.stack || err) + '```');
}
}
}
});
// creator.startServer();
client.connect();