Skip to content

Latest commit

 

History

History
104 lines (61 loc) · 3.33 KB

presentation.md

File metadata and controls

104 lines (61 loc) · 3.33 KB

Writing your own Discord Bot

Based on notes from a presentation given to the Port Townsend Web Developers Meetup on Jan 13 2021.

Discord

Discord is an American VoIP, instant messaging and digital distribution platform designed for creating communities. Users communicate with voice calls, video calls, text messaging, media and files in private chats or as part of communities called "servers." Servers are a collection of persistent chat rooms and voice chat channels.

Much like Slack but more focused on gaming than business. Some overlap.

  • Some limited integrations, Twitch and YouTube (gamer oriented)
  • Supports webhooks, ie. from Github
  • more focus on "bots"

The Discord desktop and web client is built on the Electron framework.

Discord Libraries

Officially Vetted Libraries for Integration with many languages

Discord API Docs

This example code uses Discord.js

Discord.js Docs

Another exanple bot

https://github.com/chalda/DiscordBot

Technical Notes

Security

Bots have an

  • ID
  • public key
  • secret key
  • token (used to invite them to a guild)

OAuth supported (required?) for larger bots

Uses Twitter "snowflake" for IDs. see https://discord.com/developers/docs/reference#snowflakes

Consistency

"True consistency is impossible." "Eventually consistent." You may receive events 0-N times. "Clients should operate on events and results from the API in as much of an idempotent behavior as possible."

Some things are done via HTTP. Most via websockets

More Info

Discord.js library

https://discordjs.guide

https://discord.js.org/#/docs/main/stable/general/welcome

Discord, Websocket Specific

https://discord.com/developers/docs/topics/gateway#gateways

https://discord.com/developers/docs/topics/gateway#list-of-intents

https://discord.com/developers/docs/topics/gateway#commands-and-events-gateway-events

Discord HTTP Resources

e.g., to access a specific Guild

GET https://discordapp.com/api/v8/guilds/:guildID
User-Agent: DiscordBot
Authorization: Bot <your secret here>

Some HTTP/REST examples

Web Socket Events

You will mainly be dealing with these Gateway Events

Alternatively, check out the list of events fired by a Discord.js Client

As you can see, there are a lot, but bots tend to work most with these: (most also have update and delete)

CLIENT_READY: 'ready', MESSAGE_CREATE: 'message', GUILD_CREATE: 'guildCreate', CHANNEL_CREATE: 'channelCreate',

The inner payload is a Message, Guild, or Channel

Example Message