Skip to content

Commit

Permalink
feat: added editable attachments support (#28)
Browse files Browse the repository at this point in the history
Co-authored-by: Jeroen Claassens <support@favware.tech>
  • Loading branch information
kyranet and favna authored Aug 8, 2021
1 parent 4f26135 commit 073a7ea
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 130 deletions.
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
"@typescript-eslint/eslint-plugin": "^4.28.4",
"@typescript-eslint/parser": "^4.28.5",
"cz-conventional-changelog": "^3.3.0",
"discord-api-types": "^0.18.1",
"discord.js": "dev",
"discord-api-types": "^0.22.0",
"discord.js": "^13.0.1",
"eslint": "^7.30.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.0",
Expand All @@ -51,7 +51,6 @@
"pretty-quick": "^3.1.1",
"rollup": "^2.53.2",
"rollup-plugin-cleaner": "^1.0.0",
"rollup-plugin-terser": "^7.0.2",
"rollup-plugin-typescript2": "^0.30.0",
"standard-version": "^9.3.1",
"typedoc": "^0.21.4",
Expand Down
10 changes: 1 addition & 9 deletions rollup.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { resolve as resolveDir } from 'path';
import cleaner from 'rollup-plugin-cleaner';
import { terser } from 'rollup-plugin-terser';
import typescript from 'rollup-plugin-typescript2';

export default {
Expand All @@ -24,13 +23,6 @@ export default {
cleaner({
targets: ['./dist/']
}),
typescript({ tsconfig: resolveDir(__dirname, 'src', 'tsconfig.json') }),
terser({
ecma: 2020,
// This will ensure that whenever Rollup is in watch (dev) mode, console logs will not be removed
// eslint-disable-next-line @typescript-eslint/naming-convention
compress: { drop_console: !Reflect.has(process.env, 'ROLLUP_WATCH') },
output: { comments: false }
})
typescript({ tsconfig: resolveDir(__dirname, 'src', 'tsconfig.json') })
]
};
22 changes: 13 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function get(message: Message): Message | null {
* @param options The options for the message sending, identical to `TextBasedChannel#send`'s options.
* @returns The response message.
*/
export async function send(message: Message, options: string | MessageOptions | WebhookMessageOptions): Promise<Message> {
export async function send(message: Message, options: string | Options): Promise<Message> {
const payload = await resolvePayload(message.channel, options);
return sendPayload(message, payload);
}
Expand All @@ -58,14 +58,8 @@ export async function reply(message: Message, options: string | ReplyMessageOpti
return sendPayload(message, payload);
}

function resolvePayload(
target: MessageTarget,
options: string | MessageOptions | WebhookMessageOptions,
extra?: MessageOptions | WebhookMessageOptions | undefined
): Promise<MessagePayload> {
if (typeof options === 'string') options = { content: options };
else options = { content: null, embeds: [], ...options };

function resolvePayload(target: MessageTarget, options: string | Options, extra?: Options | undefined): Promise<MessagePayload> {
options = typeof options === 'string' ? { content: options, ...unsetValues } : { content: null, ...unsetValues, ...options };
return MessagePayload.create(target, options, extra).resolveData().resolveFiles();
}

Expand All @@ -82,3 +76,13 @@ async function sendPayload(message: Message, payload: MessagePayload): Promise<M

return response;
}

type Options = MessageOptions | WebhookMessageOptions;

// This variable is casted like this (instead of `unsetValues: Options`) because `attachments` is a property on
// `MessageEditOptions`, therefore it does not exist in `MessageOptions`. This makes the property non-assignable, and so
// it can't be inlined with the other object spreads.
//
// As a workaround, I just define this object somewhere so we don't re-create it on each call. It is then casted to the
// valid type. This way TypeScript is happy.
const unsetValues = { embeds: [], attachments: [] } as Options;
Loading

0 comments on commit 073a7ea

Please sign in to comment.