Skip to content

Commit

Permalink
Merge branch 'preview'
Browse files Browse the repository at this point in the history
  • Loading branch information
lmquang committed Nov 20, 2023
2 parents a63ff0d + 9df53cb commit 91dafee
Show file tree
Hide file tree
Showing 4 changed files with 336 additions and 113 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
<<<<<<< HEAD
=======
## [6.38.4-rc.2](https://github.com/consolelabs/mochi-discord/compare/v6.38.4-rc.1...v6.38.4-rc.2) (2023-11-20)


### Bug Fixes

* fix unleash not trigger to redeploy cmds correctly ([#1524](https://github.com/consolelabs/mochi-discord/issues/1524)) ([740bf9a](https://github.com/consolelabs/mochi-discord/commit/740bf9a1b9063698669f43fbea8ae49974b4de20))

>>>>>>> preview
## [6.38.4](https://github.com/consolelabs/mochi-discord/compare/v6.38.3...v6.38.4) (2023-11-20)


Expand Down
31 changes: 20 additions & 11 deletions src/adapters/unleash/unleash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@ import { Unleash } from "unleash-client"
import { logger } from "logger"

export const appName = "mochi"
export let featureData: Record<string, FeatureData>[] = []
export const unleash = new Unleash({
url: `${UNLEASH_SERVER_HOST}/api`,
appName: appName,
projectName: UNLEASH_PROJECT,
customHeaders: { Authorization: UNLEASH_API_TOKEN },
})

unleash.on("ready", () => {
logger.info("Unleash READY")
})
export let unleash: Unleash

export async function initUnleash() {
unleash = new Unleash({
url: `${UNLEASH_SERVER_HOST}/api`,
appName: appName,
projectName: UNLEASH_PROJECT,
customHeaders: { Authorization: UNLEASH_API_TOKEN },
})

unleash.on("ready", () => {
logger.info("Unleash READY")
})
}

export async function getProjectFeatures(
projectId: string,
Expand Down Expand Up @@ -105,6 +108,12 @@ export async function getFeatures(
const parts = name.split(".")
// Extract values from "strategies" constraints with "contextName" as "guildId"
environmentData.strategies.forEach((strat) => {
if (featureData[parts[parts.length - 1]]) {
featureData[parts[parts.length - 1]].push(
...collectGuildIdValues(strat),
)
return
}
featureData[parts[parts.length - 1]] = collectGuildIdValues(strat)
})
}
Expand Down
56 changes: 53 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import Discord from "discord.js"
import { API_SERVER_HOST, DISCORD_TOKEN, PORT } from "./env"
import { API_SERVER_HOST, DEV, DISCORD_TOKEN, PORT } from "./env"
import { REST } from "@discordjs/rest"
import { logger } from "logger"
import { slashCommands } from "commands"
Expand All @@ -10,12 +10,15 @@ import { run } from "queue/kafka/producer"
import { IS_READY } from "listeners/discord/ready"
import events from "listeners/discord"
import { getTipsAndFacts } from "cache/tip-fact-cache"
import { syncCommands } from "utils/slash-command"
import { registerCommand, syncCommands } from "utils/slash-command"
import { appName, initUnleash, unleash } from "adapters/unleash/unleash"
import { isEqual } from "lodash"
export { slashCommands }

export let emojis = new Map()

let server: Server | null = null
let featureData: Record<string, FeatureData>[] = []

const client = new Discord.Client({
intents: [
Expand Down Expand Up @@ -68,7 +71,54 @@ const body = Object.entries(slashCommands ?? {}).map((e) =>
const rest = new REST({ version: "9" }).setToken(DISCORD_TOKEN)
;(async () => {
try {
await syncCommands()
if (DEV) {
await registerCommand()
} else {
await initUnleash()
unleash.on("changed", (stream: any) => {
if (!unleash.isEnabled(`${appName}.discord.cmd.unleash_on_changed`)) {
return
}

let changed = false
try {
stream.forEach((feature: any) => {
if (
typeof feature.name !== "string" ||
!feature.name.includes(appName)
) {
return
}

// if feature data is empty, it means that onchange is triggered by the first time
if (featureData.length === 0) {
changed = true
}

// assign feature data if feature data is empty to store featureData's stage
if (!featureData[feature.name]) {
featureData[feature.name] = feature
return
}

if (!isEqual(featureData[feature.name], feature)) {
logger.info("Unleash toggles updated")
changed = true
}

featureData[feature.name] = feature
})
} catch (error) {
console.log(error)
}
if (changed) {
syncCommands()
logger.info("Unleash toggles synced")
} else {
logger.info("Unleash toggles not changed")
}
})
}

logger.info("Getting tips and facts.")
await getTipsAndFacts()
Expand Down
Loading

0 comments on commit 91dafee

Please sign in to comment.