Edits history of script submission #377 for ' Receive Application Command Sample (discord)'

  • deno
    One script reply has been approved by the moderators
    Ap­pro­ved
    // Getting started:
    // If you don't have a discord application yet, follow step 1 to get your application created and registered to your server
    //   https://discord.com/developers/docs/getting-started#step-1-creating-an-app
    
    // Navigate to your application in the Discord developer portal: https://discord.com/developers/applications
    
    // Create a new discord_bot_configuration resource with the public_key and application_id fields from the discord portal
    // Populate the DEFUALT_DISCORD_CONFIG variable below with the path to your new resource
    
    // Next we deploy this script to Windmill and register it as the "Interactions Endpoint URL"
    //   Get the endpoint for the sync version: https://docs.windmill.dev/docs/core_concepts/webhooks#synchronous
    //   Include query parameters for your token as well as capturing the raw body and headers to verify the signature
    //   {DEPLOYED_SCRIPT_SYNC_WEBHOOK}?include_header=X-Signature-Ed25519,X-Signature-Timestamp&raw=true&token={YOUR_TOKEN}
    
    // Finally we need to create a discord command and register it with our application:
    //   https://discord.com/developers/docs/interactions/application-commands#making-a-global-command
    
    // Now you should be able to use your registered command in discord and see the bot reply with some of the json payload
    
    import {
      InteractionResponseType,
      InteractionType,
      verifyKey,
    } from "npm:[email protected]";
    
    type DiscordBotConfiguration = {
      public_key: string;
      application_id: string;
    };
    export async function main(
      x_signature_ed25519: string,
      x_signature_timestamp: string,
      raw_string: string,
      discord_config: DiscordBotConfiguration,
    ) {
      // We'll need the http request body as a string and the two headers to verify the request signature
      // https://discord.com/developers/docs/interactions/receiving-and-responding#security-and-authorization
      const isVerified = verifyKey(
        raw_string,
        x_signature_ed25519,
        x_signature_timestamp,
        discord_config.public_key,
      );
    
      // If we can't verify the request, we return 401.
      // We will be tested when we submit the interaction webhook to discord
      if (!isVerified) {
        return { windmill_status_code: 401 };
      }
    
      const interaction = JSON.parse(raw_string);
    
      // If we get a PING, we need to respond with a PONG
      // https://discord.com/developers/docs/interactions/receiving-and-responding#responding-to-an-interaction
      const type = interaction.type as InteractionType;
      if (type === InteractionType.PING) {
        return { type: InteractionResponseType.PONG };
      }
    
      // At this point we can process the interaction and can handle here or delegate to another script/flow
      // https://discord.com/developers/docs/interactions/receiving-and-responding#interactions-and-bot-users
      if (type === InteractionType.APPLICATION_COMMAND) {
        const message = `Received command: \`\`\`json\n${JSON.stringify(
          {
            id: interaction.id,
            sender: interaction.member?.user,
            data: interaction.data,
          },
          undefined,
          2,
        )}\`\`\``;
    
        return {
          type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
          data: {
            content: message,
          },
        };
      }
    }
    

    Submitted by hugo697 382 days ago

  • deno
    // Getting started:
    // If you don't have a discord application yet, follow step 1 to get your application created and registered to your server
    //   https://discord.com/developers/docs/getting-started#step-1-creating-an-app
    
    // Navigate to your application in the Discord developer portal: https://discord.com/developers/applications
    
    // Create a new discord_bot_configuration resource with the public_key and application_id fields from the discord portal
    // Populate the DEFUALT_DISCORD_CONFIG variable below with the path to your new resource
    
    // Next we deploy this script to Windmill and register it as the "Interactions Endpoint URL"
    //   Get the endpoint for the sync version: https://docs.windmill.dev/docs/core_concepts/webhooks#synchronous
    //   Include query parameters for your token as well as capturing the raw body and headers to verify the signature
    //   {DEPLOYED_SCRIPT_SYNC_WEBHOOK}?include_header=X-Signature-Ed25519,X-Signature-Timestamp&raw=true&token={YOUR_TOKEN}
    
    // Finally we need to create a discord command and register it with our application:
    //   https://discord.com/developers/docs/interactions/application-commands#making-a-global-command
    
    // Now you should be able to use your registered command in discord and see the bot reply with some of the json payload
    
    import {
      InteractionResponseType,
      InteractionType,
      verifyKey,
    } from "npm:[email protected]";
    
    type DiscordBotConfiguration = {
      public_key: string;
      application_id: string;
    };
    export async function main(
      x_signature_ed25519: string,
      x_signature_timestamp: string,
      raw_string: string,
      discord_config: DiscordBotConfiguration,
    ) {
      // We'll need the http request body as a string and the two headers to verify the request signature
      // https://discord.com/developers/docs/interactions/receiving-and-responding#security-and-authorization
      const isVerified = verifyKey(
        raw_string,
        x_signature_ed25519,
        x_signature_timestamp,
        discord_config.public_key,
      );
    
      // If we can't verify the request, we return 401.
      // We will be tested when we submit the interaction webhook to discord
      if (!isVerified) {
        return { windmill_status_code: 401 };
      }
    
      const interaction = JSON.parse(raw_string);
    
      // If we get a PING, we need to respond with a PONG
      // https://discord.com/developers/docs/interactions/receiving-and-responding#responding-to-an-interaction
      const type = interaction.type as InteractionType;
      if (type === InteractionType.PING) {
        return { type: InteractionResponseType.PONG };
      }
    
      // At this point we can process the interaction and can handle here or delegate to another script/flow
      // https://discord.com/developers/docs/interactions/receiving-and-responding#interactions-and-bot-users
      if (type === InteractionType.APPLICATION_COMMAND) {
        const message = `Received command: \`\`\`json\n${JSON.stringify(
          {
            id: interaction.id,
            sender: interaction.member?.user,
            data: interaction.data,
          },
          undefined,
          2,
        )}\`\`\``;
    
        return {
          type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
          data: {
            content: message,
          },
        };
      }
    }
    

    Submitted by admin 1014 days ago

  • deno
    // Getting started:
    // If you don't have a discord application yet, follow step 1 to get your application created and registered to your server
    //   https://discord.com/developers/docs/getting-started#step-1-creating-an-app
    
    // Navigate to your application in the Discord developer portal: https://discord.com/developers/applications
    
    // Create a new discord_bot_configuration resource with the public_key and application_id fields from the discord portal
    // Populate the DEFUALT_DISCORD_CONFIG variable below with the path to your new resource
    
    // Next we deploy this script to Windmill and register it as the "Interactions Endpoint URL"
    //   Get the endpoint for the sync version: https://docs.windmill.dev/docs/core_concepts/webhooks#synchronous
    //   Include query parameters for your token as well as capturing the raw body and headers to verify the signature
    //   {DEPLOYED_SCRIPT_SYNC_WEBHOOK}?include_header=X-Signature-Ed25519,X-Signature-Timestamp&raw=true&token={YOUR_TOKEN}
    
    // Finally we need to create a discord command and register it with our application:
    //   https://discord.com/developers/docs/interactions/application-commands#making-a-global-command
    
    // Now you should be able to use your registered command in discord and see the bot reply with some of the json payload
    
    import {
      InteractionResponseType,
      InteractionType,
      verifyKey,
    } from "npm:[email protected]";
    
    
    type DiscordBotConfiguration = {
      public_key: string;
      application_id: string;
    };
    export async function main(
      x_signature_ed25519: string,
      x_signature_timestamp: string,
      raw_string: string,
      discord_config: DiscordBotConfiguration,
    ) {
    
      // We'll need the http request body as a string and the two headers to verify the request signature
      // https://discord.com/developers/docs/interactions/receiving-and-responding#security-and-authorization
      const isVerified = verifyKey(
        raw_string,
        x_signature_ed25519,
        x_signature_timestamp,
        discord_config.public_key,
      );
    
      // If we can't verify the request, we return 401.
      // We will be tested when we submit the interaction webhook to discord
      if (!isVerified) {
        return { windmill_status_code: 401 };
      }
    
      const interaction = JSON.parse(raw_string);
    
      // If we get a PING, we need to respond with a PONG
      // https://discord.com/developers/docs/interactions/receiving-and-responding#responding-to-an-interaction
      const type = interaction.type as InteractionType;
      if (type === InteractionType.PING) {
        return { type: InteractionResponseType.PONG };
      }
    
      // At this point we can process the interaction and can handle here or delegate to another script/flow
      // https://discord.com/developers/docs/interactions/receiving-and-responding#interactions-and-bot-users
      if (type === InteractionType.APPLICATION_COMMAND) {
        const message = `Received command: \`\`\`json\n${
          JSON.stringify(
            {
              id: interaction.id,
              sender: interaction.member?.user,
              data: interaction.data,
            },
            undefined,
            2,
          )
        }\`\`\``;
    
        return {
          type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
          data: {
            content: message,
          },
        };
      }
    }

    Submitted by admin 1018 days ago

  • deno
    // Getting started:
    // If you don't have a discord application yet, follow step 1 to get your application created and registered to your server
    //   https://discord.com/developers/docs/getting-started#step-1-creating-an-app
    
    // Navigate to your application in the Discord developer portal: https://discord.com/developers/applications
    
    // Now we need to create a Windmill Resource type "c_discord_bot" to host our { public_key: string, application_id: string }
    //   https://docs.windmill.dev/docs/core_concepts/resources_and_types#create-a-resource-type
    
    // Create a new c_discord_bot resurce with the public_key and application_id fields from the discord portal
    // Populate the DEFUALT_DISCORD_CONFIG variable below with the path to your new resource
    
    // Next we deploy this script to Windmill and register it as the "Interactions Endpoint URL"
    //   Get the endpoint for the sync version: https://docs.windmill.dev/docs/core_concepts/webhooks#synchronous
    //   Include query parameters for your token as well as capturing the raw body and headers to verify the signature
    //   {DEPLOYED_SCRIPT_SYNC_WEBHOOK}?include_header=X-Signature-Ed25519,X-Signature-Timestamp&raw=true&token={YOUR_TOKEN}
    
    // Finally we need to create a discord command and register it with our application:
    //   https://discord.com/developers/docs/interactions/application-commands#making-a-global-command
    
    // Now you should be able to use your registered command in discord and see the bot reply with some of the json payload
    
    import * as wmill from "https://deno.land/x/[email protected]/mod.ts";
    import {
      InteractionResponseType,
      InteractionType,
      verifyKey,
    } from "npm:[email protected]";
    
    // change path to resource c_discord_bot created in the getting started section
    const DEFAULT_DISCORD_CONFIG = "u/johlrich/glorious_c_discord_bot";
    
    export async function main(
      x_signature_ed25519: string,
      x_signature_timestamp: string,
      raw_string: string,
      discord_config: wmill.Resource<"c_discord_bot">,
    ) {
      // todo: can we set this as a default value another way?
      discord_config = discord_config ??
        await wmill.getResource(DEFAULT_DISCORD_CONFIG);
    
      // We'll need the http request body as a string and the two headers to verify the request signature
      // https://discord.com/developers/docs/interactions/receiving-and-responding#security-and-authorization
      const isVerified = verifyKey(
        raw_string,
        x_signature_ed25519,
        x_signature_timestamp,
        discord_config.public_key,
      );
    
      // If we can't verify the request, we return 401.
      // We will be tested when we submit the interaction webhook to discord
      if (!isVerified) {
        return { windmill_status_code: 401 };
      }
    
      const interaction = JSON.parse(raw_string);
    
      // If we get a PING, we need to respond with a PONG
      // https://discord.com/developers/docs/interactions/receiving-and-responding#responding-to-an-interaction
      const type = interaction.type as InteractionType;
      if (type === InteractionType.PING) {
        return { type: InteractionResponseType.PONG };
      }
    
      // At this point we can process the interaction and can handle here or delegate to another script/flow
      // https://discord.com/developers/docs/interactions/receiving-and-responding#interactions-and-bot-users
      if (type === InteractionType.APPLICATION_COMMAND) {
        const message = `Received command: \`\`\`json\n${
          JSON.stringify(
            {
              id: interaction.id,
              sender: interaction.member?.user,
              data: interaction.data,
            },
            undefined,
            2,
          )
        }\`\`\``;
    
        return {
          type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
          data: {
            content: message,
          },
        };
      }
    }

    Submitted by jonathan ohlrich398 1093 days ago