Skip to content

Commit

Permalink
Added admin command section
Browse files Browse the repository at this point in the history
You can now add, edit and remove commands from the admin section.
  • Loading branch information
Mstiekema committed Jan 29, 2017
1 parent 4fcfe91 commit 717bedf
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 15 deletions.
16 changes: 8 additions & 8 deletions install.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@ connection.query(
function (err, result) {if (err) {return}}
)

connection.query('insert into user set ? ', {"name": options.identity.admin, "points": 0, "num_lines": 0, "level": 500, "isMod": true }, function (err, result) {if (err) {console.log(err)}})
if (options.identity.admin != options.channels[0]) {
connection.query('insert into user set ? ', {"name": options.channels[0], "points": 0, "num_lines": 0, "level": 400, "isMod": true }, function (err, result) {if (err) {console.log(err)}})
connection.query('insert into user set ? ', {"name": options.identity.username, "points": 0, "num_lines": 0, "level": 300, "isMod": true }, function (err, result) {if (err) {console.log(err)}})
} else {
connection.query('insert into user set ? ', {"name": options.identity.username, "points": 0, "num_lines": 0, "level": 300, "isMod": true }, function (err, result) {if (err) {console.log(err)}})
}
// connection.query('insert into user set ? ', {"name": options.identity.admin, "points": 0, "num_lines": 0, "level": 500, "isMod": true }, function (err, result) {if (err) {console.log(err)}})
// if (options.identity.admin != options.channels[0]) {
// connection.query('insert into user set ? ', {"name": options.channels[0], "points": 0, "num_lines": 0, "level": 400, "isMod": true }, function (err, result) {if (err) {console.log(err)}})
// connection.query('insert into user set ? ', {"name": options.identity.username, "points": 0, "num_lines": 0, "level": 300, "isMod": true }, function (err, result) {if (err) {console.log(err)}})
// } else {
// connection.query('insert into user set ? ', {"name": options.identity.username, "points": 0, "num_lines": 0, "level": 300, "isMod": true }, function (err, result) {if (err) {console.log(err)}})
// }

var sql = "insert into module (moduleName, moduleDescription, state) values ?";
var moduleSettings = [
Expand All @@ -121,7 +121,7 @@ var moduleSettings = [
['sub', "Notifies chat if a user subs or resubs", true],
['timeout', "Saves a log if a user is timed out or banned", true],
];
connection.query(sql, [moduleSettings], function (err, result) {if (err) {console.log(err)}})
// connection.query(sql, [moduleSettings], function (err, result) {if (err) {console.log(err)}})

var sql2 = "insert into commands (commName, response, commDesc, cdType, cd, level, commUse) values ?"
var standardCommands = [
Expand Down
6 changes: 3 additions & 3 deletions modules/streaminfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ module.exports = {
}
}
request(info, function (error, response, body) {
var base = JSON.parse(body).streams[0]
if (base != undefined) {
if (JSON.parse(body).streams[0] != undefined) {
var base = JSON.parse(body).streams[0]
var streamid = base._id
connection.query('select * from streaminfo where streamid = ?', streamid, function(err, result) {
if (result[0] != undefined) {
Expand Down Expand Up @@ -80,4 +80,4 @@ module.exports = {
})
}, function () {}, true );
}
}
}
3 changes: 2 additions & 1 deletion static/html/adminmenu.htm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ <h3><u>Menu</u></h3>
<br>
<h3><u>Admin panel</u></h3>
<p onclick="songlist()">Songlist</p>
<p onclick="commManager()">Command Manager</p>
<p onclick="logs()">Admin logs</p>
<p onclick="module()">Modules</p>
<footer>
Expand All @@ -17,4 +18,4 @@ <h3><u>Admin panel</u></h3>
<span onclick="dev()">Developer </span>
</span>
</footer>
</div>
</div>
55 changes: 55 additions & 0 deletions static/js/commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
var create = document.getElementsByClassName("create");
var remove = document.getElementsByClassName("remove");
var edit = document.getElementsByClassName("edit");
var update = document.getElementById("update")
var add = document.getElementById("add")
var socket = io.connect();

for (var i = 0; i < create.length; i++) {
create[i].addEventListener('click', function() {
window.location.href = "/admin/commands/create"
}, false);
}

for (var i = 0; i < remove.length; i++) {
remove[i].addEventListener('click', function() {
var comm = this.id
socket.emit('removeComm', comm)
alert("Succesfully removed the following command: " + comm)
window.location.reload()
}, false);
}

for (var i = 0; i < edit.length; i++) {
edit[i].addEventListener('click', function() {
window.location.href = "/admin/commands/edit/" + this.id
}, false);
}

if (update) {
update.addEventListener('click', function() {
var updatedComm = {
commName: window.location.href.split("/").pop(),
response: document.getElementById('response').value,
level: document.getElementById('level').value,
cdType: document.getElementById('cdType').value,
cd: document.getElementById('cd').value
}
socket.emit('updateComm', updatedComm)
window.location.href = "/admin/commands"
}, false);
}

if (add) {
add.addEventListener('click', function() {
var newComm = {
commName: document.getElementById('command').value,
response: document.getElementById('response').value,
level: document.getElementById('level').value,
cdType: document.getElementById('cdType').value,
cd: document.getElementById('cd').value
}
socket.emit('addCom', newComm)
window.location.href = "/admin/commands"
}, false);
}
8 changes: 6 additions & 2 deletions static/js/yucisite.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ $("#searchBar").keyup(function(ev) {
var name = $("input").val();
location.href = "/user/" + name;
}
});
});

