Skip to content

Commit 733f0b8

Browse files
committed
updated database schema
1 parent edf3ad0 commit 733f0b8

File tree

1 file changed

+41
-10
lines changed

1 file changed

+41
-10
lines changed

bot.js

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ function prepareDatabase()
2828
}
2929

3030
// 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", "codeSnippet", "date", "language", "associatedMsg"]);
3335
};
3436

3537
// close the database if its open
@@ -43,10 +45,10 @@ function closeDatabase()
4345
function insertRow(table, values)
4446
{
4547
// 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+
});
5052
}
5153

5254
// get the latest timestamp for a message object
@@ -57,16 +59,41 @@ function getLatestTimestamp(message)
5759
return message.editedTimestamp || message.createdTimestamp;
5860
}
5961

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+
6074
// create a new entry in the database for this message
6175
function createEntry(message)
6276
{
63-
insertRow("messages", [message.id, message.author.tag]);
77+
insertRow("Message", [message.id, getChannelName(message.channel), message.author.tag]);
6478
}
6579

6680
// add a revision to a message in the database
6781
function storeRevision(message)
6882
{
69-
insertRow("revisions", [message.id, getLatestTimestamp(message), message.content]);
83+
// i'm not sure how we should handle multiple code snippets per message yet
84+
// so for now i'll just add separate row per snippet
85+
// (they are associated by timestamp)
86+
// also i'm assuming "date" column should be a universal time stamp
87+
// (but we can change it to a formatted date if that was the intention)
88+
getCodeBlocks(message.content).forEach(block =>
89+
insertRow("Content", [message.content, block.code,
90+
getLatestTimestamp(message), block.lang, message.id]));
91+
92+
// i'm not sure what to do with urls at the moment,
93+
// so i'll just stuff them in the same row for now
94+
getLinks(message.content).forEach(link =>
95+
insertRow("Content", [message.content, link,
96+
getLatestTimestamp(message), null, message.id]));
7097
}
7198

7299
// tab over newlines
@@ -93,12 +120,16 @@ function logMessage(message, edit)
93120
}
94121

95122
// return an array of all the code blocks contained in a string
123+
// each object in the array has a "lang" property and a "code" property
96124
function getCodeBlocks(string)
97125
{
98-
const regex = /\`\`\`([a-z]*[\s\S]*?)\`\`\`/g;
126+
const regex = /\`\`\`(([a-z]+)\n)?\n*([\s\S]*?)\n*\`\`\`/g;
99127
const result = [];
100128
var matches;
101-
while((matches = regex.exec(string)) !== null) result.push(matches[1]);
129+
while((matches = regex.exec(string)) !== null) result.push({
130+
lang: matches[2],
131+
code: matches[3],
132+
});
102133
return result;
103134
}
104135

0 commit comments

Comments
 (0)