mirror of
https://github.com/discordeno/discordeno.git
synced 2026-09-17 08:47:22 +00:00
implement a logger system functions
This commit is contained in:
+7
-7
@@ -12,8 +12,8 @@ import {
|
||||
// import { encode } from "https://deno.land/std/strings/mod.ts"
|
||||
// import { BufReader } from "https://deno.land/std/io/bufio.ts"
|
||||
// import { TextProtoReader } from "https://deno.land/std/textproto/mod.ts"
|
||||
import { blue, green, red, yellow } from "https://deno.land/std/fmt/colors.ts"
|
||||
import { keepDiscordWebsocketAlive, updatePreviousSequenceNumber } from "./websocket.ts";
|
||||
import { logGreen, logRed, logYellow, logBlue } from "../utils/logger.ts";
|
||||
|
||||
class Client {
|
||||
/** The bot's token. This should never be used by end users. It is meant to be used internally to make requests to the Discord API. */
|
||||
@@ -37,7 +37,7 @@ class Client {
|
||||
// Open a WS with the url from discord.
|
||||
const sock = await connectWebSocket(data.url);
|
||||
console.log(sock)
|
||||
console.log(green("ws connected! (type 'close' to quit)"));
|
||||
logGreen("ws connected! (type 'close' to quit)");
|
||||
|
||||
for await (const msg of sock.receive()) {
|
||||
if (typeof msg === "string") {
|
||||
@@ -45,15 +45,15 @@ class Client {
|
||||
const json = JSON.parse(msg)
|
||||
this.handleDiscordPayload(json, sock)
|
||||
} catch {
|
||||
console.log(red(`Invalid JSON String send by discord: ${msg}`))
|
||||
logRed(`Invalid JSON String send by discord: ${msg}`)
|
||||
}
|
||||
console.log(yellow("< " + msg));
|
||||
logYellow("< " + msg);
|
||||
} else if (isWebSocketPingEvent(msg)) {
|
||||
console.log(blue("< ping"));
|
||||
logBlue("< ping");
|
||||
} else if (isWebSocketPongEvent(msg)) {
|
||||
console.log(blue("< pong"));
|
||||
logBlue("< pong");
|
||||
} else if (isWebSocketCloseEvent(msg)) {
|
||||
console.log(red(`closed: code=${msg.code}, reason=${msg.reason}`));
|
||||
logRed(`closed: code=${msg.code}, reason=${msg.reason}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
import { blue, green, red, yellow } from "https://deno.land/std/fmt/colors.ts"
|
||||
|
||||
export const getTime = () => {
|
||||
const now = new Date()
|
||||
const hours = now.getHours()
|
||||
const minute = now.getMinutes()
|
||||
|
||||
let hour = hours
|
||||
let amOrPm = `AM`
|
||||
if (hour > 12) {
|
||||
amOrPm = `PM`
|
||||
hour = hour - 12
|
||||
}
|
||||
|
||||
return `${hour >= 10 ? hour : `0${hour}`}:${minute >= 10 ? minute : `0${minute}`} ${amOrPm}`
|
||||
}
|
||||
|
||||
export const logGreen = (text: string) => {
|
||||
console.log(green(`[${getTime()}] => ${text}`))
|
||||
}
|
||||
|
||||
export const logBlue = (text: string) => {
|
||||
console.log(blue(`[${getTime()}] => ${text}`))
|
||||
}
|
||||
|
||||
export const logRed = (text: string) => {
|
||||
console.log(red(`[${getTime()}] => ${text}`))
|
||||
}
|
||||
|
||||
export const logYellow = (text: string) => {
|
||||
console.log(yellow(`[${getTime()}] => ${text}`))
|
||||
}
|
||||
Reference in New Issue
Block a user