Skip to content

Commit

Permalink
refactor: discord bot and giphy services, rem unused file
Browse files Browse the repository at this point in the history
  • Loading branch information
viliusddd committed Jul 30, 2024
1 parent b62b40e commit a4a75c4
Show file tree
Hide file tree
Showing 14 changed files with 25 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import templates from '@/modules/templates/controller'
import emojis from '@/modules/emojis/controller'
import sprints from '@/modules/sprints/controller'
import users from '@/modules/users/controller'
import type BotClient from './utils/bot'
import type BotClient from '@/services/discord'

export default function createApp(db: Kysely<DB>, bot: BotClient) {
const app = express()
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dotenv/config'
import createApp from './app'
import createDB from './database'
import BotClient from './utils/bot'
import BotClient from './services/discord'

const {DATABASE_URL, PORT} = process.env

Expand Down
2 changes: 1 addition & 1 deletion src/modules/emojis/tests/emojis.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {createFor} from '@tests/utils/records'
import {omit} from 'lodash/fp'
import {fakeEmoji, emojiMatcher} from './utils'
import createApp from '@/app'
import BotClient from '@/utils/bot'
import BotClient from '@/services/discord'

const bot = new BotClient(
process.env.DISCORD_CHANNEL_ID,
Expand Down
2 changes: 1 addition & 1 deletion src/modules/messages/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {discordHandler, jsonRoute, unsupportedRoute} from '@/utils/middleware'
import {MessageNotFound} from './errors'
import {SprintNotFound} from '../sprints/errors'
import {UserNotFound} from '../users/errors'
import BotClient from '@/utils/bot'
import BotClient from '@/services/discord'
import buildRepository from './repository'
import * as schema from './schema'

Expand Down
2 changes: 1 addition & 1 deletion src/modules/messages/services.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'dotenv/config'
import {Selectable, Insertable} from 'kysely'
import type BotClient from '@/utils/bot'
import type BotClient from '@/services/discord'
import {snakeToCamel, randFromArray} from './utils'
import emojisRepo from '../emojis/repository'
import praisesRepo from '../praises/repository'
Expand Down
2 changes: 1 addition & 1 deletion src/modules/messages/tests/messages.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {fakePraise} from '@/modules/praises/tests/utils'
import {fakeSprint} from '@/modules/sprints/tests/utils'
import {fakeTemplate} from '@/modules/templates/tests/utils'
import {fakeUser} from '@/modules/users/tests/utils'
import BotClient from '@/utils/bot'
import BotClient from '@/services/discord'
import createApp from '@/app'

const bot = new BotClient(
Expand Down
6 changes: 0 additions & 6 deletions src/modules/messages/types.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/modules/praises/tests/praises.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {createFor} from '@tests/utils/records'
import {omit} from 'lodash/fp'
import {fakePraise, praiseMatcher} from './utils'
import createApp from '@/app'
import BotClient from '@/utils/bot'
import BotClient from '@/services/discord'

const bot = new BotClient(
process.env.DISCORD_CHANNEL_ID,
Expand Down
2 changes: 1 addition & 1 deletion src/modules/sprints/tests/sprints.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {createFor} from '@tests/utils/records'
import {omit} from 'lodash/fp'
import {fakeSprint, sprintMatcher} from './utils'
import createApp from '@/app'
import BotClient from '@/utils/bot'
import BotClient from '@/services/discord'

const bot = new BotClient(
process.env.DISCORD_CHANNEL_ID,
Expand Down
2 changes: 1 addition & 1 deletion src/modules/templates/tests/templates.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {createFor} from '@tests/utils/records'
import {omit} from 'lodash/fp'
import {fakeTemplate, templateMatcher} from './utils'
import createApp from '@/app'
import BotClient from '@/utils/bot'
import BotClient from '@/services/discord'

const bot = new BotClient(
process.env.DISCORD_CHANNEL_ID,
Expand Down
2 changes: 1 addition & 1 deletion src/modules/users/tests/users.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {createFor} from '@tests/utils/records'
import {omit} from 'lodash/fp'
import {fakeUser, userMatcher} from './utils'
import createApp from '@/app'
import BotClient from '@/utils/bot'
import BotClient from '@/services/discord'

const bot = new BotClient(
process.env.DISCORD_CHANNEL_ID,
Expand Down
File renamed without changes.
18 changes: 11 additions & 7 deletions src/utils/gif.ts → src/services/giphy.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import 'dotenv/config'

export default async function getGif(): Promise<string | undefined> {
const url = `https://api.giphy.com/v1/gifs/random?${new URLSearchParams({
api_key: process.env.GIPHY_API_KEY,
tag: 'celebrate',
rating: 'g'
})}`
type GiphyResponse = {
data: {images: {original: {url: string}}}
errors: {message: string}
}

export default async function getGif(
api_key: string,
tag: string
): Promise<string | undefined> {
const url = `https://api.giphy.com/v1/gifs/random?${new URLSearchParams({api_key, tag, rating: 'g'})}`

try {
const response = await fetch(url)

if (!response.ok) throw new Error(`Response status: ${response.status}`)

const {data, errors} = await response.json()
const {data, errors} = (await response.json()) as GiphyResponse

if (errors) throw new Error(errors.message)

Expand Down
7 changes: 4 additions & 3 deletions src/utils/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import 'dotenv/config'
import type {Response, Request, NextFunction, RequestHandler} from 'express'
import {StatusCodes} from 'http-status-codes'
import type BotClient from './bot'
import getGifUrl from './gif'
import type BotClient from '@/services/discord'
import getGifUrl from '@/services/giphy'
import MethodNotAllowed from './errors/MethodNotAllowed'

type JsonHandler<T> = (
Expand Down Expand Up @@ -46,7 +47,7 @@ type DiscordHandler = (
export const discordHandler: DiscordHandler =
(bot: BotClient) => (req: Request, res: Response, next: NextFunction) => {
res.on('finish', async () => {
const gifUrl = await getGifUrl()
const gifUrl = await getGifUrl(process.env.GIPHY_API_KEY, 'celebrate')
if (!gifUrl) throw new Error('Gif URL is unavailable.')

bot.sendMessage(res.locals.msg)
Expand Down

0 comments on commit a4a75c4

Please sign in to comment.