Skip to content

Sending Attachments

Attachments let you upload files directly to Discord. discord-luau supports sending them by providing the raw file content as a string.

This guide uses the onMessage event, which requires the MessageContent intent. Make sure it is enabled in the Discord Developer Portal under your application’s Bot tab.

Call :addAttachment(content, name) on a builders.message.message object, where content is the raw file data as a string and name is the filename that will appear in Discord.

local discord = require("@self/../luau_packages/discord")
local classes = require("@self/../luau_packages/classes")
local builders = require("@self/../luau_packages/builders")
local env = require("@self/../.env")
local bot = discord.bot.new({
token = env.DISCORD_BOT_TOKEN,
intents = builders.intents.new({ "Guilds", "GuildMessages", "MessageContent" }):build(),
reconnect = true,
})
bot.onMessage:listen(function(message: classes.Message)
if message.content ~= "!file" then
return
end
message:replyAsync(
builders.message.message.new()
:addAttachment("Hello from discord-luau!", "hello.txt")
:build()
):await()
end)
bot.onAllShardsReady:listenOnce(function()
print(`Bot '{bot.user.username}' is online!`)
end)
bot:connectAsync():await()

Call :addAttachment() multiple times to include more than one file in a single message. Each call adds one file.

message:replyAsync(
builders.message.message.new()
:addAttachment("First file contents", "first.txt")
:addAttachment("Second file contents", "second.txt")
:build()
):await()

Attachments, message content, and embeds can all be combined on the same message builder.

message:replyAsync(
builders.message.message.new()
:setContent("Here is your file:")
:addAttachment("some,csv,data\n1,2,3", "data.csv")
:build()
):await()
Full script
local discord = require("@self/../luau_packages/discord")
local classes = require("@self/../luau_packages/classes")
local builders = require("@self/../luau_packages/builders")
local env = require("@self/../.env")
local bot = discord.bot.new({
token = env.DISCORD_BOT_TOKEN,
intents = builders.intents.new({ "Guilds", "GuildMessages", "MessageContent" }):build(),
reconnect = true,
})
bot.onMessage:listen(function(message: classes.Message)
if message.content ~= "!file" then
return
end
message:replyAsync(
builders.message.message.new()
:addAttachment("Hello from discord-luau!", "hello.txt")
:build()
):await()
end)
bot.onAllShardsReady:listenOnce(function()
print(`Bot '{bot.user.username}' is online!`)
end)
bot:connectAsync():await()
  • Bot - the discord.bot class, gateway connection and event emitters
  • Message builder - builders.message.message, constructs message payloads including attachments
  • Message class - classes.Message, the object passed to onMessage handlers
  • Intents builder - builders.intents, constructs the gateway intent bitfield
  • Futures - the FutureLike async primitive returned by async calls