summary refs log tree commit diff
path: root/src/util/Message.ts
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-05-21 21:58:41 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-05-21 21:58:41 +0200
commit6af753f4e05dd769a52af084d45e2b00dfc09830 (patch)
tree28ecb502634370e44bf5d4bb7ff7ac03039d4ab3 /src/util/Message.ts
parentMerge pull request #143 from DarrenAlex/patch-1 (diff)
downloadserver-6af753f4e05dd769a52af084d45e2b00dfc09830.tar.xz
:sparkles: sendMessage() function
Diffstat (limited to 'src/util/Message.ts')
-rw-r--r--src/util/Message.ts52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/util/Message.ts b/src/util/Message.ts
new file mode 100644

index 00000000..8e9f98cd --- /dev/null +++ b/src/util/Message.ts
@@ -0,0 +1,52 @@ +import { ChannelModel, MessageCreateEvent } from "@fosscord/server-util"; +import { Snowflake } from "@fosscord/server-util"; +import { MessageModel } from "@fosscord/server-util"; +import { PublicMemberProjection } from "@fosscord/server-util"; +import { toObject } from "@fosscord/server-util"; +import { getPermission } from "@fosscord/server-util"; +import { Message } from "@fosscord/server-util"; +import { HTTPError } from "lambert-server"; +import { emitEvent } from "./Event"; +// TODO: check webhook, application, system author + +export async function sendMessage(opts: Partial<Message>) { + const channel = await ChannelModel.findOne({ id: opts.channel_id }, { guild_id: true, type: true, permission_overwrites: true }).exec(); + if (!channel || !opts.channel_id) throw new HTTPError("Channel not found", 404); + // TODO: are tts messages allowed in dm channels? should permission be checked? + + const permissions = await getPermission(opts.author_id, channel.guild_id, opts.channel_id, { channel }); + permissions.hasThrow("SEND_MESSAGES"); + if (opts.tts) permissions.hasThrow("SEND_TTS_MESSAGES"); + if (opts.message_reference) { + permissions.hasThrow("READ_MESSAGE_HISTORY"); + if (opts.message_reference.guild_id !== channel.guild_id) throw new HTTPError("You can only reference messages from this guild"); + } + + if (opts.message_reference) { + if (opts.message_reference.channel_id !== opts.channel_id) throw new HTTPError("You can only reference messages from this channel"); + // TODO: should be checked if the referenced message exists? + } + + // TODO: check and put it all in the body + const message: Message = { + ...opts, + id: Snowflake.generate(), + timestamp: new Date(), + guild_id: channel.guild_id, + channel_id: opts.channel_id, + // TODO: generate mentions and check permissions + mention_channels_ids: [], + mention_role_ids: [], + mention_user_ids: [], + attachments: [], // TODO: message attachments + embeds: opts.embeds || [], + reactions: opts.reactions || [], + type: opts.type ?? 0 + }; + + const data = toObject(await new MessageModel(message).populate({ path: "member", select: PublicMemberProjection }).save()); + + await emitEvent({ event: "MESSAGE_CREATE", channel_id: opts.channel_id, data, guild_id: channel.guild_id } as MessageCreateEvent); + + return data; +}