Skip to content

buzzkillb/pythwrangler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pythwrangler

Cloudflare Worker pricebot using Pyth Network oracle to update Discord bot nicknames with live price data.

How It Works

  1. Cron Trigger - Runs every 1 minute
  2. Fetches Price - Gets price from Pyth Hermes API
  3. Formats - Dynamic decimal places (BTC: $50000, DOGE: $0.1500)
  4. Updates - Sets bot nickname in all guilds the bot is in
  5. Gateway Keepalive - Connects to Discord gateway and stays connected for ~59 seconds to keep bot "online" between cron triggers

Setup

1. Create Discord Bot

  1. Go to Discord Developer Portal
  2. Create a new application
  3. Go to Bot section
  4. Click Reset Token to get your bot token
  5. Enable Public Bot (for personal use)
  6. Under Privileged Gateway Intents, enable:
    • Server Members Intent

2. Add Bot to Server

Generate OAuth2 URL with bot scope:

https://discord.com/oauth2/authorize?client_id=YOUR_CLIENT_ID&scope=bot&permissions=1

Visit that URL to add the bot to your server(s).

3. Get Pyth Feed ID

Find the price feed ID for your desired asset at Pyth Network Price Feeds.

Example feed IDs:

  • BTC: 0xe62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43
  • ETH: 0xff6147a7b6429c8f3f3e9d0da9a6e7f6c6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6

4. Create GitHub Repository

Push this code to a new GitHub repository.

5. Connect to Cloudflare Workers

  1. Go to Cloudflare Dashboard
  2. Navigate to Workers & Pages
  3. Click Create applicationImport a Worker project from GitHub
  4. Connect your GitHub repo (root directory)

6. Configure Secrets

In Cloudflare Workers dashboard, go to your worker → SettingsVariables:

Add the following secrets via wrangler secret put:

# Via Wrangler CLI
wrangler secret put DISCORD_BOT_TOKEN
wrangler secret put PYTH_FEED_ID
wrangler secret put TICKER

Or set via dashboard:

  • DISCORD_BOT_TOKEN - Your Discord bot token
  • PYTH_FEED_ID - Pyth price feed ID (e.g., 0xe62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43)
  • TICKER - Ticker symbol (e.g., BTC, ETH, SOL)

7. Deploy

Once secrets are configured, Cloudflare will auto-deploy on push to main branch.

Or manually deploy:

npm install
npm run deploy

Nickname Format

Price Range Format Example
>= $1000 No decimals BTC: $50000
>= $100 2 decimals ETH: $3500.00
>= $1 3 decimals SOL: $150.000
< $1 4 decimals DOGE: $0.1500

File Structure

pythwrangler/
├── src/
│   ├── index.ts      # Cron handler
│   ├── pyth.ts       # Pyth API fetch
│   ├── discord.ts    # Discord API calls
│   └── format.ts     # Price formatting
├── wrangler.toml     # Cron config
├── package.json
└── tsconfig.json

Usage & Costs

  • Cron Schedule: 1 request per minute
  • Daily Requests: ~1,440 per day (well under 100,000/day free tier limit)
  • Cost: $0 on Cloudflare Workers free tier - DYOR

Local Testing

npm install
npx wrangler dev --test-scheduled
curl "http://localhost:8787/cdn-cgi/handler/scheduled?cron=*/1+*+*+*+*"

License

MIT

About

Cloudflare Worker pricebot using Pyth Network oracle to keep Discord bot nicknames updated with live crypto prices. Serverless, free tier friendly.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors