-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move tag operations to a manager file
- Loading branch information
Showing
5 changed files
with
81 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,14 @@ | ||
const fs = require('fs') | ||
|
||
if (!fs.existsSync('./databases/tags.json')) fs.writeFileSync('./databases/tags.json', '{}') | ||
const tag = require('../utils/tag') | ||
|
||
module.exports = { | ||
id: 'add-tag', | ||
async execute(interaction) { | ||
const tags = JSON.parse(fs.readFileSync('./databases/tags.json', 'utf-8')) | ||
const name = interaction.fields.fields.find(f => f.customId === 'name').value | ||
const content = interaction.fields.fields.find(f => f.customId === 'content').value | ||
const image = interaction.fields.fields.find(f => f.customId === 'image').value | ||
if (tags[name]) return interaction.reply({ content: `A tag with the name ${name} already exists.`, ephemeral: true }) | ||
if (tag.get(name)) return interaction.reply({ content: `A tag with the name ${name} already exists.`, ephemeral: true }) | ||
|
||
tags[name] = { | ||
content, | ||
image | ||
} | ||
fs.writeFileSync('./databases/tags.json', JSON.stringify(tags, null, 4)) | ||
tag.add(name, content, image) | ||
interaction.reply({ content: `Added tag ${name}.`, ephemeral: true }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,14 @@ | ||
const fs = require('fs') | ||
|
||
if (!fs.existsSync('./databases/tags.json')) fs.writeFileSync('./databases/tags.json', '{}') | ||
const tag = require('../utils/tag') | ||
|
||
module.exports = { | ||
id: 'edit-tag', | ||
async execute(interaction) { | ||
const tags = JSON.parse(fs.readFileSync('./databases/tags.json', 'utf-8')) | ||
const name = interaction.fields.fields.find(f => f.customId === 'name').value | ||
const content = interaction.fields.fields.find(f => f.customId === 'content').value | ||
const image = interaction.fields.fields.find(f => f.customId === 'image').value | ||
if (!tags[name]) return interaction.reply({ content: `A tag with the name ${name} does not exist.`, ephemeral: true }) | ||
if (!tag.get(name)) return interaction.reply({ content: `A tag with the name ${name} does not exist.`, ephemeral: true }) | ||
|
||
tags[name] = { | ||
content, | ||
image | ||
} | ||
fs.writeFileSync('./databases/tags.json', JSON.stringify(tags, null, 4)) | ||
tag.modify(name, content, image) | ||
interaction.reply({ content: `Edited tag ${name}.`, ephemeral: true }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
const fs = require('fs') | ||
|
||
function ensureDatabase() { | ||
const exists = fs.existsSync('./databases/tags.json') | ||
if (!exists) fs.writeFileSync('./databases/tags.json', '[]') | ||
try { | ||
JSON.parse(fs.readFileSync('./databases/tags.json')) | ||
} catch { | ||
fs.writeFileSync('./databases/tags.bak.json', fs.readFileSync('./databases/tags.json')) | ||
console.log('Your tags database was corrupted, so we had to reset it. You can find a backup in ./databases/tags.bak.json') | ||
fs.writeFileSync('./databases/tags.json', '[]') | ||
} | ||
} | ||
|
||
function writeDatabase(data) { | ||
ensureDatabase() | ||
fs.writeFileSync('./databases/tags.json', JSON.stringify(data)) | ||
} | ||
|
||
function getDatabase() { | ||
ensureDatabase() | ||
return JSON.parse(fs.readFileSync('./databases/tags.json')) | ||
} | ||
|
||
module.exports = { | ||
add: async (name, content, image) => { | ||
ensureDatabase() | ||
const database = getDatabase() | ||
if (database[name]) return | ||
database[name] = { | ||
name, | ||
content, | ||
image | ||
} | ||
writeDatabase(database) | ||
}, | ||
remove: name => { | ||
ensureDatabase() | ||
const database = getDatabase() | ||
delete database[name] | ||
writeDatabase(database) | ||
}, | ||
modify: (name, content, image) => { | ||
ensureDatabase() | ||
const database = getDatabase() | ||
if (!database[name]) database[name] = {} | ||
const item = database[name] | ||
item.name = name || item.name | ||
item.content = content || item.content | ||
item.image = image || item.image | ||
writeDatabase(database) | ||
}, | ||
get: name => { | ||
ensureDatabase() | ||
return getDatabase()[name] | ||
}, | ||
getAll: () => { | ||
ensureDatabase() | ||
return getDatabase() | ||
} | ||
} |