slash-create with Cloudflare Workers template

Overview

/create with Cloudflare Workers

A slash-create template, using Cloudflare Workers.

Getting Started

Cloning the repo

You can either use degit to locally clone this repo without git, or create a new repo from this template and clone that.

npx degit Snazzah/slash-create-worker

After that, make sure to install dependencies using npm or yarn:

npm install
# yarn

Installing and setting up Wrangler

Make sure to sign up for a Cloudflare Workers account in a browser before continuing. Install wrangler with npm or yarn:

npm install -g @cloudflare/wrangler
# yarn global add @cloudflare/wrangler

Read more about installing wrangler.

Afterwards, run wrangler login to login to your Cloudflare account with OAuth:

wrangler login

Copy wrangler.example.toml into wrangler.toml. Make sure to fill in your account ID in the config and update the name of the worker. You can find your account ID here towards the right side.

Filling in secrets

You can enter in environment secrets with wrangler secret put, here are the keys that are required to run this:

wrangler secret put DISCORD_APP_ID
wrangler secret put DISCORD_PUBLIC_KEY
wrangler secret put DISCORD_BOT_TOKEN

For a development environment, you can also include COMMANDS_DEV_GUILD for commands to be updated in that guild live

wrangler secret put DISCORD_APP_ID -e development
wrangler secret put DISCORD_PUBLIC_KEY -e development
wrangler secret put DISCORD_BOT_TOKEN -e development
wrangler secret put COMMANDS_DEV_GUILD -e development

If an error occurs when trying to create a worker to put the secret in, create a worker manually in the dashboard and set the subdomain. It will be overwritten later.

Development

You can run npm run dev to start a development environment and use something like ngrok to tunnel it to a URL. To sync commands, copy .env.example to development.env and fill in the variables, then run npm run sync:dev.

Note: When you create a command, make sure to include it in the array of commands in ./src/commands/index.ts.

Production

To sync to production, copy .env.example to .env and fill in the variables, then run npm run sync. To publish code to a worker, run npm run deploy.

You might also like...

Template project for ComputerCraft programs written in TypeScript.

cc-tstl-template Template project for ComputerCraft programs written in TypeScript. Uses TypeScriptToLua to compile with ComputerCraft typing declarat

Dec 4, 2022

Template to get started with week0 tasks for MonthOfMERN

Template to get started with week0 tasks for MonthOfMERN

Dec 4, 2022

This project demonstrates how you can use the multi-model capabilities of Redis to create a real-time stock watchlist application.

This project demonstrates how you can use the multi-model capabilities of Redis to create a real-time stock watchlist application.

Introduction This project demonstrates how you can use Redis Stack to create a real-time stock watchlist application. It uses several different featur

Jan 2, 2023

Use Cloudflare Workers Cron Triggers to keep your Hetzner Cloud Firewall allowing the latest list of Cloudflare IPs, or any other lists!

Hetzner Cloud Firewall automation with Cloudflare Workers Heavily inspired by xopez/Hetzner-Cloud-Firewall-API-examples, this repository holds a Cloud

Dec 17, 2022

Functional-style Cloudflare Durable Objects with direct API calls from Cloudflare Workers and TypeScript support.

durable-apis Simplifies usage of Cloudflare Durable Objects, allowing a functional programming style or class style, lightweight object definitions, a

Jan 2, 2023

Starting template for building a Remix site with CloudFlare Workers (ES Modules Syntax)

Starting template for building a Remix site with CloudFlare Workers (ES Modules Syntax)

May 20, 2022

A starter template for Remix + Cloudflare Workers + DO + KV + Turborepo

Remix + Cloudflare Workers starter with Turborepo πŸš€ Starter to get going with Remix and Cloudflare Workers. This template is based on the starter cre

Jan 2, 2023

Discord.js bot starter template, Slash Commands only (Raymond forced me to make a bot template)

boat-template Raymond forced me to make a bot template This template is meant is just for stupidness ig Getting Started Rename the config.example.ts t

Jan 5, 2022

基于 gh-proxy + Jsdelivr+ cnpmjs + cloudflare workers ηš„ GitHub Serverless API ε·₯具。

better-github-api Better, Eazy, Access Anywhere 介绍 基于 gh-proxy + Jsdelivr + cnpmjs + cloudflare workers ηš„ GitHub Serverless API ε·₯具。 cdn.jsοΌšδ»…ε« gh-proxy

Nov 23, 2022

