Merge pull request #92 from DjDeveloperr/slash

Add support for Message Attachment and add SlashCommandsManager#editBulk
This commit is contained in:
DjDeveloper 2021-01-24 21:09:27 +05:30 committed by GitHub
commit 71ded8ea2e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 86 additions and 16 deletions

View file

@ -139,7 +139,7 @@ Documentation is available for `main` (branch) and `stable` (release).
- [Main](https://doc.deno.land/https/raw.githubusercontent.com/harmonyland/harmony/main/mod.ts)
- [Stable](https://doc.deno.land/https/deno.land/x/harmony/mod.ts)
- [Guide](https://harmonyland.github.io)
- [Guide](https://harmony.mod.land)
## Found a bug or want support? Join our discord server!

6
mod.ts
View file

@ -59,7 +59,7 @@ export { NewsChannel } from './src/structures/guildNewsChannel.ts'
export { VoiceChannel } from './src/structures/guildVoiceChannel.ts'
export { Invite } from './src/structures/invite.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 {
Presence,
@ -69,6 +69,7 @@ export {
export { Role } from './src/structures/role.ts'
export { Snowflake } from './src/utils/snowflake.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 { User } from './src/structures/user.ts'
export { Webhook } from './src/structures/webhook.ts'
@ -97,7 +98,8 @@ export type {
GuildChannelPayload,
GuildTextChannelPayload,
GuildVoiceChannelPayload,
GroupDMChannelPayload
GroupDMChannelPayload,
MessageOptions
} from './src/types/channel.ts'
export type { EmojiPayload } from './src/types/emoji.ts'
export type {

View file

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

View file

@ -271,7 +271,8 @@ export class RESTManager {
if (body?.file !== undefined) {
const form = new FormData()
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
} else if (
body !== undefined &&

View file

@ -314,6 +314,23 @@ export class SlashCommandsManager {
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

View file

@ -3,7 +3,7 @@ import {
Attachment,
MessageActivity,
MessageApplication,
MessageOption,
MessageOptions,
MessagePayload,
MessageReference
} from '../types/channel.ts'
@ -19,7 +19,7 @@ import { MessageReactionsManager } from '../managers/messageReactions.ts'
import { MessageSticker } from './messageSticker.ts'
import { Emoji } from './emoji.ts'
type AllMessageOptions = MessageOption | Embed
type AllMessageOptions = MessageOptions | Embed
export class Message extends Base {
id: string
@ -171,3 +171,39 @@ export class Message extends Base {
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 { MessageOption } from '../types/channel.ts'
import { MessageOptions } from '../types/channel.ts'
import { INTERACTION_CALLBACK, WEBHOOK_MESSAGE } from '../types/endpoint.ts'
import {
InteractionData,
@ -16,7 +16,7 @@ import { GuildTextChannel, TextChannel } from './textChannel.ts'
import { User } from './user.ts'
import { Webhook } from './webhook.ts'
interface WebhookMessageOptions extends MessageOption {
interface WebhookMessageOptions extends MessageOptions {
embeds?: Embed[]
name?: string
avatar?: string

View file

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

View file

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

View file

@ -12,6 +12,7 @@ import {
GuildTextChannel
} from '../../mod.ts'
import { Collector } from '../models/collectors.ts'
import { MessageAttachment } from '../structures/message.ts'
import { TOKEN } from './config.ts'
const client = new Client({
@ -143,6 +144,16 @@ client.on('messageCreate', async (msg: Message) => {
coll.on('collect', (msg) =>
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 { MessageAttachment } from '../structures/message.ts'
import { EmojiPayload } from './emoji.ts'
import { MemberPayload } from './guild.ts'
import { UserPayload } from './user.ts'
@ -155,10 +156,10 @@ export interface MessagePayload {
stickers?: MessageStickerPayload[]
}
export interface MessageOption {
export interface MessageOptions {
tts?: boolean
embed?: Embed
file?: Attachment
file?: MessageAttachment | string
allowedMentions?: {
parse?: 'everyone' | 'users' | 'roles'
roles?: string[]