-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.ts
More file actions
69 lines (53 loc) · 2.09 KB
/
command.ts
File metadata and controls
69 lines (53 loc) · 2.09 KB
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
import { ClientEvents, Message, MessageReaction, Permissions, ThreadChannel } from "discord.js";
import messageRepo from "./command/messageRepo";
import reactionRepo from "./command/reactionRepo";
import threadRepo from "./command/threadRepo";
import { Meta } from "./command/interfaces";
function isMessage(eventObj: Message | ClientEvents): eventObj is Message {
const premise = eventObj as Message;
return premise.member !== undefined &&
premise.tts !== undefined;
}
function isReact(eventObj: MessageReaction | ClientEvents): eventObj is MessageReaction {
const premise = eventObj as MessageReaction;
return premise.message !== undefined &&
premise.emoji != undefined;
}
function isThread(eventObj: ThreadChannel | ClientEvents): eventObj is ThreadChannel {
const premise = eventObj as ThreadChannel;
return premise.autoArchiveDuration != undefined;
}
function handleEvents(type: string, eventObject: ClientEvents) {
const META: Meta = {
isMod: false,
fromGuild: false,
};
console.log(eventObject);
// messageCommand
if (isMessage(eventObject)) {
const message = eventObject as Message;
// abort if it's invoked by Forumify itself or another bot
if (message.author.bot || message.author.id === message.client.user?.id) return;
META.fromGuild = message.guildId !== null;
META.isMod = message.member?.permissions.has(Permissions.FLAGS.MANAGE_CHANNELS);
const content = message.content.trim().split(/\s+/);
META.commandArgs = content.slice(1);
META.command = content[0];
return messageRepo.delegate(META, message);
}
// reactCommand
else if (isReact(eventObject)) {
const react = eventObject as MessageReaction;
// abort if it's invoked by Forumify itself
if (react.me) return;
return reactionRepo.delegate(META, react);
}
// threadCommand
else if (isThread(eventObject)) {
const thread = eventObject as ThreadChannel;
return threadRepo.delegate(META, thread);
}
}
export default {
handleEvents
};