Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added editable attachments support #28

Merged
merged 4 commits into from
Aug 8, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 from
// `MessageEditOptions`, and therefore does not exist in `MessageOptions`, this makes the property non-assignable, and
// 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, and cast it to the valid
// types, this way TypeScript is happy.
kyranet marked this conversation as resolved.
Show resolved Hide resolved
const unsetValues = { embeds: [], attachments: [] } as Options;
Loading