Skip to content

Refactor Discord Message Logic #6

@MathyouMB

Description

@MathyouMB

In the following code block on src/discord/index.ts:

export const sendDiscordItemMessage = async (
  message: DiscordItemMessage,
): Promise<Result<any, Error>> => {
  const webhookUrl = process.env.DISCORD_CHANNEL_WEBHOOK_URL ?? "";
  const messageHeader = formatMessageTitle(message.title, message.message);
  const messageSections = message.sections.map((section) => {
    const sectionHeader = formatMessageSectionTitle(section.title);
    const sectionItems = section.items
      .map((item) => formatItem(item))
      .join("\n");
    return `${sectionHeader} ${sectionItems}`;
  });

  try {
    const response = await axios.post(webhookUrl, {
      content: `${messageHeader} ${messageSections.join()}`,
    });
    return Ok(response.data);
  } catch (error) {
    return Err(new Error("Failed to send Discord message"));
  }
};

The lines...

const messageHeader = formatMessageTitle(message.title, message.message);
  const messageSections = message.sections.map((section) => {
    const sectionHeader = formatMessageSectionTitle(section.title);
    const sectionItems = section.items
      .map((item) => formatItem(item))
      .join("\n");
    return `${sectionHeader} ${sectionItems}`;
  });

and

 content: `${messageHeader} ${messageSections.join()}`,

Should be separated into a separate method that has the single responsibility of building a discord message.

In addition to making this minor refactor, please also write some tests that method on test/discord/index.ts

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    Status

    Unrefined

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions