-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
utils.ts
103 lines (96 loc) · 2.52 KB
/
utils.ts
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
import log4js from "log4js";
import request from "request";
import chalk from "chalk";
import _ from "lodash";
import { REST } from "@discordjs/rest";
import { Routes } from "discord-api-types/v9";
import { Guild, Client, User, ActivityType } from "discord.js";
export const token: string = process.env?.DISCORD_TOKEN as string;
if (!token) {
throw new Error("Token required.");
}
// setup logger
export function getLogger(name: string) {
let logPattern = "%[[%p]%] " + chalk.red("[%c]") + " - %m";
if (!process.env.PAPERTRAIL_API_TOKEN) {
logPattern = "[%d{yy/MM/dd hh:mm:ss}] " + logPattern;
}
// configure pattern
log4js.configure({
appenders: {
out: { type: "stdout", layout: { type: "pattern", pattern: logPattern } },
},
categories: {
default: { appenders: ["out"], level: process.env.LOG_LEVEL || "info" },
},
});
return log4js.getLogger(name + "-" + process.pid);
}
// create a pretty log message for a user / guild
export const prettyLog = (
{
guild,
channel = undefined,
author = undefined,
}: {
guild: Guild;
channel?: any | undefined;
author?: User | undefined;
},
action: string,
log: string = ""
) => {
const logMessage = [
chalk.blue(
"[" +
(guild?.name || "direct message") +
"#" +
(channel?.name || "") +
"]"
),
chalk.yellow(
"[" +
(author?.username
? author.username + "#" + author.discriminator
: "server") +
"]"
),
chalk.magenta("[" + action + "]"),
log,
];
return logMessage.join(" ");
};
// send updated stats to bots.discord.com
export function updateServerCount(bot: Client) {
bot.user?.setPresence({
activities: [
{
name:
"MTG on " +
bot.guilds.cache.size +
" servers (" +
bot.ws.shards.size +
" shards)",
type: ActivityType.Playing,
url: "https://github.com/bra1n/judgebot",
},
],
});
const options = {
url: "https://bots.discord.pw/api/bots/240537940378386442/stats",
method: "POST",
headers: { Authorization: process.env.BOT_TOKEN },
body: { server_count: bot.guilds.cache.size || 0 },
json: true,
};
// post stats to bots.discord.pw
if (process.env.BOT_TOKEN) {
request(options);
}
// post stats to discordbots.org
if (process.env.BOT_TOKEN2) {
options.url = "https://discordbots.org/api/bots/240537940378386442/stats";
options.headers["Authorization"] = process.env.BOT_TOKEN2;
request(options);
}
}