-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement cmd for drawing random card of game
- Loading branch information
Showing
6 changed files
with
151 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/node_modules/ | ||
/dist/ | ||
/cards/ | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import { | ||
ApplicationCommandOptionType, | ||
AttachmentBuilder, | ||
ChatInputCommandInteraction, | ||
EmbedBuilder, | ||
Check failure on line 7 in src/server/discord/command/draw.ts GitHub Actions / Continuous Integration
|
||
} from 'discord.js'; | ||
import { DiscordCommand, DiscordCommandOption } from './interface'; | ||
|
||
export class DrawCommand implements DiscordCommand { | ||
public name = 'draw'; | ||
public description = 'Draws a playing card from a deck of some game'; | ||
|
||
public async getOptions(): Promise<DiscordCommandOption[]> { | ||
const cardsPath = path.join(process.cwd(), 'cards'); | ||
// eslint-disable-next-line no-sync | ||
if (!fs.existsSync(cardsPath)) { | ||
await fs.promises.mkdir(cardsPath); | ||
console.warn( | ||
"Cards directory didn't exist. Created empty one at: " + | ||
cardsPath, | ||
); | ||
} | ||
const cardsDirectoryContents = await fs.promises.readdir( | ||
path.join(process.cwd(), 'cards'), | ||
{ withFileTypes: true }, | ||
); | ||
const games = cardsDirectoryContents.filter((dirent) => | ||
dirent.isDirectory(), | ||
); | ||
const options = [ | ||
{ | ||
type: ApplicationCommandOptionType.SubcommandGroup, | ||
name: 'game', | ||
description: 'What game are we drawing from?', | ||
options: await Promise.all( | ||
games.map(async (game) => { | ||
const cards = await this.getCardsOfGame(game.name); | ||
return { | ||
type: ApplicationCommandOptionType.Subcommand, | ||
name: this.directoryToCommand(game.name), | ||
description: game.name, | ||
options: cards.map((card) => ({ | ||
type: ApplicationCommandOptionType.Number, | ||
name: this.directoryToCommand(card), | ||
description: card + ' card(s)', | ||
min_value: 1, | ||
})), | ||
}; | ||
}), | ||
), | ||
}, | ||
]; | ||
return options; | ||
} | ||
|
||
public async run(interaction: ChatInputCommandInteraction): Promise<void> { | ||
const subcommand = interaction.options.getSubcommand(); | ||
switch (subcommand) { | ||
case 'paranoia': | ||
case 'executive': { | ||
await this.runGame(subcommand, interaction); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
private async getCardsOfGame(game: string): Promise<string[]> { | ||
const subdirectoryContents = await fs.promises.readdir( | ||
path.join(process.cwd(), 'cards', game), | ||
{ withFileTypes: true }, | ||
); | ||
const cardTypes = subdirectoryContents.filter((dirent) => | ||
dirent.isDirectory(), | ||
); | ||
return cardTypes.map((cardType) => cardType.name); | ||
} | ||
|
||
private directoryToCommand(directory: string): string { | ||
return directory.replace(/ /gu, '-').toLowerCase(); | ||
} | ||
|
||
private async runGame( | ||
game: string, | ||
interaction: ChatInputCommandInteraction, | ||
): Promise<void> { | ||
const allCards = await this.getCardsOfGame(game); | ||
await Promise.all( | ||
allCards.map(async (card) => { | ||
const count = interaction.options.getNumber(card); | ||
if (count <= 0) { | ||
return; | ||
} | ||
const cards = ( | ||
await Promise.all( | ||
[...Array(count).keys()].map((_) => | ||
this.getCard(game, card), | ||
), | ||
) | ||
).filter((c) => c !== null); | ||
await interaction.reply({ | ||
files: cards.map((c) => new AttachmentBuilder(c)), | ||
}); | ||
}), | ||
); | ||
} | ||
|
||
private async getCard(game: string, type: string): Promise<string> { | ||
const cardsDirectory = path.join(process.cwd(), 'cards', game, type); | ||
const cardsDirectoryContents = await fs.promises.readdir( | ||
cardsDirectory, | ||
{ withFileTypes: true }, | ||
); | ||
const images = cardsDirectoryContents.filter( | ||
(dirent) => | ||
dirent.isFile() && this.fileExtensionIsImage(dirent.name), | ||
); | ||
if (images.length === 0) { | ||
return null; | ||
} | ||
const index = Math.floor(Math.random() * images.length); | ||
const imageName = images[index].name; | ||
return path.join(cardsDirectory, imageName); | ||
} | ||
|
||
private fileExtensionIsImage(fileName: string): boolean { | ||
return ['.png'].includes(path.extname(fileName).toLowerCase()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import { PingCommand } from './ping'; | ||
import { DrawCommand } from './draw'; | ||
|
||
export default [new PingCommand()]; | ||
export default [new DrawCommand()]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,19 @@ | ||
import { ChatInputCommandInteraction } from 'discord.js'; | ||
import { | ||
ApplicationCommandOptionType, | ||
ChatInputCommandInteraction, | ||
} from 'discord.js'; | ||
|
||
export interface DiscordCommandOption { | ||
type: ApplicationCommandOptionType; | ||
name: string; | ||
description: string; | ||
min_value?: number; | ||
options?: DiscordCommandOption[]; | ||
} | ||
|
||
export interface DiscordCommand { | ||
name: string; | ||
description: string; | ||
getOptions(): Promise<DiscordCommandOption[]>; | ||
run(interaction: ChatInputCommandInteraction): Promise<void>; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters