Skip to content

Commit

Permalink
Read/Write Files
Browse files Browse the repository at this point in the history
  • Loading branch information
PakL committed Sep 20, 2019
1 parent 2163966 commit d926098
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 5 deletions.
11 changes: 7 additions & 4 deletions addon.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const VarArgument = require('./modules/VarArgument')
const VarStorage = require('./modules/VarStorage')
const VarContext = require('./modules/VarContext')
const VarRequest = require('./modules/VarRequest')
const VarFile = require('./modules/VarFile')

const Sync = require('./sync')

Expand Down Expand Up @@ -525,7 +526,7 @@ class Bot extends UIPage {
* @returns {VarInterface}
*/
getVarInterface(expression, args, msg) {
let matches = expression.match(/^(%(\-?[0-9]+)(,\-?[0-9]{0,})?|\/[a-z0-9]+|\$[a-z0-9]+|(🔗|🌎|🌍|🌏)(https?:\/\/.+))(\[(.*?)\])?$/i)
let matches = expression.match(/^(%(\-?[0-9]+)(,\-?[0-9]{0,})?|\/[a-z0-9]+|\$[a-z0-9]+|(🔗|🌎|🌍|🌏)(https?:\/\/.+)|📝([a-z]:(.*?)))(\[(.*?)\])?$/i)
if(matches !== null) {
let argMatches = matches[1].match(/^%(\-?[0-9]+)(,\-?[0-9]{0,})?$/)
if(argMatches) {
Expand All @@ -545,6 +546,8 @@ class Bot extends UIPage {
return new VarStorage(matches[1].substr(1))
} else if(matches[1].match(/^(🔗|🌎|🌍|🌏)https?:\/\/.+$/i)) {
return new VarRequest(matches[5])
} else if(matches[1].match(/^📝([a-z]:(.*?))$/i)) {
return new VarFile(matches[6])
}
} else {
return new VarArgument(expression)
Expand All @@ -560,9 +563,9 @@ class Bot extends UIPage {
getVarIndex(expression, args, msg) {
return new Promise(async (resolve, reject) => {
try {
let matches = expression.match(/^(%(\-?[0-9]+)(,\-?[0-9]{0,})?|\/[a-z0-9]+|\$[a-z0-9]+|(🔗|🌎|🌍|🌏)(https?:\/\/.+))(\[(.*?)\])?$/i)
if(matches !== null && typeof(matches[6]) !== 'undefined') {
let indexStr = matches[6].substr(1, matches[6].length-2)
let matches = expression.match(/^(%(\-?[0-9]+)(,\-?[0-9]{0,})?|\/[a-z0-9]+|\$[a-z0-9]+|(🔗|🌎|🌍|🌏)(https?:\/\/.+)|📝([a-z]:(.*?)))(\[(.*?)\])?$/i)
if(matches !== null && typeof(matches[8]) !== 'undefined') {
let indexStr = matches[8].substr(1, matches[8].length-2)
let interf = this.getVarInterface(indexStr, args, msg)
if(interf === null) {
resolve(null)
Expand Down
72 changes: 72 additions & 0 deletions modules/VarFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
const VarInterface = require('./VarInterface')
const fs = require('fs')

class VarFile extends VarInterface {

constructor(name)
{
super(name)
this.name = this.name.replace(/%20/g, ' ')
this.value = ''
}

loadFile() {
try {
this.value = fs.readFileSync(this.name, { encoding: 'utf8' })
} catch(e) {}
let rows = this.value.split('\r\n')
return rows
}

saveFile(rows) {
this.value = rows.join('\r\n')
try {
fs.writeFileSync(this.name, this.value)
} catch(e) {}
}

validateIndex(index) {
if(typeof(index) == 'string' && index.match(/^[0-9]+$/)) {
index = parseInt(index)
}
if(typeof(index) !== 'number') index = 0
return index
}

getValue(index) {
const self = this
return new Promise((y, n) => {
let rows = self.loadFile()
index = self.validateIndex(index)
if(rows.length <= index) {
index = 0
}
y(rows[index])
})
}

setTo(value, index) {
const self = this
return new Promise((y, n) => {
let rows = self.loadFile()
index = self.validateIndex(index)
rows[index] = value
self.saveFile(rows)
y()
})
}

addTo(value, index) {
const self = this
return new Promise((y, n) => {
let rows = self.loadFile()
index = self.validateIndex(index)
if(rows.length <= index) { rows[index] = '' }
rows[index] += value
self.saveFile(rows)
y()
})
}

}
module.exports = VarFile
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "chatbot",
"description": "A chatbot for TTVST. Let's you automate responses, and such...",
"version": "1.0.0",
"version": "1.1.0",
"systems": ["win32"],
"toolversion": "1.x.x"
}
12 changes: 12 additions & 0 deletions views/commandhelp_de.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ <h4>Inhalt</h4>
<li><a href="#chapter_2_2_2">Umgebung</a></li>
<li><a href="#chapter_2_2_3">Ablage</a></li>
<li><a href="#chapter_2_2_4">HTTP-Abfrage</a></li>
<li><a href="#chapter_2_2_5">Datei lesen/schreiben</a></li>
</ol>
</li>
<li><a href="#chapter_2_3">Vorverarbeitung</a></li>
Expand Down Expand Up @@ -297,6 +298,17 @@ <h3 id="chapter_2_2_4">2.2.4. HTTP-Abfrage</h3>
<strong>Beispiel:</strong><br>
<code>{% print 🔗https://twitch.center/customapi/quote?token=xxxxx&data={{§%1,§}} %}</code>
</p>
<h3 id="chapter_2_2_5">2.2.5. Datei lesen/schreiben</h3>
<p>
Ähnlich wie die HTTP-Abfragen kannst du Dateien auslesen indem das Emoji 📝 vor einen absoluten Dateipfad
gesetzt wird. Anders als HTTP-Abfragen kann eine Datei aber auch beschrieben werden. Da Zeilenumbrüche
nicht unterstützt werden kann ein Index verwendet werden um spezifische Zeilen aus der Datei zu lesen.
Keinen Index zu definieren bedeutet Zeile 1.<br>
<strong>Beispiel:</strong><br>
<code>{% print 📝C:\Pfad\zu\Datei.txt %}</code> - Gibt den Inhalt der ersten Zeile aus der Datei aus<br>
<code>{% set $num > 📝C:\Pfad\zu\Datei.txt[1] %}</code> - Schreibt den Inhalt von $num in die zweite Zeile der Datei<br>
<code>{% add $str > 📝C:\Pfad\zu\Datei.txt %}</code> - Hängt den Inhalt von $str an die erste Zeile der Datei an
</p>
<h3 id="chapter_2_3">2.3. Vorverarbeitung</h3>
<p>
Um noch etwas komplexere Ausgaben zu generieren können spezielle <code>print</code> Anweisungen ausgeführt
Expand Down
13 changes: 13 additions & 0 deletions views/commandhelp_en.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ <h4>Content</h4>
<li><a href="#chapter_2_2_2">Context</a></li>
<li><a href="#chapter_2_2_3">Storage</a></li>
<li><a href="#chapter_2_2_4">HTTP request</a></li>
<li><a href="#chapter_2_2_5">Read/Write File</a></li>
</ol>
</li>
<li><a href="#chapter_2_3">Preprocessing</a></li>
Expand Down Expand Up @@ -286,6 +287,18 @@ <h3 id="chapter_2_2_4">2.2.4. HTTP request</h3>
<strong>Example:</strong><br>
<code>{% print 🔗https://twitch.center/customapi/quote?token=xxxxx&data={{§%1,§}} %}</code>
</p>
<h3 id="chapter_2_2_5">2.2.5. Read/Write File</h3>
<p>
Similar to the HTTP request you can read from files by putting the emoji 📝 in front of an absolute path to
a file. Other than the HTTP request you are able to write into a file using that method. Since line breaks
are not supported you can use a variable index to write to or read from specific rows (0-indexed: meaning
the first row has index 0, the second row index 1 and so on). Specifiying no index will always read or write
the first line.<br>
<strong>Example:</strong><br>
<code>{% print 📝C:\path\to\file.txt %}</code> - prints the contents of the first row of the file<br>
<code>{% set $num > 📝C:\path\to\file.txt[1] %}</code> - writes the contents of $num into the second row of the file<br>
<code>{% add $str > 📝C:\path\to\file.txt %}</code> - appends the contents of $str to the first row of the file
</p>
<h3 id="chapter_2_3">2.3. Preprocessing</h3>
<p>
To generate even more complex output, special <code>print</code> statements can be executed before the rest
Expand Down

0 comments on commit d926098

Please sign in to comment.