Skip to content

Commit

Permalink
Merge pull request #41 from claustra01/wip/command_template
Browse files Browse the repository at this point in the history
各コマンドのテンプレートをまとめて作成
  • Loading branch information
claustra01 authored Dec 17, 2023
2 parents da8be7c + 7358c38 commit 11a1125
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/adapter/commands/help.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Reply, ReplyType } from '../../usecase/types/reply';

export const commandHelp = (): Reply => {
return {
type: ReplyType.Error,
errorText: 'Error: Not Implemented',
};
};
8 changes: 8 additions & 0 deletions src/adapter/commands/recalculate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Reply, ReplyType } from '../../usecase/types/reply';

export const commandRecalculate = (): Reply => {
return {
type: ReplyType.Error,
errorText: 'Error: Not Implemented',
};
};
9 changes: 9 additions & 0 deletions src/adapter/commands/result.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Reply, ReplyType } from '../../usecase/types/reply';

export const commandResult = (args: string[]): Reply => {
console.log(args);
return {
type: ReplyType.Error,
errorText: 'Error: Not Implemented',
};
};
9 changes: 9 additions & 0 deletions src/adapter/commands/rollback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Reply, ReplyType } from '../../usecase/types/reply';

export const commandRollback = (args: string[]): Reply => {
console.log(args);
return {
type: ReplyType.Error,
errorText: 'Error: Not Implemented',
};
};
42 changes: 40 additions & 2 deletions src/infrastructure/discord.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { Client, GatewayIntentBits, Message, Partials } from 'discord.js';
import { commandHelp } from '../adapter/commands/help';
import { commandLink } from '../adapter/commands/link';
import { commandPing } from '../adapter/commands/ping';
import { commandRanking } from '../adapter/commands/ranking';
import { commandRate } from '../adapter/commands/rate';
import { commandRecalculate } from '../adapter/commands/recalculate';
import { commandRegister } from '../adapter/commands/register';
import { commandResult } from '../adapter/commands/result';
import { commandRollback } from '../adapter/commands/rollback';
import { config } from '../config/config';
import { generateReply } from '../usecase/functions/generateReply';
import { commands } from '../usecase/types/commands';
Expand Down Expand Up @@ -80,6 +84,15 @@ export const runDiscordBot = () => {
message.reply(replyText);
break;
}
// help
case commands.help.name: {
if (!checkPermission(message, line, commands.ping.requirePermission))
break;
const reply = commandHelp();
const replyText = generateReply(reply, line);
message.reply(replyText);
break;
}
// rate
case commands.rate.name: {
if (!checkPermission(message, line, commands.ping.requirePermission))
Expand Down Expand Up @@ -118,8 +131,33 @@ export const runDiscordBot = () => {
message.reply(replyText);
break;
}
// other commands...

// result
case commands.result.name: {
if (!checkPermission(message, line, commands.link.requirePermission))
break;
const reply = await commandResult(commandText);
const replyText = generateReply(reply, line);
message.reply(replyText);
break;
}
// rollback
case commands.rollback.name: {
if (!checkPermission(message, line, commands.link.requirePermission))
break;
const reply = await commandRollback(commandText);
const replyText = generateReply(reply, line);
message.reply(replyText);
break;
}
// recalculate
case commands.recalculate.name: {
if (!checkPermission(message, line, commands.link.requirePermission))
break;
const reply = await commandRecalculate();
const replyText = generateReply(reply, line);
message.reply(replyText);
break;
}
// invalid commands
default: {
const invalidCommandError: Reply = {
Expand Down
20 changes: 20 additions & 0 deletions src/usecase/types/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,25 @@ export interface Command {

export const enum CommandList {
ping = 'ping',
help = 'help',
rate = 'rate',
ranking = 'ranking',
register = 'register',
link = 'link',
result = 'result',
rollback = 'rollback',
recalculate = 'recalculate',
}

export const commands: Record<CommandList, Command> = {
ping: {
name: 'ping',
requirePermission: false,
},
help: {
name: 'help',
requirePermission: false,
},
rate: {
name: 'rate',
requirePermission: false,
Expand All @@ -32,4 +40,16 @@ export const commands: Record<CommandList, Command> = {
name: 'link',
requirePermission: true,
},
result: {
name: 'result',
requirePermission: true,
},
rollback: {
name: 'rollback',
requirePermission: true,
},
recalculate: {
name: 'recalculate',
requirePermission: true,
},
};

0 comments on commit 11a1125

Please sign in to comment.