-
Notifications
You must be signed in to change notification settings - Fork 34
/
TicTacToeBot.ts
106 lines (97 loc) 路 3.13 KB
/
TicTacToeBot.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
import AppCommandRegister from '@bot/command/AppCommandRegister';
import GameCommand from '@bot/command/GameCommand';
import EventHandler from '@bot/EventHandler';
import GameStateManager from '@bot/state/GameStateManager';
import Config from '@config/Config';
import { Client, CommandInteraction, Message } from 'discord.js';
/**
* Manages all interactions with the Discord bot.
*
* @author Utarwyn
* @since 2.0.0
*/
export default class TicTacToeBot {
/**
* Game configuration object
* @private
*/
private readonly _configuration: Config;
/**
* Manages the command handling
* @private
*/
private readonly _eventHandler: EventHandler;
/**
* Manages the command handling
* @private
*/
private readonly command: GameCommand;
/**
* Constructs the Discord bot interaction object.
*
* @param configuration game configuration object
* @param eventHandler event handling system
*/
constructor(configuration: Config, eventHandler: EventHandler) {
this._configuration = configuration;
this._eventHandler = eventHandler;
this.command = new GameCommand(new GameStateManager(this));
}
/**
* Retrieves the game configuration object.
*/
public get configuration(): Config {
return this._configuration;
}
/**
* Retrieves the event handling system.
*/
public get eventHandler(): EventHandler {
return this._eventHandler;
}
/**
* Attaches a new Discord client
* to the module by preparing command handing.
*
* @param client discord.js client obbject
*/
public attachToClient(client: Client): void {
const onReady = () => {
// Handle slash command if enabled
if (client.application && this.configuration.command) {
const register = new AppCommandRegister(
client.application.commands,
this.configuration.command,
this.configuration.commandOptionName ?? 'opponent'
);
client.on('messageCreate', register.handleDeployMessage.bind(register));
client.on('interactionCreate', this.command.handleInteraction.bind(this.command));
}
// Handle text command if enabled
if (this.configuration.textCommand) {
client.on('messageCreate', this.command.handleMessage.bind(this.command));
}
};
if (client.readyAt) {
onReady();
} else {
client.on('ready', onReady.bind(this));
}
}
/**
* Programmatically handles a discord.js message to request a game.
*
* @param message Discord.js message object
*/
public handleMessage(message: Message): void {
this.command.handleMessage(message, true);
}
/**
* Programmatically handles a discord.js interaction to request a game.
*
* @param interaction Discord.js interaction object
*/
public handleInteraction(interaction: CommandInteraction): void {
this.command.handleInteraction(interaction, true);
}
}