-
Notifications
You must be signed in to change notification settings - Fork 3
/
models.js
executable file
·136 lines (119 loc) · 3.28 KB
/
models.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
"use strict"
var settings = require('./settings')
, mongoose = settings.mongoose
, hook = settings.hook
, Schema = mongoose.Schema
, ObjectId = mongoose.ObjectId
, mongooseAuth = require('mongoose-auth')
, LocalUser
, _ = require('underscore')
, logger = settings.logger(module)
var CommandSchema = new Schema({
name: String,
help: String,
type: String,
config: {},
options: Array,
commands: Array
})
var ServiceSchema = new Schema({
type: String,
rooms: {},
auth: {}
})
var BotSchema = new Schema({
name: {type: String, index: true},
services: [ServiceSchema],
commands: [CommandSchema],
config: {},
webhook_url: String,
disabled_commands: {}
})
var UserSchema = new Schema({
bots: [BotSchema]
})
UserSchema.methods.is_admin = function() {
return (settings.config.admins.indexOf(this.twit.screenName) !== -1)
}
UserSchema.plugin(mongooseAuth, {
everymodule: {
everyauth: {
User: function () {
return LocalUser
}
}
},
twitter: {
everyauth: {
myHostname: settings.config.twitter.hostname,
consumerKey: settings.config.twitter.consumerKey,
consumerSecret: settings.config.twitter.consumerSecret,
redirectPath: '/dashboard/'
}
}
})
exports.Command = mongoose.model('Commands', CommandSchema)
exports.Service = mongoose.model('Service', ServiceSchema)
exports.Bot = mongoose.model('Bot', BotSchema)
exports.User = mongoose.model('User', UserSchema)
LocalUser = mongoose.model('User')
exports.Bot.spawn = function (user, bot, callback) {
logger.debug('spawning ' + user._id + ": " + bot.name)
hook.emit('botUpdated', {user: user._id, bot: bot.toJSON(), callback: callback})
}
exports.Bot.service = function (bot, type) {
return bot.services.filter(function(service) { return service.type === type })[0]
}
// We are only using one bot per user for now
exports.User.bot = function (user) {
if(user.bots && user.bots.length) {
return user.bots[0]
}
}
// Setup the available commands
var commandSets = require('./common/commands').all()
var enabledCommandSets = ['base', 'search', 'curse', 'rage', 'help', 'simplegeo', 'fun', 'nerdicon']
var dc = []
var ac = {}
var loadCommands = function() {
Object.keys(commandSets).forEach(function(commandFile) {
// Substring due to .js extensions
var commandSetName = commandFile.substr(0, commandFile.length - 3)
var commands = commandSets[commandFile]
Object.keys(commands).forEach(function(commandName) {
var command = commands[commandName]
if (command.hidden) {
// Skip
return
}
var c = new exports.Command()
c.name = command.name
c.help = command.description
c.options = command.options
c.commands = command.commands
c.type = commandSetName
c.config = {}
if(c.commands)
console.log(c.commands[0])
if(_.any(enabledCommandSets, function(name) {return name === commandSetName})) {
dc.push(c)
}
if(!ac[commandSetName]) {
ac[commandSetName] = {}
}
ac[commandSetName][c.name] = c.toJSON()
})
})
}
exports.allCommands = function() {
if(!ac || Object.keys(ac).length === 0) {
loadCommands()
}
return ac
}
exports.defaultCommands = function() {
if(!dc || dc.length === 0) {
loadCommands()
}
return dc
}