v3.0 (#145)
* Change rules.txt to rules.json (#31) * Migrate to yarn * Add role configs to config template * Install packges and setup typescript * Migrate entry point * Migrate about command * Migrate ban command * Migrate clear command * Migrate kick command * Migrate mute command * Migrate poll command * Migrate bunny command * Update required roles checker * Migrate role command * Migrate unmute command * Migrate warn command * Migrate eval command * Migrate help command * Migrate rules command * Migrate events to typescript * Update about command to use the PublicEmbed class * Update ErrorMessage to ChannelNotFound * Update messageDelete event to ignore bots * Feature/74 merge vylbot core (#80) * Merge VylBot-Core * Update commands to new system * Fix issue where events would not load * Feature/12 create tests (#102) * Fix tests * Update coverage * Remove unrequired mock files * Add about command test * Update about tests * Ban command tests * eval command tests * Start help command tests * Add help command tests * Add kick command tests * Mute command tests * Poll command tests * Add role command tests Signed-off-by: Ethan Lane <ethan@vylpes.com> * Add rules command tests * Add unmute command tests * Add warn command tests * Add MemberEvents tests * Add GuildMemberUpdate tests Signed-off-by: Ethan Lane <ethan@vylpes.com> * Add MessageEvents tests * Add StringTools test Signed-off-by: Ethan Lane <ethan@vylpes.com> * Add embed tests Signed-off-by: Ethan Lane <ethan@vylpes.com> * Add GitHub Actions Signed-off-by: Ethan Lane <ethan@vylpes.com> * Move to tslint Signed-off-by: Ethan Lane <ethan@vylpes.com> * Remove tslint Signed-off-by: Ethan Lane <ethan@vylpes.com> * Remove linting script Signed-off-by: Ethan Lane <ethan@vylpes.com> * Update rules with blog website and event spoilers rule" (#106) Signed-off-by: Ethan Lane <ethan@vylpes.com> * Containerise bot (#107) * Add moderator names to audit reason (#108) * Feature/48 database (#114) * Add database and default values * Add ability to save a setting to the database * Get commands and events to use database * Setup and config command * Update commands to check roles per server * Different rules per server Signed-off-by: Ethan Lane <ethan@vylpes.com> * Different prefix per server Signed-off-by: Ethan Lane <ethan@vylpes.com> * Add verification system Signed-off-by: Ethan Lane <ethan@vylpes.com> * Disabled commands per server * Add devmode for default prefix * Update embeds * Fix broken tests * Feature/66 add different commands per server (#122) * Add ability for server exclusive commands * Add MankBot server-exclusive commands * Add lobby entity to database * Add documentation * Add setup command for lobby (#123) * Update bot to discord.js v13 (#125) * Update bot to discord.js v13 * Remove debug code * 110 commandshelp about command errors which causes command to not run (#126) * Change onMessage to onMessageCreate * Fix help command * Add override for bot owner and server owner (#135) * Change help command so exclusive commands can only be seen for the server they're assigned to (#136) * Change parsing to not crash if invalid (#142) * 137 role command cannot read properties of undefined (#141) * Fix issue with bot crashing * Fix server prefix not showing * Add easy way to configure role command * Move help text to its own directory * Make role config command to use role id * Get lobby command to use IDs instead of names (#144) Co-authored-by: Vylpes <getgravitysoftware@gmail.com>
This commit is contained in:
parent
1168898e57
commit
04a4a6204c
118 changed files with 13966 additions and 4027 deletions
79
src/client/client.ts
Normal file
79
src/client/client.ts
Normal file
|
@ -0,0 +1,79 @@
|
|||
import { Client } from "discord.js";
|
||||
import * as dotenv from "dotenv";
|
||||
import { createConnection } from "typeorm";
|
||||
import DefaultValues from "../constants/DefaultValues";
|
||||
import ICommandItem from "../contracts/ICommandItem";
|
||||
import IEventItem from "../contracts/IEventItem";
|
||||
import { Command } from "../type/command";
|
||||
import { Event } from "../type/event";
|
||||
|
||||
import { Events } from "./events";
|
||||
import { Util } from "./util";
|
||||
|
||||
export class CoreClient extends Client {
|
||||
private static _commandItems: ICommandItem[];
|
||||
private static _eventItems: IEventItem[];
|
||||
|
||||
private _events: Events;
|
||||
private _util: Util;
|
||||
|
||||
public static get commandItems(): ICommandItem[] {
|
||||
return this._commandItems;
|
||||
}
|
||||
|
||||
public static get eventItems(): IEventItem[] {
|
||||
return this._eventItems;
|
||||
}
|
||||
|
||||
constructor(intents: number[], devmode: boolean = false) {
|
||||
super({ intents: intents });
|
||||
dotenv.config();
|
||||
|
||||
DefaultValues.useDevPrefix = devmode;
|
||||
|
||||
CoreClient._commandItems = [];
|
||||
CoreClient._eventItems = [];
|
||||
|
||||
this._events = new Events();
|
||||
this._util = new Util();
|
||||
}
|
||||
|
||||
public async start() {
|
||||
if (!process.env.BOT_TOKEN) {
|
||||
console.error("BOT_TOKEN is not defined in .env");
|
||||
return;
|
||||
}
|
||||
|
||||
await createConnection().catch(e => {
|
||||
console.error(e);
|
||||
return;
|
||||
});
|
||||
|
||||
super.on("messageCreate", (message) => {
|
||||
this._events.onMessageCreate(message, CoreClient._commandItems)
|
||||
});
|
||||
super.on("ready", this._events.onReady);
|
||||
|
||||
super.login(process.env.BOT_TOKEN);
|
||||
|
||||
this._util.loadEvents(this, CoreClient._eventItems);
|
||||
}
|
||||
|
||||
public static RegisterCommand(name: string, command: Command, serverId?: string) {
|
||||
const item: ICommandItem = {
|
||||
Name: name,
|
||||
Command: command,
|
||||
ServerId: serverId,
|
||||
};
|
||||
|
||||
CoreClient._commandItems.push(item);
|
||||
}
|
||||
|
||||
public static RegisterEvent(event: Event) {
|
||||
const item: IEventItem = {
|
||||
Event: event,
|
||||
};
|
||||
|
||||
CoreClient._eventItems.push(item);
|
||||
}
|
||||
}
|
37
src/client/events.ts
Normal file
37
src/client/events.ts
Normal file
|
@ -0,0 +1,37 @@
|
|||
import { Message } from "discord.js";
|
||||
import ICommandItem from "../contracts/ICommandItem";
|
||||
import SettingsHelper from "../helpers/SettingsHelper";
|
||||
import { Util } from "./util";
|
||||
|
||||
export class Events {
|
||||
private _util: Util;
|
||||
|
||||
constructor() {
|
||||
this._util = new Util();
|
||||
}
|
||||
|
||||
// Emit when a message is sent
|
||||
// Used to check for commands
|
||||
public async onMessageCreate(message: Message, commands: ICommandItem[]) {
|
||||
if (!message.guild) return;
|
||||
if (message.author.bot) return;
|
||||
|
||||
const prefix = await SettingsHelper.GetSetting("bot.prefix", message.guild.id);
|
||||
|
||||
if (!prefix) return;
|
||||
|
||||
if (message.content.substring(0, prefix.length).toLowerCase() == prefix.toLowerCase()) {
|
||||
const args = message.content.substring(prefix.length).split(" ");
|
||||
const name = args.shift();
|
||||
|
||||
if (!name) return;
|
||||
|
||||
await this._util.loadCommand(name, args, message, commands);
|
||||
}
|
||||
}
|
||||
|
||||
// Emit when bot is logged in and ready to use
|
||||
public onReady() {
|
||||
console.log("Ready");
|
||||
}
|
||||
}
|
101
src/client/util.ts
Normal file
101
src/client/util.ts
Normal file
|
@ -0,0 +1,101 @@
|
|||
// Required Components
|
||||
import { Client, Message } from "discord.js";
|
||||
import { ICommandContext } from "../contracts/ICommandContext";
|
||||
import ICommandItem from "../contracts/ICommandItem";
|
||||
import IEventItem from "../contracts/IEventItem";
|
||||
import SettingsHelper from "../helpers/SettingsHelper";
|
||||
import StringTools from "../helpers/StringTools";
|
||||
import { CommandResponse } from "../constants/CommandResponse";
|
||||
import ErrorMessages from "../constants/ErrorMessages";
|
||||
|
||||
// Util Class
|
||||
export class Util {
|
||||
public async loadCommand(name: string, args: string[], message: Message, commands: ICommandItem[]) {
|
||||
if (!message.member) return;
|
||||
if (!message.guild) return;
|
||||
|
||||
const disabledCommandsString = await SettingsHelper.GetSetting("commands.disabled", message.guild?.id);
|
||||
const disabledCommands = disabledCommandsString?.split(",");
|
||||
|
||||
if (disabledCommands?.find(x => x == name)) {
|
||||
message.reply(process.env.COMMANDS_DISABLED_MESSAGE || "This command is disabled.");
|
||||
return;
|
||||
}
|
||||
|
||||
const item = commands.find(x => x.Name == name && !x.ServerId);
|
||||
const itemForServer = commands.find(x => x.Name == name && x.ServerId == message.guild?.id);
|
||||
|
||||
let itemToUse: ICommandItem;
|
||||
|
||||
if (!itemForServer) {
|
||||
if (!item) {
|
||||
message.reply('Command not found');
|
||||
return;
|
||||
}
|
||||
|
||||
itemToUse = item;
|
||||
} else {
|
||||
itemToUse = itemForServer;
|
||||
}
|
||||
|
||||
const requiredRoles = itemToUse.Command.Roles;
|
||||
|
||||
if (message.author.id != process.env.BOT_OWNERID && message.author.id != message.guild.ownerId) {
|
||||
for (const i in requiredRoles) {
|
||||
if (message.guild) {
|
||||
const setting = await SettingsHelper.GetSetting(`role.${requiredRoles[i]}`, message.guild?.id);
|
||||
|
||||
if (!setting) {
|
||||
message.reply("Unable to verify if you have this role, please contact your bot administrator");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!message.member.roles.cache.find(role => role.name == setting)) {
|
||||
message.reply(`You require the \`${StringTools.Capitalise(setting)}\` role to run this command`);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const context: ICommandContext = {
|
||||
name: name,
|
||||
args: args,
|
||||
message: message
|
||||
};
|
||||
|
||||
const precheckResponse = itemToUse.Command.precheck(context);
|
||||
const precheckAsyncResponse = await itemToUse.Command.precheckAsync(context);
|
||||
|
||||
if (precheckResponse != CommandResponse.Ok) {
|
||||
message.reply(ErrorMessages.GetErrorMessage(precheckResponse));
|
||||
return;
|
||||
}
|
||||
|
||||
if (precheckAsyncResponse != CommandResponse.Ok) {
|
||||
message.reply(ErrorMessages.GetErrorMessage(precheckAsyncResponse));
|
||||
return;
|
||||
}
|
||||
|
||||
itemToUse.Command.execute(context);
|
||||
}
|
||||
|
||||
// Load the events
|
||||
loadEvents(client: Client, events: IEventItem[]) {
|
||||
events.forEach((e) => {
|
||||
client.on('channelCreate', e.Event.channelCreate);
|
||||
client.on('channelDelete', e.Event.channelDelete);
|
||||
client.on('channelUpdate', e.Event.channelUpdate);
|
||||
client.on('guildBanAdd', e.Event.guildBanAdd);
|
||||
client.on('guildBanRemove', e.Event.guildBanRemove);
|
||||
client.on('guildCreate', e.Event.guildCreate);
|
||||
client.on('guildMemberAdd', e.Event.guildMemberAdd);
|
||||
client.on('guildMemberRemove', e.Event.guildMemberRemove);
|
||||
client.on('guildMemberUpdate', e.Event.guildMemberUpdate);
|
||||
client.on('messageCreate', e.Event.messageCreate);
|
||||
client.on('messageDelete', e.Event.messageDelete);
|
||||
client.on('messageUpdate', e.Event.messageUpdate);
|
||||
client.on('ready', e.Event.ready);
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue