-
Notifications
You must be signed in to change notification settings - Fork 34
/
index.ts
117 lines (104 loc) 路 3.42 KB
/
index.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
import EventHandler, { EventType } from '@bot/EventHandler';
import TicTacToeBot from '@bot/TicTacToeBot';
import Config from '@config/Config';
import localize from '@i18n/localize';
import { ChatInputCommandInteraction, Client, GatewayIntentBits, Message } from 'discord.js';
/**
* Controls all interactions between modules of the bot.
* Loads configuration, language files and the client.
*
* @author Utarwyn
* @since 2.0.0
*/
class TicTacToe {
/**
* Bot configuration
* @private
*/
private readonly config: Config;
/**
* Internal event handling system
* @private
*/
private readonly eventHandler: EventHandler;
/**
* Connection handling service to Discord
* @private
*/
private readonly bot: TicTacToeBot;
/**
* Constructs the game controller.
*
* @param config tictactoe configuration, default if not provided
*/
constructor(config?: Config) {
this.config = config ?? {};
this.eventHandler = new EventHandler();
this.bot = new TicTacToeBot(this.config, this.eventHandler);
localize.loadFromLocale(this.config.language);
}
/**
* Connects the module through an internal Discord client.
*/
public async login(token?: string): Promise<void> {
const loginToken = token ?? this.config.token;
if (!loginToken) {
throw new Error('Bot token needed to start Discord client.');
} else if (!this.config.command && !this.config.textCommand) {
throw new Error('Game slash or text command needed to start Discord client.');
}
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMessageReactions,
...(this.config.textCommand ? [GatewayIntentBits.MessageContent] : [])
]
});
try {
await client.login(loginToken);
} catch (e: any) {
if (e.message?.startsWith('Privileged')) {
throw new Error('You must enable Message Content intent to use the text command.');
} else {
throw e;
}
}
this.bot.attachToClient(client);
}
/**
* Attaches an external Discord Client to the module.
*
* @param client Discord.js client instance
*/
public attach(client: Client): void {
this.bot.attachToClient(client);
}
/**
* Programmatically handles a discord.js message to request a game.
*
* @param message Discord.js message object
* @deprecated use chat command interaction instead
*/
public handleMessage(message: Message): void {
this.bot.handleMessage(message);
}
/**
* Programmatically handles a discord.js interaction to request a game.
*
* @param interaction Discord.js interaction object
*/
public handleInteraction(interaction: ChatInputCommandInteraction): void {
this.bot.handleInteraction(interaction);
}
/**
* Register a listener to a specific event by its name.
*
* @param eventName name of the event to listen
* @param listener callback method called when the event is emitted
*/
public on(eventName: EventType, listener: (data?: any) => void): void {
this.eventHandler.registerListener(eventName, listener);
}
}
export = TicTacToe;