From 5428e71f01691e6b11d3c36e43deaf79cfbc7336 Mon Sep 17 00:00:00 2001 From: benji7425 Date: Sat, 12 Aug 2017 01:44:35 +0100 Subject: [PATCH] Merge in template --- .gitignore | 5 ++--- CHANGELOG.md | 3 +++ app/config.json | 6 ++++++ app/index.js | 55 +++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 7 ++++--- 5 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 CHANGELOG.md create mode 100644 app/config.json create mode 100644 app/index.js diff --git a/.gitignore b/.gitignore index f772ff4..7836b18 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,6 @@ -# Project specific - -token.json +### Discord bots #### guilds.json +token.json log ### Node ### diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..8deb76e --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,3 @@ +# Changelog + +## Unreleased \ No newline at end of file diff --git a/app/config.json b/app/config.json new file mode 100644 index 0000000..ddda549 --- /dev/null +++ b/app/config.json @@ -0,0 +1,6 @@ +{ + "saveIntervalSec": 60, + "commands": { + "version": "version" + } +} \ No newline at end of file diff --git a/app/index.js b/app/index.js new file mode 100644 index 0000000..11ba6b5 --- /dev/null +++ b/app/index.js @@ -0,0 +1,55 @@ +//node imports +const FileSystem = require("fs"); + +//external lib imports +const JsonFile = require("jsonfile"); + +//my imports +const DiscordUtil = require("discordjs-util"); + +//global vars +const SAVE_FILE = "./guilds.json"; + +module.exports = (client) => { + const config = require("./config.json"); + + const guildsData = FileSystem.existsSync(SAVE_FILE) ? fromJSON(JsonFile.readFileSync(SAVE_FILE)) : {}; + setInterval(() => writeFile(guildsData), config.saveIntervalSec * 1000); + + client.on("message", message => { + if (message.author.id !== client.user.id) { //check the bot isn't triggering itself + if (message.channel.type === "dm") + HandleMessage.DM(client, config, message); + else if (message.channel.type === "text" && message.member) + HandleMessage.Text(client, config, message, guildsData); + } + }); +}; + +const HandleMessage = { + DM: (client, config, message) => { + message.reply("This bot does not have any handling for direct messages. To learn more or get help please visit http://benji7425.github.io, or join my Discord server here: https://discord.gg/SSkbwSJ"); + }, + Text: (client, config, message, guildsData) => { + //handle admins invoking commands + if (message.content.startsWith(message.guild.me.toString()) //user is @mention-ing the bot + && message.member.permissions.has("ADMINISTRATOR")) //user has admin perms + { + const params = message.content.split(" "); //split the message at the spaces + switch (params[1]) { + //add handling for different commands here + case config.commands.version: + message.reply("v" + require("../package.json").version); + break; + } + } + } +}; + +function writeFile(guildsData) { + JsonFile.writeFile(SAVE_FILE, guildsData, err => { if (err) DiscordUtil.dateError("Error writing file", err); }); +} + +function fromJSON(json) { + throw "Not implemented"; +} \ No newline at end of file diff --git a/package.json b/package.json index 1223f18..b918016 100644 --- a/package.json +++ b/package.json @@ -2,9 +2,10 @@ "name": "discord-bot-feed-linker", "version": "2.0.0", "description": "", - "main": "index.js", + "main": "app/index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "echo \"Error: no test specified\" && exit 1", + "start": "node wrapper.js" }, "repository": { "type": "git", @@ -24,4 +25,4 @@ "jsonfile": "3.0.1", "urijs": "1.18.10" } -} +} \ No newline at end of file