Discord API (Discord piece or Code Piece with discord.js)

I am finding some limitations of the Discord integration, specifically I’m unable to access actions like these, I believe due to the way Active Pieces authorises the API call

I saw someone on the activepieces discord suggest using a code piece instead, and integrating discord.js, but I am either getting a 401 response, or “missing ) after argument list” from any custom code.

Does anyone have a template for accessing the Discord API from active pieces? I am 100% certain my bot should have sufficient permissions, I am just struggling to get active pieces and discord to share that information.

Hello @jacksonw , Welcome to the community :wave:,

You can use the “Custom API Call” action for actions that are not available, like “Get Channel Messages.” Are you facing any errors with this action?

Can you provide the code snippet you used for the code piece, removing any confidential details? Also, make sure you have installed the “discord.js” package in the code editor by clicking “Add NPM package” in the top right corner.

1 Like

Yeah, I’ve installed discord.js, but from the console it looks as though it’s not compiling properly. Changes to the code seem to make no difference. If it includes discord.js then it has the same output regardless of the rest of the code.

index.ts
import Discord from 'discord.js'
export const code = async (inputs) => {
  const client = new Discord.Client();
  await client.login(inputs.token);
  await client.api.channels('inputs.channelId').messages(inputs.messageId).delete();
  return true;
};
package.json
{
  "dependencies": {
    "discord.js": "14.15.3"
  }
}

and then the output is

“missing ) after argument list”

and console output is indicating the problem is the compiler

console output
---------error-------
 {"stack":"/root/codes/***/step_12/index.js:2\n  throw new Error('\"Compilation Error:\\n{\\\"code\\\":2,\\\"killed\\\":false,\\\"signal\\\":null,\\\"cmd\\\":\\\"pnpm tsc \\\",\\\"stdout\\\":\\\"index.ts(1,21): error TS2307: Cannot find module 'discord' or its corresponding type declarations.\\\\n\\\",\\\"stderr\\\":\\\"\\\",\\\"context\\\":{\\\"[PackageManager#runCommand]\\\":{\\\"path\\\":\\\"/usr/src/cache/sandbox/CODE-sourceCodeHash-***-name-step_12-flowId-***/codes/***/step_12\\\",\\\"command\\\":\\\"tsc\\\",\\\"args\\\":[]}}}\"');\n                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\nSyntaxError: missing ) after argument list\n    at internalCompileFunction (node:internal/vm:73:18)\n    at wrapSafe (node:internal/modules/cjs/loader:1274:20)\n    at Module._compile (node:internal/modules/cjs/loader:1320:27)\n    at Module._extensions..js (node:internal/modules/cjs/loader:1414:10)\n    at Module.load (node:internal/modules/cjs/loader:1197:32)\n    at Module._load (node:internal/modules/cjs/loader:1013:12)\n    at Module.require (node:internal/modules/cjs/loader:1225:19)\n    at require (node:internal/modules/helpers:177:18)\n    at moduleId (webpack://activepieces/node_modules/@activepieces/import-fresh-webpack/index.js:35:32)\n    at <anonymous> (webpack://activepieces/engine/src/lib/handler/code-executor.ts:43:102)","message":"missing ) after argument list"}