-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
98 additions
and
109 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
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
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
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
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
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,14 +1,13 @@ | ||
import { Context } from "./context"; | ||
|
||
/** | ||
* Typeguards are most helpful when you have a union type and you want to narrow it down to a specific one. | ||
* In other words, if `SupportedEvents` has multiple types then these restrict the scope | ||
* of `context` to a specific event payload. | ||
*/ | ||
function isCommentEvent(context: Context): context is Context<"issue_comment.created"> { | ||
return context.eventName === "issue_comment.created"; | ||
} | ||
|
||
/** | ||
* Restricts the scope of `context` to the `issue_comment.created` payload. | ||
*/ | ||
export function isIssueCommentEvent(context: Context): context is Context<"issue_comment.created"> { | ||
return context.eventName === "issue_comment.created"; | ||
return isCommentEvent(context) && context.payload.comment.body.startsWith("/"); | ||
} | ||
|
||
export function isIssueClosedEvent(context: Context): context is Context<"issues.closed"> { | ||
return context.eventName === "issues.closed"; | ||
} |
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,71 @@ | ||
import { STRINGS } from "../../tests/__mocks__/strings"; | ||
import { faucet } from "../handlers/faucet"; | ||
import { isIssueCommentEvent } from "../types/typeguards"; | ||
import { logAndComment, throwError } from "./logger"; | ||
import { Context } from "../types"; | ||
import { register } from "../handlers/register"; | ||
|
||
export async function handleSlashCommand(context: Context) { | ||
if (isIssueCommentEvent(context)) { | ||
const { | ||
payload: { | ||
comment: { body }, | ||
}, | ||
} = context; | ||
const [command, ...args] = body.split(" "); | ||
const params = await parseArgs( | ||
context, | ||
args.filter((arg) => arg !== "") | ||
); | ||
switch (command) { | ||
case "/register": | ||
return register(context, params); | ||
case "/faucet": | ||
if (Object.keys(params).length < 2) { | ||
await logAndComment(context, "error", STRINGS.INVALID_USE_OF_ARGS); | ||
throwError(STRINGS.INVALID_USE_OF_ARGS); | ||
} | ||
return faucet(context, params); | ||
default: | ||
throwError("Unknown command", { command }); | ||
} | ||
} else { | ||
throwError("Unknown event type", { eventName: context.eventName }); | ||
} | ||
} | ||
|
||
export async function parseArgs(context: Context<"issue_comment.created">, args: string[]) { | ||
if (args.length === 4) { | ||
return { | ||
recipient: args[0].toLowerCase(), | ||
networkId: args[1], | ||
amount: BigInt(args[2]), | ||
token: args[3].toLowerCase(), | ||
}; | ||
} else if (args.length === 3) { | ||
return { | ||
recipient: args[0].toLowerCase(), | ||
networkId: args[1], | ||
amount: BigInt(args[2]), | ||
token: "native", | ||
}; | ||
} else if (args.length === 2) { | ||
return { | ||
recipient: args[0].toLowerCase(), | ||
networkId: args[1], | ||
amount: BigInt(0), | ||
token: "native", | ||
}; | ||
} else if (args.length < 2) { | ||
// only used in /register | ||
return { | ||
recipient: context.payload.comment.user?.login.toLowerCase() ?? context.payload.sender.login.toLowerCase(), | ||
networkId: "1", | ||
amount: BigInt(0), | ||
token: "native", | ||
}; | ||
} else { | ||
await logAndComment(context, "error", STRINGS.INVALID_USE_OF_ARGS); | ||
throwError(STRINGS.INVALID_USE_OF_ARGS); | ||
} | ||
} |