Cloudflare Workers Discord embed & keep URL example scripts.

Using Cloudflare Workers for Discord embed support Requirements: Your own image host, on a domain that you control via Cloudflare. Limitations: 100,00

Dec 27, 2022

πŸ”₯ Miniflare is a simulator for developing and testing Cloudflare Workers.

πŸ”₯ Miniflare is a simulator for developing and testing Cloudflare Workers.

Jan 3, 2023

Airtable + Cloudflare Workers URL Shortener 🌀

Airtable + Cloudflare Workers URL Shortener 🌀

tableflare Airtable + Cloudflare Workers URL Shortener 🌀 Quick Start Airtable Generate your Airtable API key from your account dashboard: https://air

Oct 16, 2022

βš”οΈ Strongly-typed Discord commands on Cloudflare Workers

βš”οΈ Strongly-typed Discord commands on Cloudflare Workers

βš”οΈ Slshx Slshx is a slightly wacky, experimental, library for building strongly-typed Discord commands that run on Cloudflare Workers, using a React-i

Dec 26, 2022

A URL shortener that runs on Cloudflare Workers

ITP Works A URL shortener that runs on Cloudflare Workers. It stores the rules in Cloudflare KV storage and sends a 301 redirect when a matched pathna

Mar 4, 2022

This library helps implement caching using Cloudflare Workers KV

With a few lines of code, you can control the execution of functions that you want to cache for speed, store them in the cache, and skip execution of the function if the cache exists.

Oct 20, 2022

Colour me! is a simple Discord bot running on Cloudflare Workers that allows server admins to give users the ability to change the colour of their own assigned colour roles!

Colour me! Colour me! is a simple Discord bot that allows server admins to give users the ability to change the colour of their own assigned colour ro

Dec 24, 2022

Google-Drive-Directory-Index | Combining the power of Cloudflare Workers and Google Drive API will allow you to index your Google Drive files on the browser.

🍿 Google-Drive-Directory-Index Combining the power of Cloudflare Workers and Google Drive will allow you to index your Google Drive files on the brow

Jan 2, 2023

Build your Cloudflare Workers with esbuild.

