1 |
import "../setup"; |
2 |
|
3 |
import { ChatInputCommandInteraction, Message } from "discord.js"; |
4 |
import assert from "node:assert"; |
5 |
import { after, before, describe, it } from "node:test"; |
6 |
import Client from "../../src/core/Client"; |
7 |
import { protectSystemAdminsFromCommands } from "../../src/utils/troll"; |
8 |
import { registerFileHandler, setCustomSystemConfig, unregisterFileHandler } from "../clientsetup"; |
9 |
import { randomSnowflake } from "../utils"; |
10 |
|
11 |
const client = new Client( |
12 |
{ |
13 |
intents: [] |
14 |
}, |
15 |
{ |
16 |
services: ["@services/ConfigManager"] |
17 |
} |
18 |
); |
19 |
|
20 |
describe("Trolling utility functions", () => { |
21 |
const CLIENT_USER_ID = randomSnowflake(); |
22 |
const SYSTEM_ADMIN = randomSnowflake(); |
23 |
|
24 |
before(() => { |
25 |
setCustomSystemConfig(` |
26 |
{ |
27 |
"system_admins": ["${SYSTEM_ADMIN}"] |
28 |
} |
29 |
`); |
30 |
registerFileHandler(); |
31 |
return client.boot(); |
32 |
}); |
33 |
|
34 |
after(() => { |
35 |
unregisterFileHandler(); |
36 |
}); |
37 |
|
38 |
it("Protects system admins from being used in joke moderation commands", async t => { |
39 |
const message1 = { |
40 |
deferred: false, |
41 |
async reply() {}, |
42 |
async editReply() {} |
43 |
} as unknown as Message & Pick<ChatInputCommandInteraction, "editReply">; |
44 |
|
45 |
const message2 = { |
46 |
deferred: true, |
47 |
async reply() {}, |
48 |
async editReply() {} |
49 |
} as unknown as Message & Pick<ChatInputCommandInteraction, "editReply">; |
50 |
|
51 |
t.mock.method(message1, "reply"); |
52 |
t.mock.method(message1, "editReply"); |
53 |
t.mock.method(message2, "reply"); |
54 |
t.mock.method(message2, "editReply"); |
55 |
|
56 |
client.user = { id: CLIENT_USER_ID } as any; |
57 |
|
58 |
assert.strictEqual(await protectSystemAdminsFromCommands(client, message1, randomSnowflake()), false); |
59 |
assert.strictEqual((message1.reply as any).mock.calls.length, 0); |
60 |
assert.strictEqual((message1.editReply as any).mock.calls.length, 0); |
61 |
|
62 |
assert.strictEqual(await protectSystemAdminsFromCommands(client, message2, randomSnowflake()), false); |
63 |
assert.strictEqual((message2.reply as any).mock.calls.length, 0); |
64 |
assert.strictEqual((message2.editReply as any).mock.calls.length, 0); |
65 |
|
66 |
assert.strictEqual(await protectSystemAdminsFromCommands(client, message1, SYSTEM_ADMIN), true); |
67 |
assert.strictEqual((message1.reply as any).mock.calls.length, 1); |
68 |
assert.strictEqual((message1.editReply as any).mock.calls.length, 0); |
69 |
|
70 |
assert.strictEqual(await protectSystemAdminsFromCommands(client, message2, SYSTEM_ADMIN), true); |
71 |
assert.strictEqual((message2.reply as any).mock.calls.length, 0); |
72 |
assert.strictEqual((message2.editReply as any).mock.calls.length, 1); |
73 |
|
74 |
assert.strictEqual(await protectSystemAdminsFromCommands(client, message1, CLIENT_USER_ID), true); |
75 |
assert.strictEqual((message1.reply as any).mock.calls.length, 2); |
76 |
assert.strictEqual((message1.editReply as any).mock.calls.length, 0); |
77 |
|
78 |
assert.strictEqual(await protectSystemAdminsFromCommands(client, message2, CLIENT_USER_ID), true); |
79 |
assert.strictEqual((message2.reply as any).mock.calls.length, 0); |
80 |
assert.strictEqual((message2.editReply as any).mock.calls.length, 2); |
81 |
}); |
82 |
}); |