@@ -28,8 +28,10 @@ function prepareDatabase()
28
28
}
29
29
30
30
// assert the tables in the db
31
- assertTable ( "messages" , [ "id" , "author" ] ) ;
32
- assertTable ( "revisions" , [ "id" , "timestamp" , "content" ] ) ;
31
+ // should we care about which server it came from as well, for the sake of generality?
32
+ // even if it's only intended to be used on one server?
33
+ assertTable ( "Message" , [ "msgId" , "channel" , "author" ] ) ;
34
+ assertTable ( "Content" , [ "fullMessage" , "date" , "associatedMsg" ] ) ;
33
35
} ;
34
36
35
37
// close the database if its open
@@ -43,10 +45,10 @@ function closeDatabase()
43
45
function insertRow ( table , values )
44
46
{
45
47
// can this sql be done more prettily?
46
- database . run ( `INSERT INTO ${ table } VALUES(${ new Array ( values . length ) . fill ( "?" ) . join ( ", " ) } )` ,
47
- ( values , e ) => {
48
- if ( e ) console . error ( `Error inserting row: ${ e } ` ) ;
49
- } ) ;
48
+ const query = `INSERT INTO ${ table } VALUES(${ new Array ( values . length ) . fill ( "?" ) . join ( ", " ) } )` ;
49
+ database . run ( query , values , e => {
50
+ if ( e ) console . error ( `Error inserting row: ${ e } ` ) ;
51
+ } ) ;
50
52
}
51
53
52
54
// get the latest timestamp for a message object
@@ -57,16 +59,29 @@ function getLatestTimestamp(message)
57
59
return message . editedTimestamp || message . createdTimestamp ;
58
60
}
59
61
62
+ // get an appropriate name for a discord channel
63
+ function getChannelName ( channel )
64
+ {
65
+ if ( channel . type === "dm" )
66
+ // or should channel name in this case be null?
67
+ return channel . recipient . tag ;
68
+ else if ( channel . type === "group" )
69
+ return channel . name ;
70
+ else if ( channel . type === "text" )
71
+ return `#${ channel . name } ` ;
72
+ }
73
+
60
74
// create a new entry in the database for this message
61
75
function createEntry ( message )
62
76
{
63
- insertRow ( "messages " , [ message . id , message . author . tag ] ) ;
77
+ insertRow ( "Message " , [ message . id , getChannelName ( message . channel ) , message . author . tag ] ) ;
64
78
}
65
79
66
80
// add a revision to a message in the database
67
81
function storeRevision ( message )
68
82
{
69
- insertRow ( "revisions" , [ message . id , getLatestTimestamp ( message ) , message . content ] ) ;
83
+ // store this revision of the message
84
+ insertRow ( "Content" , [ message . content , getLatestTimestamp ( message ) , message . id ] ) ;
70
85
}
71
86
72
87
// tab over newlines
@@ -93,12 +108,16 @@ function logMessage(message, edit)
93
108
}
94
109
95
110
// return an array of all the code blocks contained in a string
111
+ // each object in the array has a "lang" property and a "code" property
96
112
function getCodeBlocks ( string )
97
113
{
98
- const regex = / \` \` \` ( [ a - z ] * [ \s \S ] * ?) \` \` \` / g;
114
+ const regex = / \` \` \` ( ( [ a - z ] + ) \n ) ? \n * ( [ \s \S ] * ?) \n * \` \` \` / g;
99
115
const result = [ ] ;
100
116
var matches ;
101
- while ( ( matches = regex . exec ( string ) ) !== null ) result . push ( matches [ 1 ] ) ;
117
+ while ( ( matches = regex . exec ( string ) ) !== null ) result . push ( {
118
+ lang : matches [ 2 ] ,
119
+ code : matches [ 3 ] ,
120
+ } ) ;
102
121
return result ;
103
122
}
104
123
0 commit comments