Skip to content

Commit

Permalink
chore: Sharding 💎
Browse files Browse the repository at this point in the history
  • Loading branch information
youKnowOwO committed Aug 22, 2020
1 parent 59e30fa commit 1bd729c
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 28 deletions.
1 change: 1 addition & 0 deletions config.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"prefix": "",
"color": "",
"owners": [],
"shard": 1,
"lavalink": {
"hosts": {
"rest": "",
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "yumeko",
"version": "1.0.0",
"version": "0.0.0",
"description": "A simple multipurpose bot",
"main": "dist",
"scripts": {
Expand All @@ -20,6 +20,7 @@
"@types/common-tags": "^1.8.0",
"@types/eslint": "^7.2.1",
"@types/figlet": "^1.2.0",
"@types/moment-duration-format": "^2.2.2",
"@types/node": "^14.6.0",
"@types/turndown": "^5.0.0",
"@typescript-eslint/eslint-plugin": "^3.9.1",
Expand All @@ -39,6 +40,7 @@
"figlet": "^1.5.0",
"lavalink": "^2.10.0",
"moment": "^2.27.0",
"moment-duration-format": "^2.3.2",
"node-superfetch": "^0.1.10",
"turndown": "^6.0.0"
}
Expand Down
7 changes: 4 additions & 3 deletions src/commands/General/About.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ import { DeclareCommand } from "../../decorators";
})
export default class AboutCommand extends Command {
public async exec(msg: Message): Promise<Message> {
const commands = this.collector!.commands.filter(x => !!x.option.aliases.length);
return msg.ctx.send(stripIndents`
Hi there, ${msg.author}! I’m **${this.client.user!.tag}** and I’m beyond happy and glad to meet you!
👋 | Hi there, ${msg.author}! I’m **${this.client.user!.tag}** and I’m beyond happy and glad to meet you!
I’m just an ordinary bot whose job is to make your Discord Server more fun and exciting
for members to chat on. I do what other bots do as well, like: sending random images of animals,
generating games for this server’s members, and most importantly, I play and queue song requests.
To conclude, I carry \`${this.collector!.commands.filter(x => msg.author.isDev || !x.option.devOnly).size}\` commands in total. To test me out,
why not start by generating my help panel? **${this.client.config.prefix}help**
To conclude, I carry \`${commands.filter(x => msg.author.isDev || !x.option.devOnly).size}\` commands in total. To test me out,
why not start by generating my help panel? **${this.client.config.prefix}help**.
`);
}
}
29 changes: 22 additions & 7 deletions src/commands/General/Stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,25 @@ import { loadavg } from "os";
})
export default class StatsCommand extends Command {
public async exec(msg: Message): Promise<Message> {
const [usersSize, channelsSize, serversSize] = this.client.shard ?
this.parseSizeEvaluate(await this.client.shard.broadcastEval(`[
this.users.cache.size,
this.channels.cache.size,
this.guilds.cache.size
]`)) : [this.client.users.cache.size, this.client.channels.cache.size, this.client.guilds.cache.size];
const usage = process.memoryUsage();
const embed = new MessageEmbed()
.setColor(this.client.config.color)
.setTitle("♪ My Current Statistic")
.setThumbnail(this.client.user!.displayAvatarURL())
.setDescription(codeBlock("ini", stripIndents`
Memory Usage : ${Math.round(process.memoryUsage().heapUsed / 1024 / 1024)} MB
Uptime : ${moment(this.client.uptime).format("hh:mm:ss")}
.setDescription(codeBlock("ascii", stripIndents`
Shard : ${Number(msg.guild!.shardID) + 1} / ${this.client.shard ? this.client.shard.count : 1}
Memory Usage : ${(usage.heapUsed / 1024 / 1024).toFixed(2)} / ${Math.round(100 * (usage.heapTotal / 1048576)) / 100} MB
Uptime : ${moment.duration(this.client.uptime).format("YY [years] MM [month] DD [days], hh:mm:ss")}
CPU : ${Math.round(loadavg()[0] * 100) / 100}%
Users : ${this.client.users.cache.size.toLocaleString()}
Channels : ${this.client.channels.cache.size.toLocaleString()}
Servers : ${this.client.guilds.cache.size.toLocaleString()}
Users : ${usersSize.toLocaleString()}
Channels : ${channelsSize.toLocaleString()}
Servers : ${serversSize.toLocaleString()}
WS ping : ${this.client.ws.ping.toFixed(2)}ms
Node : ${process.version}
`))
Expand All @@ -40,7 +48,14 @@ export default class StatsCommand extends Command {
if (msg.guild!.members.cache.has(user.id)) return `• ${user} (${user.id})`;
return `• ${user.tag} (${user.id})`;
}))
.addField("\u200B", "[Github](https://github.com/youKnowOwO) | [Repository](https://github.com/youKnowOwO/yumeko-ts)");
.addField("\u200B", "[Github](https://github.com/youKnowOwO) | [Repository](https://github.com/youKnowOwO/yumeko)");
return msg.ctx.send(embed);
}

public parseSizeEvaluate(data: [number, number, number][]): [number, number, number] {
const result: [number, number, number] = [0, 0, 0];
for (const dat of data)
for (let i = 0; i < 3; i++) result[i] += dat[i];
return result;
}
}
6 changes: 1 addition & 5 deletions src/events/Message.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import type YumekoClient from "../classes/Client";
import * as linter from "../util/CodeLinter";
import * as myriad from "../util/Myriad";
import type { Message } from "discord.js";
import { Event } from "../interfaces";

Expand All @@ -9,9 +7,7 @@ export default class MessageEvent implements Event {
public constructor(public readonly client: YumekoClient) {}
public exec(msg: Message): void {
this.client.collector.runner.handle(msg);
linter.handle(msg);
myriad.handle(msg);
if (msg.guild && [`<@${this.client.user!.id}>`, `<@!${this.client.user!.id}>`].includes(msg.content))
if (msg.guild && !this.client.collector.runner.isCooldown(msg, false) && [`<@${this.client.user!.id}>`, `<@!${this.client.user!.id}>`].includes(msg.content))
return this.client.collector.commands.get("about")!.exec(msg);
}
}
4 changes: 0 additions & 4 deletions src/events/MessageEdit.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import type YumekoClient from "../classes/Client";
import * as linter from "../util/CodeLinter";
import * as myriad from "../util/Myriad";
import type { Message } from "discord.js";
import { Event } from "../interfaces";

Expand All @@ -10,7 +8,5 @@ export default class MessageEvent implements Event {
public exec(oldMessage: Message, newMessage: Message): void {
if (oldMessage.content === newMessage.content) return undefined;
this.client.collector.runner.handle(newMessage);
linter.handle(newMessage);
myriad.handle(newMessage);
}
}
2 changes: 1 addition & 1 deletion src/events/Ready.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default class ReadyEvent implements Event {
public constructor(public readonly client: YumekoClient) {}
public exec (): void {
this.client.log.info(stripIndents`
${this.client.log.color(this.client.user!.tag, "FFFFFF")} is Ready to play.
${this.client.log.color(this.client.user!.tag, "FFFFFF")} is Ready to play. ${this.client.shard ? this.client.shard.ids.map(x => this.client.log.color(`#${x + 1}`, "00FFFF")).join(", ") : ""}
`);
this.client.lavalink.userID = this.client.user!.id;
presence.call(null, this.client);
Expand Down
5 changes: 4 additions & 1 deletion src/extension/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import "./Message";
import "./User";
import "./Guild";
import "./Guild";

import dur from "moment-duration-format";
dur(require("moment"));
15 changes: 11 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import YumekoClient from "./classes/Client";
import { ShardingManager } from "discord.js";
import { join } from "path";

const config = require("../config.json");

if (process.argv[2] === "dev") {
require("./util/EnvLoader");
require("../config.json").debug = true;
config.debug = true;
}
const client = new YumekoClient();
client.login(process.env.TOKEN);

const path = join(__dirname, "./yumeko.js");
const shards = new ShardingManager(path, {
totalShards: process.argv[2] === "dev" ? 1 : config.shard
});
shards.spawn();
4 changes: 4 additions & 0 deletions src/yumeko.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import YumekoClient from "./classes/Client";

const client = new YumekoClient();
client.login(process.env.TOKEN);
15 changes: 13 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.5.tgz#dcce4430e64b443ba8945f0290fb564ad5bac6dd"
integrity sha512-7+2BITlgjgDhH0vvwZU/HZJVyk+2XUlvxXe8dFMedNX/aMkaOq++rMAFXc0tM7ij15QaWlbdQASBR9dihi+bDQ==

"@types/moment-duration-format@^2.2.2":
version "2.2.2"
resolved "https://registry.yarnpkg.com/@types/moment-duration-format/-/moment-duration-format-2.2.2.tgz#da0c9501861661d19fdb62415907a93c44709a81"
integrity sha512-CuYswsMI3y5uR5sD9i/VUqIbZrsYN2eaCs7nH3qpDl2CZlNI48mjMf4ve2RpQ/65irprtnQVetfnea9my+jqcg==
dependencies:
moment ">=2.14.0"

"@types/node@*", "@types/node@^14.6.0":
version "14.6.0"
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.6.0.tgz#7d4411bf5157339337d7cff864d9ff45f177b499"
Expand Down Expand Up @@ -575,7 +582,6 @@ diff@^4.0.1:

"discord.js@github:discordjs/discord.js":
version "12.3.1"
uid "05c9e301639848121dda27ab1f8a643f465be502"
resolved "https://codeload.github.com/discordjs/discord.js/tar.gz/05c9e301639848121dda27ab1f8a643f465be502"
dependencies:
"@discordjs/collection" "^0.1.6"
Expand Down Expand Up @@ -1280,7 +1286,12 @@ mkdirp@^0.5.0, mkdirp@^0.5.1:
dependencies:
minimist "^1.2.5"

moment@^2.27.0:
moment-duration-format@^2.3.2:
version "2.3.2"
resolved "https://registry.yarnpkg.com/moment-duration-format/-/moment-duration-format-2.3.2.tgz#5fa2b19b941b8d277122ff3f87a12895ec0d6212"
integrity sha512-cBMXjSW+fjOb4tyaVHuaVE/A5TqkukDWiOfxxAjY+PEqmmBQlLwn+8OzwPiG3brouXKY5Un4pBjAeB6UToXHaQ==

moment@>=2.14.0, moment@^2.27.0:
version "2.27.0"
resolved "https://registry.yarnpkg.com/moment/-/moment-2.27.0.tgz#8bff4e3e26a236220dfe3e36de756b6ebaa0105d"
integrity sha512-al0MUK7cpIcglMv3YF13qSgdAIqxHTO7brRtaz3DlSULbqfazqkc5kEjNrLDOM7fsjshoFIihnU8snrP7zUvhQ==
Expand Down

0 comments on commit 1bd729c

Please sign in to comment.