Skip to content
This repository has been archived by the owner on Jun 2, 2023. It is now read-only.

Commit

Permalink
A lot has been fixed and switched to typeorm
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Jul 29, 2020
1 parent 47397ae commit 21f710c
Show file tree
Hide file tree
Showing 17 changed files with 184 additions and 106 deletions.
17 changes: 17 additions & 0 deletions archive/cmdTemplate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//@ts-ignore

import { Command } from "../../../lib";

export default class extends Command {
constructor() {
super("NAME", {
aliases: [""],
description: "",
usage: "",
botPerms: [],
userPerms: [],
});
}

async run() {}
}
11 changes: 11 additions & 0 deletions archive/evtTemplate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//@ts-ignore

import { BotEvent } from "../../../lib";

export default class extends BotEvent {
constructor() {
super("NAME");
}

async run() {}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
"chalk": "^4.1.0",
"discord.js": "^12.2.0",
"dotenv": "^8.2.0",
"mongoose": "^5.9.25"
"mongodb": "^3.5.9",
"typeorm": "^0.2.24"
},
"devDependencies": {
"@types/mongoose": "^5.7.32",
"@types/node": "^14.0.13",
"@types/ws": "^7.2.6"
}
Expand Down
39 changes: 37 additions & 2 deletions src/bot/events/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default class EMessage extends BotEvent {
if (!message.author.db) message.author._init();
if (!message.member!.db) message.member!._init();

const prefix = bot.getConfig("prefix");
const prefix = message.guild.db!.prefix;

// A safety check
if (!message.guild.me!.hasPermission("SEND_MESSAGES")) return;
Expand All @@ -27,6 +27,41 @@ export default class EMessage extends BotEvent {

// Trying to get the command
const command = bot.handler.getCmd(key);
if (command) await command.run(message, args);
if (!command) return;

if (
command.userPerms.every((p) =>
message.member!.hasPermission(p, {
checkAdmin: true,
checkOwner: true,
})
) &&
command.botPerms.every((p) =>
message.guild!.me!.hasPermission(p, { checkAdmin: true })
)
) {
await (command as any).run(message, args);
} else {
let perms = message.embed
.setTitle(`Not Enough Permissions`)
.setColor("#d94337")
.addField(
"User Perms",
`Users need the following permission(s) to run this command: ${
command.userPerms.length
? command.userPerms.map((x) => `\`${x}\``).join(" | ")
: `\`NONE\``
}`
)
.addField(
"Bot Perms",
`I need the following permission(s) to run this command: ${
command.botPerms.length
? command.botPerms.map((x) => `\`${x}\``).join(" | ")
: `\`NONE\``
}`
);
message.channel.send(perms).catch();
}
}
}
35 changes: 9 additions & 26 deletions src/lib/BaseManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ require("dotenv").config({
import { Client, Collection } from "discord.js";
import { Handler } from "./managers/Handler";
import { Utils } from "./utils/Utils";
import { connect } from "mongoose";
import { createConnection } from "typeorm";
import { Command } from "./structure/command";
import { readdirSync } from "fs";
import config from "../config.json";
import { GuildEntity } from "./models/GuildModel";
import { UserEntity } from "./models/UserModel";
import { MemberEntity } from "./models/MemberModel";

export class BaseManager extends Client {
private TOKEN: string;
Expand Down Expand Up @@ -37,9 +39,13 @@ export class BaseManager extends Client {
}

public loadMongo() {
connect(this.getConfig("mongo"), {
createConnection({
type: "mongodb",
url: this.config.get("mongo") as string,
useNewUrlParser: true,
useUnifiedTopology: true,
entities: [GuildEntity, UserEntity, MemberEntity],
synchronize: true,
})
.then(() => this.utils.log("[MongoDB] => Connected!"))
.catch((e) => {
Expand All @@ -50,27 +56,4 @@ export class BaseManager extends Client {
public getConfig(key: string) {
return this.config[this.type][key];
}

public loadCmd(cmd: string) {
const cats = readdirSync(__dirname + "/../bot/commands/").filter(
(f) => !f.endsWith(".js")
);

for (let cat of cats) {
const files = readdirSync(__dirname + `/../bot/commands/${cat}/`)
.filter((f) => f.endsWith(".js"))
.map((x) => x.split(".").shift());
if (!files.map((f) => f!.toLowerCase()).includes(cmd.toLowerCase()))
continue;

let { default: command } = require(__dirname +
`/../bot/commands/${cat}/${cmd}`);
if (!command) this.utils.log("No command file found!", "error");

command = new command();
command.bot = this;
this.commands.set(command.name, command);
return command;
}
}
}
5 changes: 4 additions & 1 deletion src/lib/managers/Bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ export class Bot extends BaseManager {
});
}

public botReload() {}
public botReload() {
this.destroy();
this.botStart();
}

public botStop() {
try {
Expand Down
1 change: 1 addition & 0 deletions src/lib/managers/Handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class Handler {
let { default: command } = require(`${dir}/${category}/${cmd}`);
command = new command();
command.bot = this.manager;
command.category = category;
this.manager.commands.set(command.name, command);
} catch (e) {
this.manager.utils.log(`[${cmd}] => ${e.message}`, "error");
Expand Down
26 changes: 19 additions & 7 deletions src/lib/models/GuildModel.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import { model, Schema } from "mongoose";
import {
Entity,
BaseEntity,
ObjectIdColumn,
PrimaryColumn,
ObjectID,
Column,
} from "typeorm";
import { Snowflake } from "discord.js";

const GuildSchema = new Schema({
id: String,
});
@Entity("guilds")
export class GuildEntity extends BaseEntity {
@ObjectIdColumn() public _id?: ObjectID;
@PrimaryColumn() public id: Snowflake;
@Column() public prefix: string = "!";

const GuildModel = model("guild", GuildSchema);

export { GuildModel };
constructor(id: Snowflake) {
super();
this.id = id;
}
}
28 changes: 20 additions & 8 deletions src/lib/models/MemberModel.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
import { model, Schema } from "mongoose";
import {
Entity,
BaseEntity,
ObjectIdColumn,
PrimaryColumn,
ObjectID,
Column,
} from "typeorm";
import { Snowflake } from "discord.js";

const MemberSchema = new Schema({
gid: String,
id: String,
});
@Entity("members")
export class MemberEntity extends BaseEntity {
@ObjectIdColumn() public _id?: ObjectID;
@PrimaryColumn() public id: Snowflake;
@PrimaryColumn() public guild: Snowflake;

const MemberModel = model("member", MemberSchema);

export { MemberModel };
constructor(id: Snowflake, gid: Snowflake) {
super();
this.id = id;
this.guild = gid;
}
}
25 changes: 18 additions & 7 deletions src/lib/models/UserModel.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import { model, Schema } from "mongoose";
import {
Entity,
BaseEntity,
ObjectIdColumn,
PrimaryColumn,
ObjectID,
Column,
} from "typeorm";
import { Snowflake } from "discord.js";

const UserSchema = new Schema({
id: String,
});
@Entity("users")
export class UserEntity extends BaseEntity {
@ObjectIdColumn() public _id?: ObjectID;
@PrimaryColumn() public id: Snowflake;

const UserModel = model("user", UserSchema);

export { UserModel };
constructor(id: Snowflake) {
super();
this.id = id;
}
}
21 changes: 8 additions & 13 deletions src/lib/structure/Guild.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
import { Structures } from "discord.js";
import { GuildModel } from "../models/GuildModel";
import { Document } from "mongoose";
import { GuildEntity } from "../models/GuildModel";

export default () => {
Structures.extend(
"Guild",
(Guild) =>
class extends Guild {
public db?: Document;
public db?: GuildEntity;

constructor() {
public constructor() {
super(arguments[0], arguments[1]);
(async () => await this._init())();
}

public _init() {
GuildModel.findOne({ id: this.id }, (err, doc) => {
if (err) throw err;
this.db =
doc ||
new GuildModel({
id: this.id,
});
});
public async _init() {
this.db =
(await GuildEntity.findOne({ id: this.id })) ||
new GuildEntity(this.id);
}
}
);
Expand Down
27 changes: 10 additions & 17 deletions src/lib/structure/GuildMember.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,24 @@
import { Structures } from "discord.js";
import { MemberModel } from "../models/MemberModel";
import { Document } from "mongoose";
import { MemberEntity } from "../models/MemberModel";

export default () => {
Structures.extend(
"GuildMember",
(GuildMember) =>
class extends GuildMember {
public db?: Document;
public db?: MemberEntity;

constructor() {
public constructor() {
super(arguments[0], arguments[1], arguments[2]);
(async () => await this._init())();
}

public _init() {
MemberModel.findOne(
{ id: this.id, gid: this.guild.id },
(err, doc) => {
if (err) throw err;
this.db =
doc ||
new MemberModel({
gid: this.guild.id,
id: this.id,
});
}
);
public async _init() {
this.db =
(await MemberEntity.findOne({
id: this.id,
guild: this.guild.id,
})) || new MemberEntity(this.id, this.guild.id);
}
}
);
Expand Down
4 changes: 4 additions & 0 deletions src/lib/structure/Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export default () => {
public em(content: string) {
return this.channel.send(new MessageEmbed().setDescription(content));
}

get embed() {
return new MessageEmbed();
}
}
);
};
22 changes: 9 additions & 13 deletions src/lib/structure/User.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
import { Structures } from "discord.js";
import { UserModel } from "../models/UserModel";
import { Document } from "mongoose";
import { UserEntity } from "../models/UserModel";

export default () => {
Structures.extend(
"User",
(User) =>
class extends User {
public db?: Document;
public db?: UserEntity;

constructor() {
public constructor() {
super(arguments[0], arguments[1]);
(async () => await this._init())();
}

public _init() {
UserModel.findOne({ id: this.id }, (err, doc) => {
if (err) throw err;
this.db =
doc ||
new UserModel({
id: this.id,
});
});
public async _init() {
this.db =
(await UserEntity.findOne({
id: this.id,
})) || new UserEntity(this.id);
}
}
);
Expand Down
Loading

0 comments on commit 21f710c

Please sign in to comment.