Skip to content

Commit

Permalink
Merge pull request #193 from elsoul/discord
Browse files Browse the repository at this point in the history
add - skeet deploy --discord
  • Loading branch information
POPPIN-FUMI authored Nov 14, 2023
2 parents 24fe0da + 5a04d51 commit f9929d1
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 66 deletions.
10 changes: 8 additions & 2 deletions src/cli/deploy/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { program } from '@/index'
import { DEFAULT_FUNCTION_NAME, program } from '@/index'
import { deploy } from './deploy'
import { yarnBuild } from '../yarn/yarnBuild'
import { importConfig } from '@/lib'
import { FUNCTIONS_PATH, importConfig } from '@/lib'
import { firebaseFunctionsDeploy } from './firebaseDeploy'
import { spawnSync } from 'node:child_process'

export const deployCommands = async () => {
program
.command('deploy')
.option('-f, --function <function>', 'Function Name. e.g. skeet:root')
.option('-d, --discord', 'Deploy Discord Commands', false)
.description('Deploy Skeet APP to Firebase')
.action(async (options) => {
if (options.function) {
Expand All @@ -21,6 +23,10 @@ export const deployCommands = async () => {
`functions:${functionName}:${methodName}`,
)
return
} else if (options.discord) {
const cmd = `yarn --cwd ${FUNCTIONS_PATH}/${DEFAULT_FUNCTION_NAME} discord:deploy`
spawnSync(cmd, { stdio: 'inherit', shell: true })
return
}
await deploy()
})
Expand Down
45 changes: 0 additions & 45 deletions src/cli/sub/add/addDiscordCmds.ts

This file was deleted.

24 changes: 22 additions & 2 deletions src/cli/sub/add/addDiscordWebhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,36 @@ import { discordRouter } from '@/templates/discord/discordRouter'
import chalk from 'chalk'
import { existsSync, writeFileSync } from 'fs'
import { insertFunction } from './addMethod'
import { helloAction } from '@/templates/discord/helloAction'
import { helloCommand } from '@/templates/discord/helloCommand'
import { helloIndex } from '@/templates/discord/helloIndex'
import { helloMessage } from '@/templates/discord/helloMessage'
import { deployCommands } from '@/templates/discord/deployCommands'

export const addDiscordWebhook = () => {
try {
const { body, filePath } = discordRouter()
let { body, filePath } = discordRouter()
if (existsSync(filePath)) {
return false
}
writeFileSync(filePath, body)
;({ body, filePath } = helloAction())
writeFileSync(filePath, body)
;({ body, filePath } = helloCommand())
writeFileSync(filePath, body)
;({ body, filePath } = helloIndex())
writeFileSync(filePath, body)
;({ body, filePath } = deployCommands())
writeFileSync(filePath, body)
;({ body, filePath } = helloMessage())
writeFileSync(filePath, body)

console.log(chalk.green(`✅ discordRouter added 🎉`))
console.log(chalk.white(`🔗 ${filePath}`))
console.log(chalk.green(`✅ helloAction added 🎉`))
console.log(chalk.green(`✅ helloCommand added 🎉`))
console.log(chalk.green(`✅ helloIndex added 🎉`))
console.log(chalk.green(`✅ deployCommands added 🎉`))
console.log(chalk.green(`✅ helloMessage added 🎉`))
const indexFile = `${FUNCTIONS_PATH}/${DEFAULT_FUNCTION_NAME}/src/index.ts`
const methodName = 'discordRouter'
insertFunction(indexFile, methodName)
Expand Down
3 changes: 0 additions & 3 deletions src/cli/sub/add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ import chalk from 'chalk'
import { addGraphQL } from './addGraphQL'
import { addScriptToPackageJson } from '@/lib/files/addScriptToPackageJson'
import { addDiscordWebhook } from './addDiscordWebhook'
import { addDiscordCmd } from './addDiscordCmds'
import { addDependencyToPackageJson } from '@/lib/files/addDependencyToPackageJson'
import inquirer from 'inquirer'
import { yarnCmdRun } from '@/cli/yarn/yarn'
import { spawnSync } from 'child_process'

export const addSubCommands = async () => {
Expand Down Expand Up @@ -115,7 +113,6 @@ export const addSubCommands = async () => {
console.log(chalk.yellow('⚠️ Discord Webhook already exists'))
return
}
addDiscordCmd()
const packageJsonPath = `${FUNCTIONS_PATH}/${DEFAULT_FUNCTION_NAME}/package.json`
addScriptToPackageJson(
packageJsonPath,
Expand Down
10 changes: 0 additions & 10 deletions src/templates/discord/discordRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
ActionData,
CommandData,
DiscordRouterParams,
InteractionResponseType,
InteractionType,
verifyKey,
} from '@skeet-framework/discord-utils'
Expand Down Expand Up @@ -54,23 +53,14 @@ export const discordRouter = onRequest(
const options = req.body.data as CommandData
if (options.name.match(/^hello$/)) {
await helloAction(res, db, DISCORD_TOKEN.value(), req.body)
return
}
} else {
// Button action
const { custom_id } = req.body.data as ActionData
if (custom_id.match(/^hello-button$/)) {
await helloAction(res, db, DISCORD_TOKEN.value(), req.body)
return
}
res.send({
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
})
return
}
res.send({
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
})
} catch (error) {
console.log(\`discordRouter: \${error}\`)
res.status(500).json({ status: 'error', message: String(error) })
Expand Down
12 changes: 9 additions & 3 deletions src/templates/discord/helloAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ export const helloAction = () => {
const filePath = `${fileDir}/helloAction.ts`
const body = `import {
DiscordRouterParams,
InteractionResponseType,
deferResponse,
updateResponse,
} from '@skeet-framework/discord-utils'
import { Response } from 'firebase-functions/v1'
import { inspect } from 'util'
export const helloAction = async (
res: Response,
Expand All @@ -21,8 +23,12 @@ export const helloAction = async (
body: DiscordRouterParams,
) => {
console.log('helloAction')
res.send({
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
console.log(inspect(body))
await deferResponse(discordToken, body.id, body.token)
// define your logic here
await updateResponse(discordToken, body.application_id, body.token, {
content: 'hello response!',
flags: 64,
})
return true
}`
Expand Down
2 changes: 1 addition & 1 deletion src/templates/discord/helloMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const helloMessage = () => {
export const helloMessage = () => {
const body = {
content: 'hello world',
content: 'hello button',
components: [
{
type: MessageComponentTypes.ACTION_ROW,
Expand Down

0 comments on commit f9929d1

Please sign in to comment.