From 8734e9a7a64d23e233a86a05b3a706f4aa92c64f Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Thu, 21 Jan 2021 20:27:06 +0530 Subject: [PATCH 1/2] update url --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 4c76c14..6a922c7 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ ## Usage -You can import the package from https://deno.land/x/harmony/mod.ts (with latest version) or can add a version too, and raw GitHub URL (latest unpublished version) https://raw.githubusercontent.com/harmony-org/harmony/main/mod.ts too. +You can import the package from https://deno.land/x/harmony/mod.ts (with latest version) or can add a version too, and raw GitHub URL (latest unpublished version) https://raw.githubusercontent.com/harmonyland/harmony/main/mod.ts too. For a quick example, run this: @@ -137,9 +137,9 @@ new MyClient().connect('super secret token comes here', Intents.All) Documentation is available for `main` (branch) and `stable` (release). -- [Main](https://doc.deno.land/https/raw.githubusercontent.com/harmony-org/harmony/main/mod.ts) +- [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://harmony-org.github.io) +- [Guide](https://harmony.mod.land) ## Found a bug or want support? Join our discord server! @@ -159,6 +159,6 @@ Small note: If editing the README, please conform to the [standard-readme](https ## License -[MIT © 2020-2021 Harmony Org](LICENSE) +[MIT © 2020-2021 Harmonyland](LICENSE) -#### Made with ❤ by Harmony-org +#### Made with ❤ by Harmonyland From ebb4be897a8c97f7e6a1ca98eada25db4f98647e Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Sun, 24 Jan 2021 19:50:49 +0530 Subject: [PATCH 2/2] add file support (really) & slash.commands.bulkEdit --- mod.ts | 6 ++++-- src/models/client.ts | 2 +- src/models/rest.ts | 3 ++- src/models/slashClient.ts | 17 +++++++++++++++ src/structures/message.ts | 40 +++++++++++++++++++++++++++++++++-- src/structures/slash.ts | 4 ++-- src/structures/textChannel.ts | 8 ++++--- src/structures/webhook.ts | 4 ++-- src/test/index.ts | 11 ++++++++++ src/types/channel.ts | 5 +++-- 10 files changed, 85 insertions(+), 15 deletions(-) diff --git a/mod.ts b/mod.ts index 8be44ca..b279793 100644 --- a/mod.ts +++ b/mod.ts @@ -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 { diff --git a/src/models/client.ts b/src/models/client.ts index efed48b..7b28f84 100644 --- a/src/models/client.ts +++ b/src/models/client.ts @@ -198,7 +198,7 @@ export class Client extends HarmonyEventEmitter { enabled: options.enableSlash }) - this.fetchGatewayInfo = options.fetchGatewayInfo ?? false + this.fetchGatewayInfo = options.fetchGatewayInfo ?? true if (this.token === undefined) { try { diff --git a/src/models/rest.ts b/src/models/rest.ts index 5f9696c..7d38968 100644 --- a/src/models/rest.ts +++ b/src/models/rest.ts @@ -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 && diff --git a/src/models/slashClient.ts b/src/models/slashClient.ts index 020179b..9520025 100644 --- a/src/models/slashClient.ts +++ b/src/models/slashClient.ts @@ -314,6 +314,23 @@ export class SlashCommandsManager { return new SlashCommand(this, data) } + + /** Bulk Edit Global or Guild Slash Commands */ + async bulkEdit( + cmds: Array, + guild?: Guild | string + ): Promise { + 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 diff --git a/src/structures/message.ts b/src/structures/message.ts index 5cdbf75..417f660 100644 --- a/src/structures/message.ts +++ b/src/structures/message.ts @@ -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 { + 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) + } +} diff --git a/src/structures/slash.ts b/src/structures/slash.ts index 0a4f427..7498cea 100644 --- a/src/structures/slash.ts +++ b/src/structures/slash.ts @@ -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 diff --git a/src/structures/textChannel.ts b/src/structures/textChannel.ts index 0546dcd..552411d 100644 --- a/src/structures/textChannel.ts +++ b/src/structures/textChannel.ts @@ -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 { 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, diff --git a/src/structures/webhook.ts b/src/structures/webhook.ts index 149827c..c8455f5 100644 --- a/src/structures/webhook.ts +++ b/src/structures/webhook.ts @@ -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 diff --git a/src/test/index.ts b/src/test/index.ts index 7d7dd83..e91db95 100644 --- a/src/test/index.ts +++ b/src/test/index.ts @@ -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') + }) } }) diff --git a/src/types/channel.ts b/src/types/channel.ts index 5cdbce1..77ee75d 100644 --- a/src/types/channel.ts +++ b/src/types/channel.ts @@ -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[]