add file support (really) & slash.commands.bulkEdit

This commit is contained in:
DjDeveloperr 2021-01-24 19:50:49 +05:30
parent 8734e9a7a6
commit ebb4be897a
10 changed files with 85 additions and 15 deletions

6
mod.ts
View file

@ -59,7 +59,7 @@ export { NewsChannel } from './src/structures/guildNewsChannel.ts'
export { VoiceChannel } from './src/structures/guildVoiceChannel.ts' export { VoiceChannel } from './src/structures/guildVoiceChannel.ts'
export { Invite } from './src/structures/invite.ts' export { Invite } from './src/structures/invite.ts'
export * from './src/structures/member.ts' export * from './src/structures/member.ts'
export { Message } from './src/structures/message.ts' export { Message, MessageAttachment } from './src/structures/message.ts'
export { MessageMentions } from './src/structures/messageMentions.ts' export { MessageMentions } from './src/structures/messageMentions.ts'
export { export {
Presence, Presence,
@ -69,6 +69,7 @@ export {
export { Role } from './src/structures/role.ts' export { Role } from './src/structures/role.ts'
export { Snowflake } from './src/utils/snowflake.ts' export { Snowflake } from './src/utils/snowflake.ts'
export { TextChannel, GuildTextChannel } from './src/structures/textChannel.ts' export { TextChannel, GuildTextChannel } from './src/structures/textChannel.ts'
export type { AllMessageOptions } from './src/structures/textChannel.ts'
export { MessageReaction } from './src/structures/messageReaction.ts' export { MessageReaction } from './src/structures/messageReaction.ts'
export { User } from './src/structures/user.ts' export { User } from './src/structures/user.ts'
export { Webhook } from './src/structures/webhook.ts' export { Webhook } from './src/structures/webhook.ts'
@ -97,7 +98,8 @@ export type {
GuildChannelPayload, GuildChannelPayload,
GuildTextChannelPayload, GuildTextChannelPayload,
GuildVoiceChannelPayload, GuildVoiceChannelPayload,
GroupDMChannelPayload GroupDMChannelPayload,
MessageOptions
} from './src/types/channel.ts' } from './src/types/channel.ts'
export type { EmojiPayload } from './src/types/emoji.ts' export type { EmojiPayload } from './src/types/emoji.ts'
export type { export type {

View file

@ -198,7 +198,7 @@ export class Client extends HarmonyEventEmitter<ClientEvents> {
enabled: options.enableSlash enabled: options.enableSlash
}) })
this.fetchGatewayInfo = options.fetchGatewayInfo ?? false this.fetchGatewayInfo = options.fetchGatewayInfo ?? true
if (this.token === undefined) { if (this.token === undefined) {
try { try {

View file

@ -271,7 +271,8 @@ export class RESTManager {
if (body?.file !== undefined) { if (body?.file !== undefined) {
const form = new FormData() const form = new FormData()
form.append('file', body.file.blob, body.file.name) form.append('file', body.file.blob, body.file.name)
form.append('payload_json', JSON.stringify({ ...body, file: undefined })) const json = JSON.stringify(body)
form.append('payload_json', json)
body.file = form body.file = form
} else if ( } else if (
body !== undefined && body !== undefined &&

View file

@ -314,6 +314,23 @@ export class SlashCommandsManager {
return new SlashCommand(this, data) return new SlashCommand(this, data)
} }
/** Bulk Edit Global or Guild Slash Commands */
async bulkEdit(
cmds: Array<SlashCommandPartial | SlashCommandPayload>,
guild?: Guild | string
): Promise<SlashCommandsManager> {
const route =
guild === undefined
? this.rest.api.applications[this.slash.getID()].commands
: this.rest.api.applications[this.slash.getID()].guilds[
typeof guild === 'string' ? guild : guild.id
].commands
await route.put(cmds)
return this
}
} }
export type SlashCommandHandlerCallback = (interaction: Interaction) => any export type SlashCommandHandlerCallback = (interaction: Interaction) => any

View file

@ -3,7 +3,7 @@ import {
Attachment, Attachment,
MessageActivity, MessageActivity,
MessageApplication, MessageApplication,
MessageOption, MessageOptions,
MessagePayload, MessagePayload,
MessageReference MessageReference
} from '../types/channel.ts' } from '../types/channel.ts'
@ -19,7 +19,7 @@ import { MessageReactionsManager } from '../managers/messageReactions.ts'
import { MessageSticker } from './messageSticker.ts' import { MessageSticker } from './messageSticker.ts'
import { Emoji } from './emoji.ts' import { Emoji } from './emoji.ts'
type AllMessageOptions = MessageOption | Embed type AllMessageOptions = MessageOptions | Embed
export class Message extends Base { export class Message extends Base {
id: string id: string
@ -171,3 +171,39 @@ export class Message extends Base {
return this.channel.removeReaction(this, emoji, user) return this.channel.removeReaction(this, emoji, user)
} }
} }
const encoder = new TextEncoder()
/** Message Attachment that can be sent while Creating Message */
export class MessageAttachment {
name: string
blob: Blob
constructor(name: string, blob: Blob | Uint8Array | string) {
this.name = name
this.blob =
typeof blob === 'string'
? new Blob([encoder.encode(blob)])
: blob instanceof Uint8Array
? new Blob([blob])
: blob
}
/** Load an Message Attachment from local file or URL */
static async load(
path: string,
filename?: string
): Promise<MessageAttachment> {
const blob = path.startsWith('http')
? await fetch(path).then((res) => res.blob())
: await Deno.readFile(path)
if (filename === undefined) {
const split = path.replaceAll('\\', '/').split('/').pop()
if (split !== undefined) filename = split.split('?')[0].split('#')[0]
else filename = 'unnamed_attachment'
}
return new MessageAttachment(filename, blob)
}
}

View file

@ -1,5 +1,5 @@
import { Client } from '../models/client.ts' import { Client } from '../models/client.ts'
import { MessageOption } from '../types/channel.ts' import { MessageOptions } from '../types/channel.ts'
import { INTERACTION_CALLBACK, WEBHOOK_MESSAGE } from '../types/endpoint.ts' import { INTERACTION_CALLBACK, WEBHOOK_MESSAGE } from '../types/endpoint.ts'
import { import {
InteractionData, InteractionData,
@ -16,7 +16,7 @@ import { GuildTextChannel, TextChannel } from './textChannel.ts'
import { User } from './user.ts' import { User } from './user.ts'
import { Webhook } from './webhook.ts' import { Webhook } from './webhook.ts'
interface WebhookMessageOptions extends MessageOption { interface WebhookMessageOptions extends MessageOptions {
embeds?: Embed[] embeds?: Embed[]
name?: string name?: string
avatar?: string avatar?: string

View file

@ -3,7 +3,7 @@ import { MessagesManager } from '../managers/messages.ts'
import { Client } from '../models/client.ts' import { Client } from '../models/client.ts'
import { import {
GuildTextChannelPayload, GuildTextChannelPayload,
MessageOption, MessageOptions,
MessagePayload, MessagePayload,
MessageReference, MessageReference,
ModifyGuildTextChannelOption, ModifyGuildTextChannelOption,
@ -28,7 +28,7 @@ import { Member } from './member.ts'
import { Message } from './message.ts' import { Message } from './message.ts'
import { User } from './user.ts' import { User } from './user.ts'
export type AllMessageOptions = MessageOption | Embed export type AllMessageOptions = MessageOptions | Embed
export class TextChannel extends Channel { export class TextChannel extends Channel {
lastMessageID?: string lastMessageID?: string
@ -105,7 +105,7 @@ export class TextChannel extends Channel {
async editMessage( async editMessage(
message: Message | string, message: Message | string,
text?: string, text?: string,
option?: MessageOption option?: MessageOptions
): Promise<Message> { ): Promise<Message> {
if (text === undefined && option === undefined) { if (text === undefined && option === undefined) {
throw new Error('Either text or option is necessary.') throw new Error('Either text or option is necessary.')
@ -135,6 +135,7 @@ export class TextChannel extends Channel {
return res return res
} }
/** Add a reaction to a Message in this Channel */
async addReaction( async addReaction(
message: Message | string, message: Message | string,
emoji: Emoji | string emoji: Emoji | string
@ -153,6 +154,7 @@ export class TextChannel extends Channel {
) )
} }
/** Remove Reaction from a Message in this Channel */
async removeReaction( async removeReaction(
message: Message | string, message: Message | string,
emoji: Emoji | string, emoji: Emoji | string,

View file

@ -4,7 +4,7 @@ import {
} from '../consts/urlsAndVersions.ts' } from '../consts/urlsAndVersions.ts'
import { Client } from '../models/client.ts' import { Client } from '../models/client.ts'
import { RESTManager } from '../models/rest.ts' import { RESTManager } from '../models/rest.ts'
import { MessageOption } from '../types/channel.ts' import { MessageOptions } from '../types/channel.ts'
import { UserPayload } from '../types/user.ts' import { UserPayload } from '../types/user.ts'
import { WebhookPayload } from '../types/webhook.ts' import { WebhookPayload } from '../types/webhook.ts'
import { Embed } from './embed.ts' import { Embed } from './embed.ts'
@ -14,7 +14,7 @@ import { User } from './user.ts'
import { fetchAuto } from '../../deps.ts' import { fetchAuto } from '../../deps.ts'
import { WEBHOOK_MESSAGE } from '../types/endpoint.ts' import { WEBHOOK_MESSAGE } from '../types/endpoint.ts'
export interface WebhookMessageOptions extends MessageOption { export interface WebhookMessageOptions extends MessageOptions {
embeds?: Embed[] embeds?: Embed[]
name?: string name?: string
avatar?: string avatar?: string

View file

@ -12,6 +12,7 @@ import {
GuildTextChannel GuildTextChannel
} from '../../mod.ts' } from '../../mod.ts'
import { Collector } from '../models/collectors.ts' import { Collector } from '../models/collectors.ts'
import { MessageAttachment } from '../structures/message.ts'
import { TOKEN } from './config.ts' import { TOKEN } from './config.ts'
const client = new Client({ const client = new Client({
@ -143,6 +144,16 @@ client.on('messageCreate', async (msg: Message) => {
coll.on('collect', (msg) => coll.on('collect', (msg) =>
msg.channel.send(`[COL] Collect: ${msg.content}`) msg.channel.send(`[COL] Collect: ${msg.content}`)
) )
} else if (msg.content === '!attach') {
msg.channel.send({
file: await MessageAttachment.load(
'https://cdn.discordapp.com/emojis/626139395623354403.png?v=1'
)
})
} else if (msg.content === '!textfile') {
msg.channel.send({
file: new MessageAttachment('hello.txt', 'hello world')
})
} }
}) })

View file

@ -1,4 +1,5 @@
import { Embed } from '../structures/embed.ts' import { Embed } from '../structures/embed.ts'
import { MessageAttachment } from '../structures/message.ts'
import { EmojiPayload } from './emoji.ts' import { EmojiPayload } from './emoji.ts'
import { MemberPayload } from './guild.ts' import { MemberPayload } from './guild.ts'
import { UserPayload } from './user.ts' import { UserPayload } from './user.ts'
@ -155,10 +156,10 @@ export interface MessagePayload {
stickers?: MessageStickerPayload[] stickers?: MessageStickerPayload[]
} }
export interface MessageOption { export interface MessageOptions {
tts?: boolean tts?: boolean
embed?: Embed embed?: Embed
file?: Attachment file?: MessageAttachment | string
allowedMentions?: { allowedMentions?: {
parse?: 'everyone' | 'users' | 'roles' parse?: 'everyone' | 'users' | 'roles'
roles?: string[] roles?: string[]