-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'v2' of https://github.com/spongedsc/SpongeChat into v2
- Loading branch information
Showing
6 changed files
with
164 additions
and
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* Defines the structure of an Action. | ||
* | ||
* @typedef {object} Action | ||
* @property {import('discord.js').RESTPostAPIApplicationCommandsJSONBody} data The data for the command | ||
* @property {(interaction: import('discord.js').ContextMenuCommandInteraction) => Promise<void> | void} execute The function to execute when the command is called | ||
*/ | ||
|
||
/** | ||
* Defines the predicate to check if an object is a valid Command type. | ||
* | ||
* @type {import('../util/loaders.js').StructurePredicate<Action>} | ||
* @returns {structure is Action} | ||
*/ | ||
export const predicate = (structure) => | ||
Boolean(structure) && | ||
typeof structure === "object" && | ||
"data" in structure && | ||
"execute" in structure && | ||
typeof structure.data === "object" && | ||
typeof structure.execute === "function"; |
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,76 @@ | ||
import { SlashCommandBuilder } from "discord.js"; | ||
import { WorkersAI } from "../util/models/index.js"; | ||
|
||
/** @type {import('./index.js').Command} */ | ||
export default { | ||
data: new SlashCommandBuilder() | ||
.setName("imagine") | ||
.setDescription("Generate an image.") | ||
.addStringOption((o) => | ||
o | ||
.setName("model") | ||
.setDescription("Enter a description of what you're generating.") | ||
.setChoices([ | ||
{ | ||
name: "lykon/dreamshaper-8-lcm", | ||
value: "@cf/lykon/dreamshaper-8-lcm", | ||
}, | ||
{ | ||
name: "bytedance/stable-diffusion-xl-lightning", | ||
value: "@cf/bytedance/stable-diffusion-xl-lightning", | ||
}, | ||
{ | ||
name: "stabilityai/stable-diffusion-xl-base-1.0", | ||
value: "@cf/stabilityai/stable-diffusion-xl-base-1.0", | ||
}, | ||
]) | ||
.setRequired(true), | ||
) | ||
.addStringOption((o) => | ||
o.setName("prompt").setDescription("Enter a description of what you're generating.").setRequired(true), | ||
) | ||
.toJSON(), | ||
async execute(interaction) { | ||
const workersAI = new WorkersAI({ | ||
accountId: process.env.CLOUDFLARE_ACCOUNT_ID, | ||
token: process.env.CLOUDFLARE_ACCOUNT_TOKEN, | ||
}); | ||
|
||
await interaction.deferReply(); | ||
const prompt = interaction.options.getString("prompt"); | ||
const model = interaction.options.getString("model"); | ||
|
||
const callToModel = await (async () => { | ||
const prefix = model?.split("/")?.[0]; | ||
if (prefix !== "@cf") return; | ||
|
||
return await workersAI | ||
.callModel( | ||
{ | ||
model, | ||
input: { | ||
prompt, | ||
}, | ||
}, | ||
true, | ||
) | ||
.then((r) => r.arrayBuffer()) | ||
.catch(() => (e) => { | ||
console.error(e); | ||
return null; | ||
}); | ||
})(); | ||
|
||
if (callToModel === null) | ||
return await interaction.editReply({ | ||
content: `The model did not generate an image.`, | ||
}); | ||
|
||
const buffer = Buffer.from(callToModel); | ||
|
||
await interaction.editReply({ | ||
content: `\`${prompt}\`\n*generated with \`${model}\`*`, | ||
files: [buffer], | ||
}); | ||
}, | ||
}; |
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,44 @@ | ||
import { Events } from "discord.js"; | ||
import chalk from "chalk"; | ||
|
||
/** @type {import('./index.js').Event<Events.InteractionCreate>} */ | ||
export default { | ||
name: Events.InteractionCreate, | ||
async execute(interaction) { | ||
const client = interaction.client; | ||
const commands = client.commandsMap; | ||
const actions = client.actionsMap; | ||
|
||
if (interaction.isContextMenuCommand()) { | ||
const action = actions.get(interaction.commandName); | ||
|
||
if (!action) { | ||
throw new Error(`Action '${interaction.commandName}' not found.`); | ||
} | ||
|
||
await action.execute(interaction).catch((e) => { | ||
console.log( | ||
`${chalk.bold.red("Action")} exception (${interaction.commandName}): \n`, | ||
e, | ||
` ${Temporal.Now.instant().toLocaleString("en-GB", { timeZone: "Etc/UTC", timeZoneName: "short" })}`, | ||
); | ||
}); | ||
} | ||
|
||
if (interaction.isChatInputCommand()) { | ||
const command = commands.get(interaction.commandName); | ||
|
||
if (!command) { | ||
throw new Error(`Command '${interaction.commandName}' not found.`); | ||
} | ||
|
||
await command.execute(interaction).catch((e) => { | ||
console.log( | ||
`${chalk.bold.red("Command")} exception (${interaction.commandName}): \n`, | ||
e, | ||
` ${Temporal.Now.instant().toLocaleString("en-GB", { timeZone: "Etc/UTC", timeZoneName: "short" })}`, | ||
); | ||
}); | ||
} | ||
}, | ||
}; |
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,31 +1,32 @@ | ||
import process from 'node:process'; | ||
import { URL } from 'node:url'; | ||
import { API } from '@discordjs/core/http-only'; | ||
import { REST } from 'discord.js'; | ||
import { Environment } from './helpers.js'; | ||
import { loadCommands } from './loaders.js'; | ||
import process from "node:process"; | ||
import { URL } from "node:url"; | ||
import { API } from "@discordjs/core/http-only"; | ||
import { REST } from "discord.js"; | ||
import { Environment } from "./helpers.js"; | ||
import { loadCommands } from "./loaders.js"; | ||
|
||
import chalk from 'chalk'; | ||
import chalk from "chalk"; | ||
|
||
export const cmdRollout = async () => { | ||
const env = new Environment(); | ||
|
||
const commands = await loadCommands(new URL('../commands/', import.meta.url)); | ||
const commandData = [...commands.values()].map((command) => command.data); | ||
const commands = await loadCommands(new URL("../commands/", import.meta.url)); | ||
const actions = await loadCommands(new URL("../actions/", import.meta.url)); | ||
const data = [...commands.values(), ...actions.values()].map((command) => command.data); | ||
|
||
const rest = new REST({ version: '10' }).setToken(process.env.DISCORD_TOKEN); | ||
const rest = new REST({ version: "10" }).setToken(process.env.DISCORD_TOKEN); | ||
const api = new API(rest); | ||
|
||
const result = | ||
env.getRuntimeScenario() === 'development' | ||
env.getRuntimeScenario() === "development" | ||
? await api.applicationCommands.bulkOverwriteGuildCommands( | ||
process.env.APPLICATION_ID, | ||
process.env.DEV_GUILD, | ||
commandData, | ||
data, | ||
) | ||
: await api.applicationCommands.bulkOverwriteGlobalCommands(process.env.APPLICATION_ID, commandData); | ||
: await api.applicationCommands.bulkOverwriteGlobalCommands(process.env.APPLICATION_ID, data); | ||
|
||
console.log( | ||
`${chalk.bold.green('Core')} Successfully registered ${chalk.bold(result.length)} commands (${Temporal.Now.instant().toLocaleString('en-GB', { timeZone: 'Etc/UTC', timeZoneName: 'short' })})`, | ||
`${chalk.bold.green("Core")} Successfully registered ${chalk.bold(result.length)} commands/actions (${Temporal.Now.instant().toLocaleString("en-GB", { timeZone: "Etc/UTC", timeZoneName: "short" })})`, | ||
); | ||
}; |
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