Start playing with a Winston client that DMs me

This commit is contained in:
Jordan Eldredge 2019-06-23 17:21:36 -04:00
parent 427c7701be
commit d820b8c830
3 changed files with 45 additions and 11 deletions

View file

@ -0,0 +1,17 @@
const { Transport } = require("winston");
class DiscordWinstonTransport extends Transport {
constructor(channel) {
super();
this._channel = channel;
}
async log(info, callback) {
const { message, ...rest } = info;
await this._channel.send(`${message}`);
// Perform the writing to the remote service
callback();
}
}
module.exports = DiscordWinstonTransport;

View file

@ -3,6 +3,9 @@ const path = require("path");
const Discord = require("discord.js");
const config = require("../config");
const logger = require("../logger");
const DiscordWinstonTransport = require("../DiscordWinstonTransport");
const CAPTBARITONE_USER_ID = "254029485463044116";
const client = new Discord.Client();
@ -53,17 +56,33 @@ client.on("message", async message => {
return;
}
const command = rawCommand.slice(1);
logger.info('User triggered WebampBot command', {command, user: message.author.username, args, channel: message.channel.name || 'DM'});
logger.info("User triggered WebampBot command", {
command,
user: message.author.username,
args,
channel: message.channel.name || "DM"
});
const handler = handlers[command];
if(handler == null) {
logger.warn('Unknown command', {command, user: message.author.username, args});
if (handler == null) {
logger.warn("Unknown command", {
command,
user: message.author.username,
args
});
return;
}
handler(message, args);
});
client.on("error", e => {
logger.error('The WebSocket encountered an error:', e);
logger.error("The WebSocket encountered an error:", e);
});
client.login(config.discordToken);
async function main() {
await client.login(config.discordToken);
const captbaritone = await client.fetchUser(CAPTBARITONE_USER_ID);
const channel = await captbaritone.createDM();
logger.add(new DiscordWinstonTransport(channel));
}
main();

View file

@ -1,18 +1,16 @@
const { createLogger, format, transports } = require('winston');
const { createLogger, format, transports } = require("winston");
const logger = createLogger({
level: 'info',
level: "info",
format: format.combine(
format.timestamp({
format: 'YYYY-MM-DD HH:mm:ss'
format: "YYYY-MM-DD HH:mm:ss",
}),
format.errors({ stack: true }),
format.splat(),
format.json()
),
transports: [
new transports.File({ filename: 'combined.log' })
]
transports: [new transports.File({ filename: "combined.log" })],
});
module.exports = logger;