function home() {
window.location.href = '/';
Expand Down Expand Up @@ -37,6 +37,10 @@ function sub() {
window.location.href = '/sub';
}

function commManager() {
window.location.href = '/admin/commands';
}

function history() {
window.location.href = '/history/' + new Date().toISOString().substr(0, 10);
}
Expand All @@ -59,4 +63,4 @@ function github() {

function dev() {
window.open("https://mstiekema.github.io", "_blank");
}
}
16 changes: 16 additions & 0 deletions templates/admin/addCommand.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<% include adminheader.ejs %>
<div class="top"><h1>Add command</h1></div>
<div class="search"><input id="searchBar" type="text"></div>
<div class="main">
<p>
Command: <input type="text" id="command" size=10 value="!"> <br>
Response: <input type="text" id="response" size=100> <br>
Level: <input type="number" min=100 max=500 size=3 id="level" value=100> <br>
Cooldown type: <select id="cdType"><option value="global" selected>Global</option><option value="user">User</option></select> <br>
Cooldown: <input type="number" min=1 max=3600 size=4 id="cd" value=10> <br>
</p>
<button id="add">Submit</button>
</div>
<script type="text/javascript" src="/../js/yucisite.js"></script>
<script type="text/javascript" src="/../js/commands.js"></script>
</body>
21 changes: 21 additions & 0 deletions templates/admin/commands.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<% include adminheader.ejs %>
<div class="top"><h1>Manage commands</h1></div>
<div class="search"><input id="searchBar" type="text"></div>
<div class="main">
<p><button class="create">Create command</button></p>
<% commands.forEach(function(commands) { %>
<div class="commands"><p>
<b>Command:</b> <%= commands.commName %> <br>
<% if (commands.commDesc != null) { %> <b>Description:</b> <%= commands.commDesc %> <br> <% } %>
<% if (commands.response != null) { %><b>Response:</b> <%= commands.response %> <br> <% } %>
<% if (commands.commUse != null) { %> <b>Usage:</b><br> <%= commands.commUse %> <br> <% } %>
<b>Cooldown type:</b> <%= commands.cdType %> <br>
<b>Cooldown:</b> <%= commands.cd %> seconds <br>
<b>User level:</b> <%= commands.level %> <br>
<button class="remove" id=<%= commands.commName %>>Remove command</button> <button class="edit" id=<%= commands.commName %>>Edit command</button>
</p></div>
<% }) %>
</div>
<script type="text/javascript" src="/../js/yucisite.js"></script>
<script type="text/javascript" src="/../js/commands.js"></script>
</body>
20 changes: 20 additions & 0 deletions templates/admin/editCommand.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<% include adminheader.ejs %>
<div class="top"><h1>Edit command</h1></div>
<div class="search"><input id="searchBar" type="text"></div>
<div class="main">
<% if (commands[0] != undefined) { %>
<p>
Command: <%= commands[0].commName %> <br>
Response: <input type="text" id="response" size=100 value="<%= commands[0].response %>"> <br>
Level: <input type="number" min=100 max=500 size=3 id="level" value=<%= commands[0].level %>> <br>
Cooldown type: <select id="cdType"><option value="global" selected>Global</option><option value="user">User</option></select> <br>
Cooldown: <input type="number" min=1 max=3600 size=4 id="cd" value=<%= commands[0].cd %>> <br>
</p>
<button id="update">Submit</button>
<% } else { %>
<p>Couldn't find a command with that name. Please try again</p>
<% } %>
</div>
<script type="text/javascript" src="/../js/yucisite.js"></script>
<script type="text/javascript" src="/../js/commands.js"></script>
</body>
36 changes: 35 additions & 1 deletion website.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,19 @@ io.on('connection', function (socket) {
socket.emit('nextSong');
}})
})
socket.on('removeComm', function (data) {
connection.query('delete from commands where commName = ?', data, function(err, result) {})
})
socket.on('addCom', function (data) {
connection.query('insert into commands set ?', data, function (err, result) {
console.log(err)
console.log(result)
})
})
socket.on('updateComm', function(data) {
connection.query('update commands set commName = "' + data.commName + '", response = "' + data.response + '", level = "' + data.level + '",\
cdType = "' + data.cdType + '", cd = "' + data.cd + '" where commName = "' + data.commName + '"', function(err, result) {})
})
socket.on('disableModule', function(data) {
connection.query('update module set state = 0 where moduleName = ?', data, function(err, result) {
socket.emit('reload')
Expand Down Expand Up @@ -400,6 +413,27 @@ app.get('/admin/modules', function(req, res) {
});
});

app.get('/admin/commands', function(req, res) {
connection.query('select * from commands where response IS NOT NULL ORDER BY level', function(err, result) {
res.render('admin/commands.html', {
commands: result
})
});
});

app.get('/admin/commands/create', function(req, res) {
connection.query('select * from commands', function(err, result) {res.render('admin/addCommand.html')});
});

app.get('/admin/commands/edit/:id', function(req, res) {
var id = req.params.id
connection.query('select * from commands where response IS NOT NULL AND commName = ?', id, function(err, result) {
res.render('admin/editCommand.html', {
commands: result
})
});
});

app.get('/403', function(req, res) {
connection.query('select * from streaminfo', function(err, result) {res.render('error403.html')});
});
Expand All @@ -410,4 +444,4 @@ app.all('*', function(req, res, next) {

console.log("Started website")

server.listen(3000);
server.listen(3000);

0 comments on commit 717bedf

Please sign in to comment.