|
| 1 | +import { makeGuildView } from "#common/template/guild.ts"; |
| 2 | +import { makeMemberUserView } from "#common/template/user.ts"; |
| 3 | +import type { SquirrelDiscordContext } from "#discord/index.ts"; |
| 4 | +import { logEvent } from "#plugin/logging/helper/logging.ts"; |
| 5 | +import { |
| 6 | + onTagCreated, |
| 7 | + onTagDeleted, |
| 8 | + onTagEdited, |
| 9 | +} from "#plugin/tags/public/extensionPoints.ts"; |
| 10 | +import type { Tag } from "#plugin/tags/public/tag.ts"; |
| 11 | +import type { Guild, Member } from "oceanic.js"; |
| 12 | + |
| 13 | +export default [ |
| 14 | + onTagCreated(handleCreate), |
| 15 | + onTagEdited(handleEdit), |
| 16 | + onTagDeleted(handleDelete), |
| 17 | +]; |
| 18 | + |
| 19 | +async function handleCreate( |
| 20 | + ctx: SquirrelDiscordContext, |
| 21 | + guild: Guild, |
| 22 | + actor: Member, |
| 23 | + tag: Tag, |
| 24 | +): Promise<void> { |
| 25 | + await logEvent(ctx, guild, null, "tag_create", () => { |
| 26 | + return { |
| 27 | + guild: makeGuildView(guild), |
| 28 | + actor: makeMemberUserView(actor), |
| 29 | + tag, |
| 30 | + }; |
| 31 | + }); |
| 32 | +} |
| 33 | + |
| 34 | +async function handleEdit( |
| 35 | + ctx: SquirrelDiscordContext, |
| 36 | + guild: Guild, |
| 37 | + actor: Member, |
| 38 | + oldTag: Tag, |
| 39 | + newTag: Tag, |
| 40 | +): Promise<void> { |
| 41 | + const nameChanged = oldTag.name !== newTag.name; |
| 42 | + const contentChanged = oldTag.content !== newTag.content; |
| 43 | + |
| 44 | + if (!(nameChanged || contentChanged)) { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + await logEvent(ctx, guild, null, "tag_edit", () => { |
| 49 | + return { |
| 50 | + guild: makeGuildView(guild), |
| 51 | + actor: makeMemberUserView(actor), |
| 52 | + old_tag: oldTag, |
| 53 | + new_tag: newTag, |
| 54 | + name_changed: nameChanged, |
| 55 | + content_changed: contentChanged, |
| 56 | + }; |
| 57 | + }); |
| 58 | +} |
| 59 | + |
| 60 | +async function handleDelete( |
| 61 | + ctx: SquirrelDiscordContext, |
| 62 | + guild: Guild, |
| 63 | + actor: Member, |
| 64 | + tag: Tag, |
| 65 | +): Promise<void> { |
| 66 | + await logEvent(ctx, guild, null, "tag_delete", () => { |
| 67 | + return { |
| 68 | + guild: makeGuildView(guild), |
| 69 | + actor: makeMemberUserView(actor), |
| 70 | + tag, |
| 71 | + }; |
| 72 | + }); |
| 73 | +} |
0 commit comments