v0.5.0

Discord Setup

Last updated


This page covers constructing the core/discord bot, the gateway intents it requests, and the Input your handler receives.

Constructor


  1. bot, err := discord.New(token)

New returns token is required for an empty token. It creates a discordgo.Session authenticated as Bot <token>, registers the message and interaction handlers, and does not connect. There are no options.

Gateway intents


IntentWhy it is requested
IntentGuildsMakes the gateway send GUILD_CREATE, which fills discordgo's state cache; without it Input.ChannelName is always ""
IntentsGuildMessagesMessages in server channels
IntentDirectMessagesDirect messages to the bot
IntentMessageContentMessage text and attachments

IntentMessageContent is privileged. Enable Message Content Intent under Bot settings in the Discord Developer Portal; otherwise Input.Text is empty for most messages (Discord only sends content for DMs and messages that mention the bot).

Start


Start(ctx) first calls REST User("@me") to validate the token, then Session.Open() to connect the WebSocket. A bad token fails at the first step, before any connection is opened. Messages sent by the bot itself are filtered using discordgo's session state, so they never reach the handler.

Input


FieldTypeContent
ChannelIDstringChannel of the message or prompt
ChannelNamestringRead from the state cache with no extra request; "" for DMs or a cache miss
GuildIDstringServer ID; "" for DMs
MessageIDstringThe user's message; for modal and select answers, the prompt message
UserID / UsernamestringSender; for interactions, taken from the guild member or, in DMs, the user
TextstringMessage content, the modal answer, or the single-select pick
Attachments[]*discordgo.MessageAttachmentEvery uploaded file (images, files, audio); tell them apart with ContentType
CallbackPicks[]stringValues chosen in a SendMultiSelect menu
Raw*discordgo.MessageCreateThe SDK event for plain messages; nil for interaction answers

All IDs are Discord snowflake strings.

中文