Skip to content

Commit

Permalink
feat: 引数取得処理の共通化
Browse files Browse the repository at this point in the history
  • Loading branch information
book000 committed Aug 27, 2023
1 parent f48cd49 commit 3e71ba9
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
1 change: 1 addition & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ rules:
'@typescript-eslint/no-explicit-any': off
'@typescript-eslint/ban-ts-comment': off
'unicorn/prefer-top-level-await': off
'unicorn/prevent-abbreviations': off
'unicorn/no-null': off
'no-console': error
11 changes: 7 additions & 4 deletions src/commands/bassline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ export class BasslineCommand implements BaseCommand {
].join('\n')
}

async execute(_discord: Discord, message: Message<boolean>): Promise<void> {
const [, ...suspectName] = message.content.split(' ')
async execute(
_discord: Discord,
message: Message<boolean>,
args: string[]
): Promise<void> {
await message.channel.send(
suspectName.length > 0
? this.getBasslineText(suspectName.join(' '))
args.length > 0
? this.getBasslineText(args.join(' '))
: this.getBasslineText()
)
}
Expand Down
26 changes: 22 additions & 4 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,30 @@ interface PermissionPermission {
export type Permission = UserPermission | RolePermission | PermissionPermission

export abstract class BaseCommand {
/** 名前: コマンドの名前 */
/**
* 名前: コマンドの名前
*
* @returns {string} コマンドの名前
*/
abstract get name(): string

/** 権限: コマンドの実行に必要なユーザー・ロール・パーミッション。NULLが指定された場合はすべて許可 */
/**
* 権限: コマンドの実行に必要なユーザー・ロール・パーミッション。NULLが指定された場合はすべて許可
*
* @returns {Permission[] | null} コマンドの実行に必要なユーザー・ロール・パーミッション
*/
abstract get permissions(): Permission[] | null

/** 実行: コマンドの実行定義 */
abstract execute(discord: Discord, message: Message): Promise<void>
/**
* 実行: コマンドの実行定義
*
* @param {Discord} discord Discordインスタンス
* @param {Message} message Discordメッセージ
* @param {string[]} args コマンド引数
*/
abstract execute(
discord: Discord,
message: Message,
args: string[]
): Promise<void>
}
6 changes: 5 additions & 1 deletion src/discord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,18 @@ export class Discord {
}

async onMessageCreate(message: Message) {
const logger = Logger.configure('Discord.onMessageCreate')
const command = Discord.commands.find((command) =>
message.content.startsWith(`/${command.name}`)
)
if (!command) {
return
}

command.execute(this, message)
logger.info(`👌 ${message.author.tag}: execute ${command.name}`)

const [, ...args] = message.content.split(' ')
command.execute(this, message, args)
}

waitReady() {
Expand Down

0 comments on commit 3e71ba9

Please sign in to comment.