Skip to content

Commit eeae578

Browse files
committed
Merge branch 'master' of github.com:dmg01/snippet-bot
2 parents 801938c + e08599d commit eeae578

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

bot.js

Lines changed: 29 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", "date", "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,29 @@ 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+
// store this revision of the message
84+
insertRow("Content", [message.content, getLatestTimestamp(message), message.id]);
7085
}
7186

7287
// tab over newlines
@@ -93,12 +108,16 @@ function logMessage(message, edit)
93108
}
94109

95110
// 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
96112
function getCodeBlocks(string)
97113
{
98-
const regex = /\`\`\`([a-z]*[\s\S]*?)\`\`\`/g;
114+
const regex = /\`\`\`(([a-z]+)\n)?\n*([\s\S]*?)\n*\`\`\`/g;
99115
const result = [];
100116
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+
});
102121
return result;
103122
}
104123

0 commit comments

Comments
 (0)