Skip to content
/ web3py-discord-bot Public template

Discord bot template using Python, web3.py, and websockets

Notifications You must be signed in to change notification settings

wolovim/web3py-discord-bot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

web3py-discord-bot

A Discord bot reference repo leveraging the newly rewritten web3.py WebSocketProvider, which includes eth_subscribe support. Check out the blog post for an introduction to WebSockets and the provider.

This bot can listen for subscriptions on a particular channel or execute one-off requests, like retrieving a balance or some block data.

The bot's architecture can support multiple chains. It will default to mainnet, but you can add sepolia or optimism to the end of any command to interact with those networks instead (if you include valid RPC URLs in your .env file).

One-off commands

One-off commands can be run in any channel that the bot has access to, for example:

  • !block latest sepolia - to return some data about the latest sepolia testnet block
  • !balance shaq.eth - to return an ether balance (ENS supported)

Subscriptions

The user flow for subscriptions is to:

  1. Tell the bot which channel to listen for messages in (!listen)
  2. Create a new subscription (!newHeads, !transfers)
  3. View your active subscriptions (!subs)
  4. Unsubscribe (!cancel newHeads, !cancel transfers)

A couple of sample subscription commands are included:

  • !newHeads will start a subscription watching for new block headers

  • !transfers will watch for new Transfer events from the Art Blocks NFT contract

    Screenshot 2023-11-01 at 10 45 32 AM

Setup

  • Follow discord.py directions to create a bot account on Discord. By the end you'll have a token.
  • Create your .env file to pass secrets into the app
    • Use the .env.example file to reference the key names
    • Store your newly created token as DISCORD_TOKEN
    • Add your websocket URL(s) to connect to a node
    • Optional: include the channel ID of a channel you'd like to see startup messages in
  • Install dependencies: pip install -r requirements.txt
  • Start the bot in a terminal: python main.py
  • Use the bot. Try !help to view a list of commands.
  • Bonus: host your bot! You can only do so much from your local machine.

Disclaimer

This bot is for educational purposes and does not aspire to be anything robustly production-grade. Hopefully it's a good starting point for your hackathon project or next adventure.

About

Discord bot template using Python, web3.py, and websockets

Resources

Stars

Watchers

Forks

Languages