discord.js - Imagine ❄d-I❄
discord.js - Imagine ❄3y ago
10 replies
NP

file being sent with content object object

Hi, the transcript file from here is being sent to GitHub with the contents of the file as [object Object]

GitHub Integration:

import { Octokit } from "@octokit/rest";
import fs from "fs";

const octokit = new Octokit({ auth: "go3df8KI2dcd89" });

const owner = "repo";
const repo = "transcripts";
const branch = "main";

const createFileInRepo = async (
  filePath: string,
  content: string,
  commitMessage: string,
) => {
  try {
    // Get the SHA of the latest commit
    const { data: refData } = await octokit.git.getRef({
      owner,
      repo,
      ref: `heads/${branch}`,
    });
    const sha = refData.object.sha;

    // Get the latest commit
    const { data: commitData } = await octokit.git.getCommit({
      owner,
      repo,
      commit_sha: sha,
    });

    // Create a new tree with the new file
    const { data: treeData } = await octokit.git.createTree({
      owner,
      repo,
      base_tree: commitData.tree.sha,
      tree: [
        {
          path: filePath,
          mode: "100644",
          content: content,
        },
      ],
    });

    // Create a new commit with the new tree
    const { data: commit } = await octokit.git.createCommit({
      owner,
      repo,
      message: commitMessage,
      tree: treeData.sha,
      parents: [sha],
    });

    // Update the branch reference to point to the new commit
    await octokit.git.updateRef({
      owner,
      repo,
      ref: `heads/${branch}`,
      sha: commit.sha,
    });

    console.log(`File ${filePath} created in repository ${owner}/${repo}`);
  } catch (error) {
    console.error(`Error creating file in repository: ${error}`);
  }
};

export default createFileInRepo;
Was this page helpful?