Skip to content

Commit

Permalink
GH-56: separate command configuration object
Browse files Browse the repository at this point in the history
  • Loading branch information
utarwyn committed Nov 10, 2021
1 parent cef8ebb commit 7580304
Show file tree
Hide file tree
Showing 18 changed files with 63 additions and 75 deletions.
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module.exports = {
moduleNameMapper: {
'@bot/(.*)': '<rootDir>/src/bot/$1',
'@config/(.*)': '<rootDir>/src/config/$1',
'@i18n/(.*)': '<rootDir>/src/i18n/$1',
'@tictactoe/(.*)': '<rootDir>/src/tictactoe/$1'
},
globals: {
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/GameBoardBuilder.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import GameBoardBuilder from '@bot/gameboard/GameBoardBuilder';
import localize from '@config/localize';
import localize from '@i18n/localize';
import AI from '@tictactoe/AI';
import { Player } from '@tictactoe/Player';

Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/I18nProvider.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { I18nProvider } from '@config/I18nProvider';
import { I18nProvider } from '@i18n/I18nProvider';

describe('I18nProvider', () => {
let provider: I18nProvider;
Expand Down
23 changes: 8 additions & 15 deletions src/bot/GameCommand.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import TicTacToeBot from '@bot/TicTacToeBot';
import localize from '@config/localize';
import CommandConfig from '@config/CommandConfig';
import localize from '@i18n/localize';
import { GuildMember, Message, TextChannel, User } from 'discord.js';

/**
Expand Down Expand Up @@ -49,22 +50,14 @@ export default class GameCommand {
* Constructs the command to start a game.
*
* @param bot game client bot
* @param trigger string whiches triggering command
* @param cooldown amount of seconds to wait after executing command
* @param allowedRoleIds list of role identifiers that can use the command.
* @param config custom configuration of the command
*/
constructor(
bot: TicTacToeBot,
trigger?: string,
cooldown = 0,
allowedChannelIds: string[] = [],
allowedRoleIds: string[] = []
) {
constructor(bot: TicTacToeBot, config: CommandConfig) {
this.bot = bot;
this.trigger = trigger;
this.cooldown = cooldown;
this.allowedChannelIds = allowedChannelIds;
this.allowedRoleIds = allowedRoleIds;
this.trigger = config.command;
this.cooldown = config.requestCooldownTime ?? 0;
this.allowedChannelIds = config.allowedChannelIds ?? [];
this.allowedRoleIds = config.allowedRoleIds ?? [];
this.memberCooldownEndTimes = new Map();
}

Expand Down
8 changes: 1 addition & 7 deletions src/bot/TicTacToeBot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,7 @@ export default class TicTacToeBot {
this._configuration = configuration;
this._eventHandler = eventHandler;
this._channels = [];
this.command = new GameCommand(
this,
configuration.command,
configuration.requestCooldownTime,
configuration.allowedChannelIds,
configuration.allowedRoleIds
);
this.command = new GameCommand(this, configuration);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/bot/channel/GameChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import GameEntity from '@bot/channel/GameEntity';
import GameBoardMessage from '@bot/gameboard/GameBoardMessage';
import DuelRequestMessage from '@bot/request/DuelRequestMessage';
import TicTacToeBot from '@bot/TicTacToeBot';
import localize from '@config/localize';
import localize from '@i18n/localize';
import AI from '@tictactoe/AI';
import { GuildMember, TextChannel } from 'discord.js';

Expand Down
2 changes: 1 addition & 1 deletion src/bot/gameboard/GameBoardBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import GameEntity from '@bot/channel/GameEntity';
import { formatDiscordName } from '@bot/util';
import localize from '@config/localize';
import localize from '@i18n/localize';
import AI from '@tictactoe/AI';
import { Player } from '@tictactoe/Player';

Expand Down
14 changes: 7 additions & 7 deletions src/bot/gameboard/GameBoardMessage.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Collection, Message, MessageReaction, Snowflake } from 'discord.js';
import GameChannel from '@bot/channel/GameChannel';
import GameEntity from '@bot/channel/GameEntity';
import GameBoardBuilder from '@bot/gameboard/GameBoardBuilder';
import GameBoardConfig from '@bot/gameboard/GameBoardConfig';
import GameConfig from '@config/GameConfig';
import AI from '@tictactoe/AI';
import Game from '@tictactoe/Game';
import { Collection, Message, MessageReaction, Snowflake } from 'discord.js';

/**
* Message sent to display the status of a game board.
Expand All @@ -29,7 +29,7 @@ export default class GameBoardMessage {
/**
* Game board configuration.
*/
private readonly configuration?: GameBoardConfig;
private readonly configuration: GameConfig;
/**
* Discord message object managed by the instance.
*/
Expand All @@ -51,7 +51,7 @@ export default class GameBoardMessage {
channel: GameChannel,
member1: GameEntity,
member2: GameEntity,
configuration?: GameBoardConfig
configuration: GameConfig
) {
this.channel = channel;
this.game = new Game();
Expand Down Expand Up @@ -109,7 +109,7 @@ export default class GameBoardMessage {
builder.withEndingMessage(this.getEntity(this.game.winner));
}

const emojies = this.configuration?.gameBoardEmojies;
const emojies = this.configuration.gameBoardEmojies;
if (emojies && emojies.length === 2) {
builder.withEmojies(emojies[0], emojies[1]);
}
Expand Down Expand Up @@ -165,7 +165,7 @@ export default class GameBoardMessage {
if (this.game.finished) {
const winner = this.getEntity(this.game.winner);

if (this.configuration?.gameBoardDelete) {
if (this.configuration.gameBoardDelete) {
await this.message?.delete();
await this.channel.channel.send(
new GameBoardBuilder().withEndingMessage(winner).toString()
Expand Down Expand Up @@ -199,7 +199,7 @@ export default class GameBoardMessage {
* @private
*/
private awaitMove(): void {
const expireTime = this.configuration?.gameExpireTime ?? 30;
const expireTime = this.configuration.gameExpireTime ?? 30;
if (!this.message || this.message.deleted) return;
this.message
.awaitReactions(
Expand Down
2 changes: 1 addition & 1 deletion src/bot/request/DuelRequestMessage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import GameChannel from '@bot/channel/GameChannel';
import { formatDiscordName } from '@bot/util';
import localize from '@config/localize';
import localize from '@i18n/localize';
import { Collection, GuildMember, Message, MessageReaction, Snowflake } from 'discord.js';

/**
Expand Down
28 changes: 28 additions & 0 deletions src/config/CommandConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Configuration values to handle commands as wanted.
*
* @author Utarwyn
* @since 2.2.0
*/
export default interface CommandConfig {
/**
* Command to type to start a new tictactoe game.
*/
command?: string;
/**
* List of channel identifiers where games can be started.
*/
allowedChannelIds?: string[];
/**
* List of role identifiers that can start a game.
*/
allowedRoleIds?: string[];
/**
* Expiration time of a duel request.
*/
requestExpireTime?: number;
/**
* Cooldown time of a duel request.
*/
requestCooldownTime?: number;
}
37 changes: 4 additions & 33 deletions src/config/Config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import CommandConfig from '@config/CommandConfig';
import GameConfig from '@config/GameConfig';

/**
* Contains the needed configuration values to start the bot.
*
* @author Utarwyn
* @since 2.0.0
*/
export default interface Config {
export default interface Config extends CommandConfig, GameConfig {
/**
* Token used to connect to Discord's API.
*/
Expand All @@ -13,36 +16,4 @@ export default interface Config {
* Locale of the module.
*/
language?: string;
/**
* Command to type to start a new tictactoe game.
*/
command?: string;
/**
* List of channel identifiers where games can be started.
*/
allowedChannelIds?: string[];
/**
* List of role identifiers that can start a game.
*/
allowedRoleIds?: string[];
/**
* Expiration time of a duel request.
*/
requestExpireTime?: number;
/**
* Cooldown time of a duel request.
*/
requestCooldownTime?: number;
/**
* Expiration time of a player turn.
*/
gameExpireTime?: number;
/**
* Should bot needs to delete the game board message.
*/
gameBoardDelete?: boolean;
/**
* List of emojies used to identify players.
*/
gameBoardEmojies?: string[];
}
4 changes: 2 additions & 2 deletions src/config/ConfigProvider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path';
import fs from 'fs';
import Config from '@config/Config';
import fs from 'fs';
import path from 'path';

/**
* Manages the configuration loading to operate the bot.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/**
* Configuration values to initialize a gameboard.
* Configuration values to handle a game as wanted.
*
* @author Utarwyn
* @since 2.1.0
*/
export default interface GameBoardConfig {
export default interface GameConfig {
/**
* Expiration time of a player turn.
*/
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/config/localize.ts → src/i18n/localize.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { I18nProvider, Replacements } from '@config/I18nProvider';
import { I18nProvider, Replacements } from '@i18n/I18nProvider';

const provider = new I18nProvider();

Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Client, Message } from 'discord.js';
import EventHandler, { EventType } from '@bot/EventHandler';
import TicTacToeBot from '@bot/TicTacToeBot';
import localize from '@config/localize';
import localize from '@i18n/localize';
import Config from '@config/Config';

/**
Expand Down
4 changes: 2 additions & 2 deletions src/tictactoe/AI.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import GameEntity from '@bot/channel/GameEntity';
import { getOpponent, Player, PlayerComputeType } from '@tictactoe/Player';
import localize from '@i18n/localize';
import Game from '@tictactoe/Game';
import localize from '@config/localize';
import { getOpponent, Player, PlayerComputeType } from '@tictactoe/Player';

/**
* Operate the AI behavior (using the minimax alghorithm).
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"paths": {
"@bot/*": ["src/bot/*"],
"@config/*": ["src/config/*"],
"@i18n/*": ["src/i18n/*"],
"@tictactoe/*": ["src/tictactoe/*"]
},
"incremental": true
Expand Down

0 comments on commit 7580304

Please sign in to comment.