Skip to content

Commit

Permalink
more local commands
Browse files Browse the repository at this point in the history
xp and shit counter
shit leaderboards script
json script bindings script
  • Loading branch information
Dragon1320 committed Sep 8, 2018
1 parent a509aad commit 079e6a5
Show file tree
Hide file tree
Showing 5 changed files with 921 additions and 172 deletions.
6 changes: 4 additions & 2 deletions src/bot/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
class Command {
constructor(options) {

this.author_id = options.author_id;

this.name = options.name;
this.description = options.description;
this.thumbnail = options.thumbnail;
Expand All @@ -16,8 +18,8 @@ class Command {

this.cb = options.cb;
}
run(client, message, guildDoc) {
this.cb(client, message, guildDoc);
run(client, message, guild, user, script, match) {
this.cb(client, message, guild, user, script, match);
}
}

Expand Down
260 changes: 260 additions & 0 deletions src/bot/commands/binding.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,260 @@
"use strict"

const Command = require("../command");

const schemas = require("../../db");

const binding = new Command({

name: "Script Bindings",
description: "*temp*",
thumbnail: "https://cdn.discordapp.com/attachments/209040403918356481/487658989069402112/pjiicjez.png",
marketplace_enabled: true,

type: "js",
match_type: "command",
match: "bind;unbind",

featured: false,

cb: function(client, message, guild, user, script, match) {

// action: text, file, embed

// text:
// bind text_binding text this is a text binding

// file:
// bind file_binding file https://cdn.discordapp.com/attachments/209040403918356481/487658989069402112/pjiicjez.png

// embed:
// bind embed_binding embed --description this is an embed binding

const args = message.content.split(" ");

const bind = args.shift();
const name = args.shift();
const action = args.shift();

// bind name action args
if (match === "bind") {

if (name === undefined) {

return message.reply("name was not set");
}

if (action === undefined) {

return message.reply("action was not set");
}

if (args[0] === undefined) {

return message.reply("args were not set");
}

const dataArgs = [];

if (action === "text") {

dataArgs.push({
field: "text",
value: args.join(" ")
});
}

if (action === "file") {

dataArgs.push({
field: "url",
value: args[0]
});
}

if (action === "embed") {

// author
// color
// description
// footer
// image
// thumbnail
// timestamp
// title
// url

// --description adfsd sdfgn df --author sdf sdf ff

let embedArgs = args.join(" ").split("--");
embedArgs.shift();
embedArgs = embedArgs.map(s => s.trim())

if (embedArgs.length % 2 !== 0) {

return message.reply("incorrect args were set");
}

for (let arg of embedArgs) {

const fieldArgs = arg.split(" ");

const field = fieldArgs.shift();
const value = fieldArgs.join(" ");

dataArgs.push({
field,
value
});
}
}

if (dataArgs.length === 0) {

return message.reply("incorrect action was set");
}

const script = new schemas.ScriptSchema({

author_id: message.author.id,

name,
description: "auto generated using script bindings",
thumbnail: "https://cdn.discordapp.com/attachments/209040403918356481/487658989069402112/pjiicjez.png",
marketplace_enabled: false,

type: "json",
match_type: "command",
match: name,

code: null,
data: {
action,
args: dataArgs
},

local: false,
featured: false,
verified: false,
likes: 0,
guild_count: 1,
use_count: 0,

created_with: "script_bindings"
});

script
.save()
.then(script => {

// get guild again to avoid conflicts
schemas.GuildSchema
.findOne({
discord_id: message.guild.id
})
.then(guild => {

const guildScript = new schemas.GuildScriptSchema({
object_id: script._id
});

guild.scripts.push(guildScript);

guild
.save()
.then(() => {

message.reply(`successfully bound '${name}'`);
})
.catch(error => {

message.reply(`error adding script to guild: ${error}`);
});
})
.catch(error => {

message.reply(`error finding guild: ${error}`);
});
})
.catch(error => {

message.reply(`error saving script: ${error}`);
});
}

// unbind name
if (match === "unbind") {

schemas.ScriptSchema
.find({
name, created_with: "script_bindings"
})
.then(scripts => {
if (scripts.length === 0) {

return message.reply(`script '${name}' not found`);
}

schemas.GuildSchema
.findOne({
discord_id: message.guild.id
})
.then(guild => {

let found = false;
for (let i = 0; i < scripts.length; i++) {

for (let j = 0; j < guild.scripts.length; j++) {

if (scripts[i]._id.equals(guild.scripts[j].object_id)) {

found = true;
guild.scripts.splice(i, 1);

break;
}
}
}

if (found = false) {

return message.reply(`script not found in current guild`);
}

guild
.save()
.then(() => {

const promises = [];
for (let i = 0; i < scripts.length; i++) {

promises.push(scripts[i].remove());
}

Promise.all(promises).then(() => {

message.reply(`successfully removed '${name}'`);
}).catch(error => {

message.reply(`error removing scripts: ${error}`);
});
})
.catch(error => {

message.reply(`error saving guild: ${error}`);
});
})
.catch(error => {

message.reply(`error finding guild: ${error}`);
});
})
.catch(error => {

message.reply(`error finding scripts: ${error}`);
});
}
}
});

module.exports = binding;

0 comments on commit 079e6a5

Please sign in to comment.