Hello is it possible to includes discord logs on my web dashboard. If the answer is yes, do you have any example(s)?
Code :
server.js
class WebSocket {
constructor(token, port, client) {
this.token = token
this.port = port
this.client = client
this.app = express()
// Register Handlebars instance as view engine
this.app.engine('hbs', hbs({
extname: 'hbs', // Extension (*.hbs Files)
defaultLayout: 'layout', // Main layout -> layouts/layout.hbs
layoutsDir: __dirname + '/layouts' // Layouts directory -> layouts/
}))
// Set folder views/ as location for views files
this.app.set('views', path.join(__dirname, 'views'))
// Set hbs as view engine
this.app.set('view engine', 'hbs')
// Set public/ as public files root
this.app.use(express.static(path.join(__dirname, 'public')))
// Register bodyParser as parser for Post requests body in JSON-format
this.app.use(bodyParser.urlencoded({ extended: false }));
this.app.use(bodyParser.json());
this.registerRoots()
// Start websocket on port defined in constructors arguments
this.server = this.app.listen(port, () => {
console.log("Websocket API set up at port " + this.server.address().port)
})
}
/**
* Compare passed token with the token defined on
* initialization of the websocket
* @param {string} _token Token from request parameter
* @returns {boolean} True if token is the same
*/
checkToken(_token) {
return (_token == this.token)
}
/**
* Register root pathes
*/
registerRoots() {
this.app.get('/', (req, res) => {
var _token = req.query.token
if (!this.checkToken(_token)) {
// Render error view if token does not pass
res.render('error', { title: "ERROR" })
return
}
// Collect all text channels and put them into an
// array as object { id, name }
var chans = []
this.client.guilds.first().channels
.filter(c => c.type == 'text')
.forEach(c => {
chans.push({id: c.id, name: c.name})
})
var status = this.client.user.presence.status
var on = []
if(status === "online"){
on.push("Online")
}
if(status === "offline") {
on.push("Offline")
}
var h1logs = []
var logs = this.client.on('messageDelete', message => {
h1logs.push(`Hello`);
});
// Render index view and pass title, token
// and channels array
res.render('index', {
title: "SECRET INTERFACE",
token: _token,
chans,
username: this.client.user.username,
on,
h1logs
})
})
this.app.post('/sendMessage', (req, res) => {
var _token = req.body.token
var channelid = req.body.channelid
var text = req.body.text
if(!_token || !channelid || !text)
return res.sendStatus(400);
if (!this.checkToken(_token))
return res.sendStatus(401)
var chan = this.client.guilds.first().channels.get(channelid)
// catch post request and if token passes,
// send message into selected channel
if (chan) {
chan.send(text)
res.sendStatus(200)
} else
res.sendStatus(406)
})
}
}
module.exports = WebSocket
index.hbs
<iframe style="display: none" name="dummyframe" id="dummyframe"></iframe>
<form action="/sendMessage" method="POST" target="dummyframe" >
{{!-- Pass token back to authenticate web request --}}
<input type="hidden" name="token" value="{{ token }}" />
{{!-- Channel selector --}}
<select name="channelid">
{{#each chans}}
<option value="{{this.id}}">{{this.name}}</option>
{{/each}}
</select>
{{!-- Message text input --}}
<input type="text" name="text" placeholder="TEXT">
{{!-- Send button --}}
<input type="submit" class="button" value="SEND">
</form>