
11 changed files with 303 additions and 0 deletions
@ -0,0 +1,79 @@
|
||||
### Discord bots #### |
||||
guilds.json |
||||
token.json |
||||
log |
||||
|
||||
|
||||
# Created by https://www.gitignore.io/api/node,visualstudiocode |
||||
|
||||
### Node ### |
||||
# Logs |
||||
logs |
||||
*.log |
||||
npm-debug.log* |
||||
yarn-debug.log* |
||||
yarn-error.log* |
||||
|
||||
# Runtime data |
||||
pids |
||||
*.pid |
||||
*.seed |
||||
*.pid.lock |
||||
|
||||
# Directory for instrumented libs generated by jscoverage/JSCover |
||||
lib-cov |
||||
|
||||
# Coverage directory used by tools like istanbul |
||||
coverage |
||||
|
||||
# nyc test coverage |
||||
.nyc_output |
||||
|
||||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) |
||||
.grunt |
||||
|
||||
# Bower dependency directory (https://bower.io/) |
||||
bower_components |
||||
|
||||
# node-waf configuration |
||||
.lock-wscript |
||||
|
||||
# Compiled binary addons (http://nodejs.org/api/addons.html) |
||||
build/Release |
||||
|
||||
# Dependency directories |
||||
node_cache |
||||
node_modules/ |
||||
jspm_packages/ |
||||
|
||||
# Typescript v1 declaration files |
||||
typings/ |
||||
|
||||
# Optional npm cache directory |
||||
.npm |
||||
|
||||
# Optional eslint cache |
||||
.eslintcache |
||||
|
||||
# Optional REPL history |
||||
.node_repl_history |
||||
|
||||
# Output of 'npm pack' |
||||
*.tgz |
||||
|
||||
# Yarn Integrity file |
||||
.yarn-integrity |
||||
|
||||
# dotenv environment variables file |
||||
.env |
||||
|
||||
|
||||
### VisualStudioCode ### |
||||
.vscode/* |
||||
!.vscode/settings.json |
||||
!.vscode/tasks.json |
||||
!.vscode/launch.json |
||||
!.vscode/extensions.json |
||||
.history |
||||
|
||||
# End of https://www.gitignore.io/api/node,visualstudiocode |
@ -0,0 +1,14 @@
|
||||
{ |
||||
// Use IntelliSense to learn about possible Node.js debug attributes. |
||||
// Hover to view descriptions of existing attributes. |
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 |
||||
"version": "0.2.0", |
||||
"configurations": [ |
||||
{ |
||||
"type": "node", |
||||
"request": "launch", |
||||
"name": "Launch Program", |
||||
"program": "${workspaceRoot}/bootstrap.js" |
||||
} |
||||
] |
||||
} |
@ -0,0 +1,9 @@
|
||||
{ |
||||
"files.exclude": { |
||||
"log": true, |
||||
".npmrc": true, |
||||
"node_modules": true, |
||||
"node_cache": true, |
||||
"token.json": true |
||||
} |
||||
} |
@ -0,0 +1,13 @@
|
||||
const GuildData = require("./models/guild-data.js"); |
||||
|
||||
module.exports = { |
||||
onCommand(commandObj, commandsObj, params, guildData, message) { |
||||
switch (commandObj.command) { |
||||
case commandsObj.commandName.command: |
||||
return; //return promise!
|
||||
} |
||||
}, |
||||
onNonCommandMsg(message, guildData) { |
||||
return; |
||||
} |
||||
}; |
@ -0,0 +1,22 @@
|
||||
{ |
||||
"generic": { |
||||
"saveFile": "./guilds.json", |
||||
"saveIntervalSec": 60, |
||||
"website": "https://benji7425.github.io", |
||||
"discordInvite": "https://discord.gg/SSkbwSJ", |
||||
"defaultDMResponse": "This bot does not have any handling for direct messages. To learn more or get help please visit %s, or join my Discord server here: %s" }, |
||||
"commands": { |
||||
"version": { |
||||
"command": "version", |
||||
"description": "Returns the bot version", |
||||
"syntax": "version", |
||||
"admin": false |
||||
}, |
||||
"help": { |
||||
"command": "help", |
||||
"description": "Display information about commands available to you", |
||||
"syntax": "help", |
||||
"admin": false |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,121 @@
|
||||
//node imports
|
||||
const FileSystem = require("fs"); //manage files
|
||||
const Util = require("util"); //various node utilities
|
||||
|
||||
//external lib imports
|
||||
const Discord = require("discord.js"); |
||||
const JsonFile = require("jsonfile"); //save/load data to/from json
|
||||
|
||||
//my imports
|
||||
const DiscordUtil = require("discordjs-util"); //some discordjs helper functions of mine
|
||||
|
||||
//app components
|
||||
const GuildData = require("./models/guild-data.js"); //data structure for guilds
|
||||
const PackageJSON = require("../package.json"); //used to provide some info about the bot
|
||||
const Bot = require("./bot.js"); |
||||
|
||||
//global vars
|
||||
let writeFile = null; |
||||
|
||||
//use module.exports as a psuedo "onready" function
|
||||
module.exports = (client, config = null) => { |
||||
config = config || require("./config.json"); //load config file
|
||||
const guildsData = FileSystem.existsSync(config.generic.saveFile) ? fromJSON(JsonFile.readFileSync(config.generic.saveFile)) : {}; //read data from file, or generate new one if file doesn't exist
|
||||
|
||||
//create our writeFile function that will allow other functions to save data to json without needing access to the full guildsData or config objects
|
||||
//then set an interval to automatically save data to file
|
||||
writeFile = () => JsonFile.writeFile(config.generic.saveFile, guildsData, err => { if (err) DiscordUtil.dateError("Error writing file", err); }); |
||||
setInterval(() => writeFile(), config.generic.saveIntervalSec * 1000); |
||||
|
||||
//handle messages
|
||||
client.on("message", message => { |
||||
if (message.author.id !== client.user.id) { //check the bot isn't triggering itself
|
||||
|
||||
//check whether we need to use DM or text channel handling
|
||||
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(Util.format(config.generic.defaultDMResponse, config.generic.website, config.generic.discordInvite)); |
||||
}, |
||||
text: (client, config, message, guildsData) => { |
||||
const isCommand = message.content.startsWith(message.guild.me.toString()); |
||||
let guildData = guildsData[message.guild.id]; |
||||
|
||||
if (!guildData) |
||||
guildData = guildsData[message.guild.id] = new GuildData({ id: message.guild.id }); |
||||
|
||||
if (isCommand) { |
||||
const userIsAdmin = message.member.permissions.has("ADMINISTRATOR"); |
||||
const botName = "@" + (message.guild.me.nickname || client.user.username); |
||||
|
||||
const split = message.content.toLowerCase().split(/\ +/); //split the message at whitespace
|
||||
const command = split[1]; //extract the command used
|
||||
const commandObj = config.commands[Object.keys(config.commands).find(x => config.commands[x].command.toLowerCase() === command)]; //find the matching command object
|
||||
|
||||
if (!commandObj || (!commandObj.admin && !userIsAdmin)) |
||||
return; |
||||
|
||||
const params = split.slice(2, split.length); //extract the parameters passed for the command
|
||||
const expectedParamCount = commandObj.syntax.split(/\ +/).length - 1; //calculate the number of expected command params
|
||||
|
||||
let finalisedParams; |
||||
if (params.length > expectedParamCount) //if we have more params than needed
|
||||
finalisedParams = params.slice(0, expectedParamCount - 1).concat([params.slice(expectedParamCount - 1, params.length).join(" ")]); |
||||
else //else we either have exactly the right amount, or not enough
|
||||
finalisedParams = params; |
||||
|
||||
//find which command was used and handle it
|
||||
switch (command) { |
||||
case config.commands.version.command: |
||||
message.reply(`${PackageJSON.name} v${PackageJSON.version}`); |
||||
break; |
||||
case config.commands.help.command: |
||||
message.channel.send(createHelpEmbed(botName, config, userIsAdmin)); |
||||
break; |
||||
default: |
||||
if (finalisedParams.length >= expectedParamCount) |
||||
Bot.onCommand(commandObj, config.commands, finalisedParams, guildData, message) |
||||
.then(msg => { |
||||
message.reply(msg); |
||||
writeFile(); |
||||
}) |
||||
.catch(err => { |
||||
message.reply(err); |
||||
DiscordUtil.dateError(err); |
||||
}); |
||||
else |
||||
message.reply(`Incorrect syntax!\n**Expected:** *${botName} ${commandObj.syntax}*\n**Need help?** *${botName} ${config.commands.help.command}*`); |
||||
break; |
||||
} |
||||
} |
||||
else |
||||
Bot.onNonCommandMsg(message, guildData); |
||||
} |
||||
}; |
||||
|
||||
function fromJSON(json) { |
||||
const guildsData = Object.keys(json); |
||||
guildsData.forEach(guildID => { json[guildID] = new GuildData(json[guildID]); }); |
||||
return json; |
||||
} |
||||
|
||||
function createHelpEmbed(name, config, userIsAdmin) { |
||||
const commandsArr = Object.keys(config.commands).map(x => config.commands[x]).filter(x => userIsAdmin || !x.admin); |
||||
|
||||
const embed = new Discord.RichEmbed().setTitle("__Help__"); |
||||
|
||||
commandsArr.forEach(command => { |
||||
embed.addField(command.command, `${command.description}\n**Usage:** *${name} ${command.syntax}*${userIsAdmin && command.admin ? "\n***Admin only***" : ""}`); |
||||
}); |
||||
|
||||
embed.addField("__Need more help?__", `[Visit my website](${config.generic.website}) or [Join my Discord](${config.generic.discordInvite})`, true); |
||||
|
||||
return { embed }; |
||||
} |
@ -0,0 +1,7 @@
|
||||
const DiscordUtil = require("discordjs-util"); |
||||
|
||||
module.exports = class GuildData { |
||||
constructor({ id }) { |
||||
this.id = id; |
||||
} |
||||
}; |
@ -0,0 +1,20 @@
|
||||
const Discord = require("discord.js"); |
||||
const DiscordUtil = require("discordjs-util"); |
||||
|
||||
const client = new Discord.Client(); |
||||
|
||||
process.on("uncaughtException", (err) => { |
||||
DiscordUtil.dateError("Uncaught exception!", err); |
||||
}); |
||||
|
||||
client.login(require("./token.json").token); |
||||
|
||||
client.on("ready", () => { |
||||
DiscordUtil.dateLog("Registered bot " + client.user.username); |
||||
require("./app/index.js")(client); |
||||
client.user.setPresence({ game: { name: "benji7425.github.io", type: 0 } }); |
||||
}); |
||||
|
||||
client.on("disconnect", eventData => { |
||||
DiscordUtil.dateError("Bot was disconnected!", eventData.code, eventData.reason); |
||||
}); |
Loading…
Reference in new issue