ADD TO SHITPOSTS
This commit is contained in:
parent
7d035b1479
commit
4eb781732b
5 changed files with 63 additions and 16 deletions
|
|
@ -10,7 +10,7 @@ export abstract class ContextCommand<T extends User | Message> extends ICommand
|
||||||
T extends Message ? ApplicationCommandType.Message :
|
T extends Message ? ApplicationCommandType.Message :
|
||||||
never;
|
never;
|
||||||
abstract contextDefinition: ContextMenuCommandBuilder
|
abstract contextDefinition: ContextMenuCommandBuilder
|
||||||
abstract run(interaction: ContextMenuCommandInteraction, target: T extends User ? User : T extends Message ? Message : never): Promise<void>
|
abstract run(interaction: ContextMenuCommandInteraction, target: T extends User ? User : T extends Message ? Message : never, config: Config): Promise<void>
|
||||||
}
|
}
|
||||||
|
|
||||||
export abstract class Command extends ICommand {
|
export abstract class Command extends ICommand {
|
||||||
|
|
|
||||||
41
src/commands/addtoshitpost.ts
Normal file
41
src/commands/addtoshitpost.ts
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
import {
|
||||||
|
ApplicationCommandType, type Attachment,
|
||||||
|
ContextMenuCommandBuilder,
|
||||||
|
ContextMenuCommandInteraction,
|
||||||
|
Message
|
||||||
|
} from "discord.js";
|
||||||
|
import { ContextCommand } from "../command.ts";
|
||||||
|
import {PutObjectCommand, type S3Client} from "@aws-sdk/client-s3";
|
||||||
|
import type {Config} from "../config.ts";
|
||||||
|
import {BUCKETNAME} from "./shitpost.ts";
|
||||||
|
|
||||||
|
export default class Mock extends ContextCommand<Message> {
|
||||||
|
targetType: ApplicationCommandType.Message = ApplicationCommandType.Message;
|
||||||
|
contextDefinition: ContextMenuCommandBuilder =
|
||||||
|
new ContextMenuCommandBuilder()
|
||||||
|
.setName('AddToShitposts')
|
||||||
|
.setType(ApplicationCommandType.Message)
|
||||||
|
async run(interaction: ContextMenuCommandInteraction, target: Message, config:Config): Promise<void> {
|
||||||
|
await interaction.deferReply()
|
||||||
|
await interaction.followUp({content: "uploading..."})
|
||||||
|
for (const [_, attachment] of target.attachments) {
|
||||||
|
|
||||||
|
const response = await fetch(attachment.proxyURL);
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
await interaction.reply({ content: "discord shat itself??????" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const buffer = await response.arrayBuffer();
|
||||||
|
|
||||||
|
const command = new PutObjectCommand({
|
||||||
|
Bucket: BUCKETNAME,
|
||||||
|
Key: attachment.name,
|
||||||
|
Body: Buffer.from(buffer),
|
||||||
|
});
|
||||||
|
await config.s3.send(command)
|
||||||
|
}
|
||||||
|
await interaction.editReply({content: "shits have been posted"})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -5,15 +5,17 @@ import {
|
||||||
InteractionContextType,
|
InteractionContextType,
|
||||||
SlashCommandBuilder
|
SlashCommandBuilder
|
||||||
} from "discord.js";
|
} from "discord.js";
|
||||||
import { config, type Config } from "../config.ts";
|
import {config, type Config} from "../config.ts";
|
||||||
import {ListObjectsV2Command, type S3Client} from "@aws-sdk/client-s3";
|
import {ListObjectsV2Command, type S3Client} from "@aws-sdk/client-s3";
|
||||||
import {inspect} from "node:util";
|
import {inspect} from "node:util";
|
||||||
|
|
||||||
|
export const BUCKETNAME = "shitposts" as const;
|
||||||
export default class ShitPostCommand extends Command {
|
export default class ShitPostCommand extends Command {
|
||||||
|
|
||||||
|
|
||||||
async run(interaction: ChatInputCommandInteraction, config: Config, s3: S3Client) {
|
async run(interaction: ChatInputCommandInteraction, config: Config, s3: S3Client) {
|
||||||
const shitpost = interaction.options.getString("shitpost")!;
|
await interaction.deferReply();
|
||||||
|
const shitpost = interaction.options.getString("shitpost")!;
|
||||||
|
|
||||||
const shitpostUrl = "https://sp.amy.rip/" + encodeURIComponent(shitpost);
|
const shitpostUrl = "https://sp.amy.rip/" + encodeURIComponent(shitpost);
|
||||||
|
|
||||||
|
|
@ -21,27 +23,28 @@ export default class ShitPostCommand extends Command {
|
||||||
const response = await fetch(shitpostUrl);
|
const response = await fetch(shitpostUrl);
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
await interaction.reply({ content: "S3 bucket shat itself??????" });
|
await interaction.followUp({content: "S3 bucket shat itself??????"});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const buffer = await response.arrayBuffer();
|
const buffer = await response.arrayBuffer();
|
||||||
const attachment = new AttachmentBuilder(Buffer.from(buffer), { name: shitpost });
|
const attachment = new AttachmentBuilder(Buffer.from(buffer), {name: shitpost});
|
||||||
|
|
||||||
await interaction.reply({ files: [attachment] });
|
await interaction.followUp({files: [attachment]});
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
await interaction.reply({ content: "fileproccessing shat itself" });
|
await interaction.followUp({content: "fileproccessing shat itself"});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async autoComplete(interaction: AutocompleteInteraction, config: Config, option: AutocompleteFocusedOption, s3: S3Client): Promise<void> {
|
async autoComplete(interaction: AutocompleteInteraction, config: Config, option: AutocompleteFocusedOption, s3: S3Client): Promise<void> {
|
||||||
|
|
||||||
await interaction.respond((await s3.send(new ListObjectsV2Command({ Bucket: "shitposts" }))).Contents!
|
await interaction.respond((await s3.send(new ListObjectsV2Command({Bucket: BUCKETNAME}))).Contents!
|
||||||
.filter((i): i is { Key: string } => !!i.Key)
|
.filter((i): i is { Key: string } => !!i.Key)
|
||||||
.map(key => ({ name: key.Key, value: key.Key })))
|
.map(key => ({name: key.Key, value: key.Key})))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
slashCommand = new SlashCommandBuilder()
|
slashCommand = new SlashCommandBuilder()
|
||||||
.setName("shitpost")
|
.setName("shitpost")
|
||||||
.setDescription("shitpost with S3!!!!!").setIntegrationTypes([
|
.setDescription("shitpost with S3!!!!!").setIntegrationTypes([
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
import rawconfig from "../config.json" with {type: "json"};
|
import rawconfig from "../config.json" with {type: "json"};
|
||||||
import {z} from 'zod';
|
import {z} from 'zod';
|
||||||
|
import type {S3Client} from "@aws-sdk/client-s3";
|
||||||
const configT = z.object({
|
const configT = z.object({
|
||||||
token: z.string(),
|
token: z.string(),
|
||||||
listenbrainzAccount: z.string(),
|
listenbrainzAccount: z.string(),
|
||||||
|
|
@ -9,7 +10,8 @@ const configT = z.object({
|
||||||
R2AccessKeyId: z.string(),
|
R2AccessKeyId: z.string(),
|
||||||
R2SecretAccessKey: z.string(),
|
R2SecretAccessKey: z.string(),
|
||||||
});
|
});
|
||||||
export type Config = z.infer<typeof configT>;
|
export type RawConfig = z.infer<typeof configT>;
|
||||||
export const config: Config = configT.parse(rawconfig);
|
export type Config = RawConfig & {s3: S3Client}
|
||||||
|
export const config: RawConfig = configT.parse(rawconfig);
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ import path from "node:path";
|
||||||
import fs from "node:fs";
|
import fs from "node:fs";
|
||||||
import { Command, ContextCommand, ICommand } from "./command.ts";
|
import { Command, ContextCommand, ICommand } from "./command.ts";
|
||||||
import { fileURLToPath } from "url";
|
import { fileURLToPath } from "url";
|
||||||
import { config } from "./config.ts";
|
import {type Config, config} from "./config.ts";
|
||||||
import {ListObjectsV2Command, S3Client} from "@aws-sdk/client-s3";
|
import {ListObjectsV2Command, S3Client} from "@aws-sdk/client-s3";
|
||||||
|
|
||||||
const __filename = fileURLToPath(import.meta.url);
|
const __filename = fileURLToPath(import.meta.url);
|
||||||
|
|
@ -29,6 +29,7 @@ const S3 = new S3Client({
|
||||||
secretAccessKey: config.R2SecretAccessKey,
|
secretAccessKey: config.R2SecretAccessKey,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
const enrichedConfig = {...config, s3:S3} satisfies Config;
|
||||||
const commandDir = path.join(__dirname, "commands");
|
const commandDir = path.join(__dirname, "commands");
|
||||||
for (const file of fs.readdirSync(commandDir)) {
|
for (const file of fs.readdirSync(commandDir)) {
|
||||||
if (!file.endsWith('.ts')) continue
|
if (!file.endsWith('.ts')) continue
|
||||||
|
|
@ -80,7 +81,7 @@ client.on(Events.InteractionCreate, async (interaction) => {
|
||||||
if (command.targetType != (interaction.isUserContextMenuCommand() ? ApplicationCommandType.User : ApplicationCommandType.Message))
|
if (command.targetType != (interaction.isUserContextMenuCommand() ? ApplicationCommandType.User : ApplicationCommandType.Message))
|
||||||
console.error("Out of date discord definition of this context command")
|
console.error("Out of date discord definition of this context command")
|
||||||
try {
|
try {
|
||||||
await command.run(interaction, interaction.isUserContextMenuCommand() ? interaction.targetUser : interaction.targetMessage)
|
await command.run(interaction, interaction.isUserContextMenuCommand() ? interaction.targetUser : interaction.targetMessage, enrichedConfig)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("error during context command execution: " + commandName, e)
|
console.error("error during context command execution: " + commandName, e)
|
||||||
interaction.reply("something sharted itself")
|
interaction.reply("something sharted itself")
|
||||||
|
|
@ -98,7 +99,7 @@ client.on(Events.InteractionCreate, async (interaction) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await command.run(interaction, config, S3);
|
await command.run(interaction, enrichedConfig, S3);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("error during command execution: " + commandName, e)
|
console.error("error during command execution: " + commandName, e)
|
||||||
interaction.reply("something sharted itself")
|
interaction.reply("something sharted itself")
|
||||||
|
|
@ -117,7 +118,7 @@ client.on(Events.InteractionCreate, async (interaction) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await command.autoComplete(interaction, config, interaction.options.getFocused(true), S3);
|
await command.autoComplete(interaction, enrichedConfig, interaction.options.getFocused(true), S3);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("error during command execution: " + commandName, e)
|
console.error("error during command execution: " + commandName, e)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue