Skip to content

Commit

Permalink
feat: bbexport bbimport bbresolve
Browse files Browse the repository at this point in the history
  • Loading branch information
vberlier committed Oct 31, 2023
1 parent 6a2c366 commit 46b3e0a
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 14 deletions.
17 changes: 3 additions & 14 deletions packages/discord/src/build.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
import { get } from 'https'
import { PoolRunner } from '@beet-bot/runner'
import { Message, Attachment } from 'discord.js'
import { Message } from 'discord.js'
import { BuildInfo } from './report'
import { GuildInfo } from './database'

export const downloadAttachmentAsBase64 = (attachment: Attachment) => {
return new Promise<string>((resolve, reject) => {
get(attachment.url, (res) => {
res.setEncoding('base64')
let body = `data:${attachment.contentType};base64,`
res.on('data', (data) => { body += data })
res.on('end', () => resolve(body))
}).on('error', err => reject(err))
})
}
import { downloadAsBase64Url } from './download'

export const packMessage = async (message?: Message) => {
let input = ''
Expand All @@ -26,7 +15,7 @@ export const packMessage = async (message?: Message) => {
if (attachment.contentType === 'application/zip') {
try {
// Download the attachment now because python's urlopen() gets a 403 (user-agent issues)
const base64 = await downloadAttachmentAsBase64(attachment)
const base64 = await downloadAsBase64Url(attachment.url, attachment.contentType)
input += '```\n@merge_zip(download)\n' + base64 + '\n```\n'
} catch (err) {
console.log(`ERROR: ${err}`)
Expand Down
24 changes: 24 additions & 0 deletions packages/discord/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,30 @@ export const BUILTIN_COMMANDS = [
.setName('action')
.setDescription('The id of the action')
),
new SlashCommandBuilder()
.setName('bbexport')
.setDefaultMemberPermissions(PermissionFlagsBits.Administrator)
.setDescription('Export beet bot action database'),
new SlashCommandBuilder()
.setName('bbimport')
.setDefaultMemberPermissions(PermissionFlagsBits.Administrator)
.setDescription('Import beet bot action database')
.addAttachmentOption(option =>
option
.setName('database')
.setDescription('The json file describing configured actions')
.setRequired(true)
),
new SlashCommandBuilder()
.setName('bbresolve')
.setDefaultMemberPermissions(PermissionFlagsBits.Administrator)
.setDescription('Debug the resolved config of a beet bot action')
.addStringOption(option =>
option
.setName('action')
.setDescription('The id of the action')
.setRequired(true)
),
new SlashCommandBuilder()
.setName('bbstop')
.setDefaultMemberPermissions(PermissionFlagsBits.Administrator)
Expand Down
17 changes: 17 additions & 0 deletions packages/discord/src/download.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { get } from 'https'

export const download = (url: string, encoding: BufferEncoding) => {
return new Promise<string>((resolve, reject) => {
get(url, (res) => {
res.setEncoding(encoding)
let body = ''
res.on('data', (data) => { body += data })
res.on('end', () => resolve(body))
}).on('error', err => reject(err))
})
}

export const downloadAsBase64Url = async (url: string, contentType: string) => {
const content = await download(url, 'base64')
return `data:${contentType};base64,${content}`
}
54 changes: 54 additions & 0 deletions packages/discord/src/handle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { generateGuildCommands } from './commands'
import { ActionDashboardOptions, createActionChoice, createActionDashboard, createEditActionModal } from './widgets'
import { createReport } from './report'
import { invokeBuild, resolveActionOverrides } from './build'
import { download } from './download'

export type BeetBotContext = {
clientId: string
Expand Down Expand Up @@ -198,6 +199,59 @@ export const handleInteractions = ({ clientId, discordClient, discordApi, db, en
guildInfo
}))
}
} else if (interaction.commandName === 'bbexport') {
const guildInfo = await db.getGuildInfo(interaction.guildId)
await interaction.reply({
ephemeral: true,
files: [{
attachment: Buffer.from(JSON.stringify(guildInfo, undefined, 2), 'utf-8'),
name: 'actionDatabase.json'
}]
})
} else if (interaction.commandName === 'bbimport') {
const attachment = interaction.options.getAttachment('database')
if (attachment) {
try {
const content = await download(attachment.url, 'utf-8')
const actionDatabase = JSON.parse(content)
const actionCount = Object.keys(actionDatabase.actions).length
await db.setGuildInfo(interaction.guildId, actionDatabase)
await updateGuildCommands(interaction.guildId)
await interaction.reply({
ephemeral: true,
embeds: [{ description: `Successfully imported ${actionCount} action(s) from attached json`, color: 0x00FF00 }]
})
} catch (err) {
console.log(`ERROR: ${err}`)
await interaction.reply({
ephemeral: true,
embeds: [{ description: 'Failed to import actions from attached json', color: 0xFF0000 }]
})
}
} else {
await interaction.reply({
ephemeral: true,
embeds: [{ description: 'Missing json attachment for action database', color: 0xFF0000 }]
})
}
} else if (interaction.commandName === 'bbresolve') {
const guildInfo = await db.getGuildInfo(interaction.guildId)
const actionId = interaction.options.get('action')?.value
if (typeof actionId === 'string' && guildInfo.actions[actionId]) {
const resolvedConfig = resolveActionOverrides(guildInfo.actions[actionId].config, guildInfo)
await interaction.reply({
ephemeral: true,
files: [{
attachment: Buffer.from(JSON.stringify(resolvedConfig, undefined, 2), 'utf-8'),
name: `${actionId.replace('menu:', '')}.json`
}]
})
} else {
await interaction.reply({
ephemeral: true,
embeds: [{ description: `Could not find action \`${actionId}\``, color: 0xFF0000 }]
})
}
} else if (interaction.commandName === 'bbstop') {
await interaction.reply('```\nShutting down...\n```')
discordClient.destroy()
Expand Down

0 comments on commit 46b3e0a

Please sign in to comment.