build-worker Bundle your Cloudflare Worker with esbuild instead of webpack. (It's ridiculously faster!) Wrangler v1 uses webpack. Wrangler v2 is using

Oct 24, 2022

Store and Deliver images with R2 backend Cloudflare Workers.

Store and Deliver images with R2 backend Cloudflare Workers.

r2-image-worker Store and Deliver images with Cloudflare R2 backend Cloudflare Workers. Synopsis Deploy r2-image-worker to Cloudflare Make a base64 st

Jan 3, 2023
Comments
  • requiredPermissions does not work (hangs command execution)

    requiredPermissions does not work (hangs command execution)

    Attempting to use requiredPermissions in seemingly any form results in the command hanging infinitely and timing out.

    This appears to happen with both 5.4.1 and 5.5.0 of slash-create.

    This does not happen on the plain JavaScript template. I have not tested any templates other this one and a plain JavaScript one.

    Steps followed for repro:

    • Clone or degit clone the repo.
    • Install deps: yarn
    • Populate the env with DISCORD_APP_ID, DISCORD_PUBLIC_KEY, DISCORD_BOT_TOKEN and DEVELOPMENT_GUILD_ID like normal.
    • Add a command that uses requiredPermissions or modify an existing command to use it.
      • I tried both ['MANAGE_ROLES'] for my own command ['MANAGE_GUILD'] for a test command taken from the docs (see below).
    • Run yarn sync:dev
    • Run yarn dev
    • As a user with permission to use it, try to execute the command.

    Expected: image

    Actual result: image

    Environment used: Ubuntu 22.04 in WSL2 with node 16.14.0 from asdf. Also happened on a GitHub Codespace default image with node 16 from nvm.

    The output is kind of uninteresting because it seems to think everything is okay:

    [mf:inf] Build succeeded
    [mf:inf] Worker reloaded! (118.09KiB)
    [mf:inf] Listening on :8787
    [mf:inf] - http://127.0.0.1:8787
    [mf:inf] - http://172.31.173.117:8787
    [mf:inf] - http://172.18.0.1:8787
    POST / 200 OK (2011.30ms, waitUntil: 2011.34ms)
    

    Command used for repro was taken from docs:

    const { SlashCommand, ApplicationCommandPermissionType } = require('slash-create');
    
    module.exports = class ExclusivityCommand extends SlashCommand {
      constructor(creator) {
        super(creator, {
          name: 'exclusivity',
          description: 'Only people who manage the guild can use this command.',
          // Whether to enable this command for everyone by default
          defaultPermission: false,
          // This will be an array of permission flag names from here: https://discord.dev/topics/permissions#permissions-bitwise-permission-flags
          requiredPermissions: ['MANAGE_GUILD']
        });
    
        this.filePath = __filename;
      }
    
      async run(ctx) {
        return 'You can manage this guild!';
      }
    }
    
    bug 
    opened by Erisa 2
  • Incompatibility with Miniflare

    Incompatibility with Miniflare

    When attempting to use this template with Miniflare, the following error is encountered:

    [mf:err] Unhandled Promise Rejection: SyntaxError: Unsupported key usage for a NODE-ED25519 key
        at verifyAcceptableEcKeyUse (node:internal/crypto/ec:86:11)
        at Object.ecImportKey (node:internal/crypto/ec:411:11)
        at Proxy.importKey (node:internal/crypto/webcrypto:489:10)
        at Object.6205 (/home/erisa/slash-create-worker/dist/worker.js:7:49825)
        at r (/home/erisa/slash-create-worker/dist/worker.js:7:82488)
        at Object.936 (/home/erisa/slash-create-worker/dist/worker.js:7:46659)
        at r (/home/erisa/slash-create-worker/dist/worker.js:7:82488)
        at Object.9569 (/home/erisa/slash-create-worker/dist/worker.js:7:48060)
        at r (/home/erisa/slash-create-worker/dist/worker.js:7:82488)
    (node:17421) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously (rejection id: 2)
    (Use `node --trace-warnings ...` to show where the warning was created)
    [mf:err] Unhandled Promise Rejection: SyntaxError: Unsupported key usage for a NODE-ED25519 key
        at verifyAcceptableEcKeyUse (node:internal/crypto/ec:86:11)
        at Object.ecImportKey (node:internal/crypto/ec:411:11)
        at Proxy.importKey (node:internal/crypto/webcrypto:489:10)
        at Object.6205 (/home/erisa/slash-create-worker/dist/worker.js:7:49825)
        at r (/home/erisa/slash-create-worker/dist/worker.js:7:82488)
        at Object.936 (/home/erisa/slash-create-worker/dist/worker.js:7:46659)
        at r (/home/erisa/slash-create-worker/dist/worker.js:7:82488)
        at Object.9569 (/home/erisa/slash-create-worker/dist/worker.js:7:48060)
        at r (/home/erisa/slash-create-worker/dist/worker.js:7:82488)
    

    This occurs once on load, and then subsequently on every request made by Discord (for example the ping test sent when setting the Interactions endpoint) and the bot does not function.

    Steps followed:

    • Clone or degit clone the repo.
    • Install deps: $ yarn
    • Install miniflare: $ npm install -g miniflare
    • Populate .env with DISCORD_APP_ID, DISCORD_PUBLIC_KEY and DISCORD_BOT_TOKEN
    • Run $ miniflare -e .env

    Environment used: Ubuntu 22.04 Beta in WSL2 with node 16.14.0 from asdf. Also happened on a GitHub Codespace default image last week.

    Click to show full output:
    erisa@Tuturu:~/slash-create-worker$ miniflare -e .env
    
    > build
    > webpack
    
    (node:17499) [DEP_WEBPACK_MODULE_ERRORS] DeprecationWarning: Module.errors was removed (use getErrors instead)
    (Use `node --trace-deprecation ...` to show where the warning was created)
    asset worker.js 115 KiB [emitted] [minimized] (name: main)
    runtime modules 432 bytes 3 modules
    modules by path ./node_modules/ 278 KiB
      modules by path ./node_modules/slash-create/lib/ 157 KiB 37 modules
      + 4 modules
    modules by path ./src/ 9.05 KiB
      modules by path ./src/shim/ 7.33 KiB 5 modules
      modules by path ./src/commands/*.ts 866 bytes 2 modules
      + 1 module
    optional modules 45 bytes [optional]
      express (ignored) 15 bytes [optional] [built] [code generated]
      fastify (ignored) 15 bytes [optional] [built] [code generated]
      fastify/lib/symbols (ignored) 15 bytes [optional] [built] [code generated]
    + 5 modules
    
    WARNING in ./node_modules/slash-create/lib/command.js 155:27-49
    Critical dependency: the request of a dependency is an expression
     @ ./node_modules/slash-create/lib/index.js 29:13-33
     @ ./src/shim/creator.ts 4:23-46
     @ ./src/shim/index.ts 13:13-33
     @ ./src/index.ts 5:15-32
    
    WARNING in ./node_modules/slash-create/lib/creator.js 131:30-47
    Critical dependency: the request of a dependency is an expression
     @ ./node_modules/slash-create/lib/index.js 30:13-33
     @ ./src/shim/creator.ts 4:23-46
     @ ./src/shim/index.ts 13:13-33
     @ ./src/index.ts 5:15-32
    
    2 warnings have detailed information that is not shown.
    Use 'stats.errorDetails: true' resp. '--stats-error-details' to show it.
    
    webpack 5.69.1 compiled with 2 warnings in 2958 ms
    [mf:inf] Build succeeded
    [mf:inf] Worker reloaded! (115.08KiB)
    [mf:inf] Listening on :8787
    [mf:inf] - http://127.0.0.1:8787
    [mf:inf] - http://172.26.110.44:8787
    [mf:err] Unhandled Promise Rejection: SyntaxError: Unsupported key usage for a NODE-ED25519 key
        at verifyAcceptableEcKeyUse (node:internal/crypto/ec:86:11)
        at Object.ecImportKey (node:internal/crypto/ec:411:11)
        at Proxy.importKey (node:internal/crypto/webcrypto:489:10)
        at Object.6205 (/home/erisa/slash-create-worker/dist/worker.js:7:49825)
        at r (/home/erisa/slash-create-worker/dist/worker.js:7:82488)
        at Object.936 (/home/erisa/slash-create-worker/dist/worker.js:7:46659)
        at r (/home/erisa/slash-create-worker/dist/worker.js:7:82488)
        at Object.9569 (/home/erisa/slash-create-worker/dist/worker.js:7:48060)
        at r (/home/erisa/slash-create-worker/dist/worker.js:7:82488)
    [mf:inf] Updated Request cf object cache!
    GET / 405 Method Not Allowed (9.51ms)
    GET /favicon.ico 405 Method Not Allowed (1.88ms)
    [mf:err] Unhandled Promise Rejection: SyntaxError: Unsupported key usage for a NODE-ED25519 key
        at verifyAcceptableEcKeyUse (node:internal/crypto/ec:86:11)
        at Object.ecImportKey (node:internal/crypto/ec:411:11)
        at Proxy.importKey (node:internal/crypto/webcrypto:489:10)
        at Object.6205 (/home/erisa/slash-create-worker/dist/worker.js:7:49825)
        at r (/home/erisa/slash-create-worker/dist/worker.js:7:82488)
        at Object.936 (/home/erisa/slash-create-worker/dist/worker.js:7:46659)
        at r (/home/erisa/slash-create-worker/dist/worker.js:7:82488)
        at Object.9569 (/home/erisa/slash-create-worker/dist/worker.js:7:48060)
        at r (/home/erisa/slash-create-worker/dist/worker.js:7:82488)
    (node:17421) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously (rejection id: 2)
    (Use `node --trace-warnings ...` to show where the warning was created)
    [mf:err] Unhandled Promise Rejection: SyntaxError: Unsupported key usage for a NODE-ED25519 key
        at verifyAcceptableEcKeyUse (node:internal/crypto/ec:86:11)
        at Object.ecImportKey (node:internal/crypto/ec:411:11)
        at Proxy.importKey (node:internal/crypto/webcrypto:489:10)
        at Object.6205 (/home/erisa/slash-create-worker/dist/worker.js:7:49825)
        at r (/home/erisa/slash-create-worker/dist/worker.js:7:82488)
        at Object.936 (/home/erisa/slash-create-worker/dist/worker.js:7:46659)
        at r (/home/erisa/slash-create-worker/dist/worker.js:7:82488)
        at Object.9569 (/home/erisa/slash-create-worker/dist/worker.js:7:48060)
        at r (/home/erisa/slash-create-worker/dist/worker.js:7:82488)
    [mf:err] Unhandled Promise Rejection: SyntaxError: Unsupported key usage for a NODE-ED25519 key
        at verifyAcceptableEcKeyUse (node:internal/crypto/ec:86:11)
        at Object.ecImportKey (node:internal/crypto/ec:411:11)
        at Proxy.importKey (node:internal/crypto/webcrypto:489:10)
        at Object.6205 (/home/erisa/slash-create-worker/dist/worker.js:7:49825)
        at r (/home/erisa/slash-create-worker/dist/worker.js:7:82488)
        at Object.936 (/home/erisa/slash-create-worker/dist/worker.js:7:46659)
        at r (/home/erisa/slash-create-worker/dist/worker.js:7:82488)
        at Object.9569 (/home/erisa/slash-create-worker/dist/worker.js:7:48060)
        at r (/home/erisa/slash-create-worker/dist/worker.js:7:82488)
    [mf:err] Unhandled Promise Rejection: SyntaxError: Unsupported key usage for a NODE-ED25519 key
        at verifyAcceptableEcKeyUse (node:internal/crypto/ec:86:11)
        at Object.ecImportKey (node:internal/crypto/ec:411:11)
        at Proxy.importKey (node:internal/crypto/webcrypto:489:10)
        at Object.6205 (/home/erisa/slash-create-worker/dist/worker.js:7:49825)
        at r (/home/erisa/slash-create-worker/dist/worker.js:7:82488)
        at Object.936 (/home/erisa/slash-create-worker/dist/worker.js:7:46659)
        at r (/home/erisa/slash-create-worker/dist/worker.js:7:82488)
        at Object.9569 (/home/erisa/slash-create-worker/dist/worker.js:7:48060)
        at r (/home/erisa/slash-create-worker/dist/worker.js:7:82488)
    ^C
    
    bug 
    opened by Erisa 1
  • Update this template for wrangler2

    Update this template for wrangler2

    Wrangler 2 released recently. There are some major changes that may be beneficial. Here's a migration guide. wrangler.toml will be much simpler and webpack may no longer be needed!

    enhancement 
    opened by kotx 0
Owner
Snazzah
A full-stack developer (mainly Typescript) with some ideas.
Snazzah
Airtable + Cloudflare Workers URL Shortener 🌀

tableflare Airtable + Cloudflare Workers URL Shortener ?? Quick Start Airtable Generate your Airtable API key from your account dashboard: https://air

Griko Nibras 10 Oct 16, 2022
Build your Cloudflare Workers with esbuild.

build-worker Bundle your Cloudflare Worker with esbuild instead of webpack. (It's ridiculously faster!) Wrangler v1 uses webpack. Wrangler v2 is using

Rom 7 Oct 24, 2022
Store and Deliver images with R2 backend Cloudflare Workers.

r2-image-worker Store and Deliver images with Cloudflare R2 backend Cloudflare Workers. Synopsis Deploy r2-image-worker to Cloudflare Make a base64 st

Yusuke Wada 62 Jan 3, 2023
Cloudflare Worker that will allow you to progressively migrate files from an S3-compatible object store to Cloudflare R2.

A Cloudflare Worker for Progressive S3 to R2 Blog Post: https://kian.org.uk/progressive-s3-to-cloudflare-r2-migration-using-workers/ This is a Cloudfl

Kian 29 Dec 30, 2022
Making service workers easy so that your app is fast and reliable, even offline.

tulo.js Making service workers easy to use so that your app can be fast and reliable, even offline. Welcome to tulo.js, a service worker library that

OSLabs Beta 37 Nov 16, 2022
Example repo for getting NextJS, Rust via wasm-pack, and web workers all playing nicely together.

Example of integrating WASM & web workers with a Typescript NextJS project. Running yarn yarn dev Open localhost:3000 Layout Rust code is in ./rust, g

Studio Lagier 63 Dec 23, 2022
Easily redirect one entire domain to another with a serverless Cloudflare Worker.

Domain Redirecting with Cloudflare Workers Easily redirect one entire domain to another with a serverless Cloudflare Worker. All paths and other data

Erisa A 19 Dec 11, 2022
Send emails using Cloudflare Worker, for free.

Email API for Proselog. Not intended for use outside of Proselog, but it should work with any worker, without any configuration. import { sendEmail }

Proselog 65 Nov 7, 2022
Cloudflare Worker to make a R2 Bucket public!

r2-public-worker A Cloudflare Worker to make your R2 bucket public! Minimum Requirements Cloudflare Account wrangler >= 2.0.2 Note: Ensure you are usi

Cole Mackenzie 20 Sep 19, 2022
Using Cloudflare worker to generate host list from firebog to keep updated.

AdGuardCloudflareHostGenerator Use a cloudflare worker to generate a up to date list from FireBog's ticked list found at https://v.firebog.net/hosts/l

Jake Steele 14 Nov 30, 2022