Merge branch 'DJj123dj:dev' into dev
This commit is contained in:
@@ -143,6 +143,14 @@ export class ODLanguageManager extends ODManager<ODLanguage> {
|
||||
}
|
||||
}
|
||||
|
||||
/**## ODLanguage `class`
|
||||
* This is an open ticket language file.
|
||||
*
|
||||
* It contains metadata and all translation strings available in this language.
|
||||
* Register this class to an `ODLanguageManager` to use it!
|
||||
*
|
||||
* JSON languages should be created using the `ODJsonLanguage` class instead!
|
||||
*/
|
||||
export class ODLanguage extends ODManagerData {
|
||||
/**The name of the file with extension. */
|
||||
file: string = ""
|
||||
@@ -164,6 +172,14 @@ export class ODLanguage extends ODManagerData {
|
||||
}
|
||||
}
|
||||
|
||||
/**## ODJsonLanguage `class`
|
||||
* This is an open ticket JSON language file.
|
||||
*
|
||||
* It contains metadata and all translation strings from a certain JSON file (in `./languages/`).
|
||||
* Register this class to an `ODLanguageManager` to use it!
|
||||
*
|
||||
* Use the `ODLanguage` class to use translations from non-JSON files!
|
||||
*/
|
||||
export class ODJsonLanguage extends ODLanguage {
|
||||
constructor(id:ODValidId, file:string, customPath?:string){
|
||||
super(id,{})
|
||||
|
||||
@@ -5,28 +5,65 @@ import { ODId, ODValidId, ODManager, ODSystemError, ODManagerData } from "./base
|
||||
import * as discord from "discord.js"
|
||||
import { ODDebugger } from "./console"
|
||||
|
||||
/**## ODPermissionType `type`
|
||||
* All available permission types/levels. Can be used in the `ODPermission` class.
|
||||
*/
|
||||
export type ODPermissionType = "member"|"support"|"moderator"|"admin"|"owner"|"developer"
|
||||
|
||||
/**## ODPermissionScope `type`
|
||||
* The scope in which a certain permission is active.
|
||||
*/
|
||||
export type ODPermissionScope = "global-user"|"channel-user"|"global-role"|"channel-role"
|
||||
|
||||
/**## ODPermissionResult `interface`
|
||||
* The result returned by `ODPermissionManager.getPermissions()`.
|
||||
*/
|
||||
export interface ODPermissionResult {
|
||||
/**The permission type. */
|
||||
type:ODPermissionType
|
||||
/**The permission scope. */
|
||||
scope:ODPermissionScope|"default"
|
||||
/**The highest level available for this scope. */
|
||||
level:ODPermissionLevel,
|
||||
/**The permission which returned this level. */
|
||||
source:ODPermission|null
|
||||
}
|
||||
|
||||
/**## ODPermissionLevel `enum`
|
||||
* All available permission types/levels. But as `enum` instead of `type`. Used to calculate the level.
|
||||
*/
|
||||
export enum ODPermissionLevel {
|
||||
/**A normal member. (Default for everyone) */
|
||||
member,
|
||||
/**Support team. Higher than a normal member. (Used for ticket-admins) */
|
||||
support,
|
||||
/**Moderator. Higher than the support team. (Unused) */
|
||||
moderator,
|
||||
/**Admin. Higher than a moderator. (Used for global-admins) */
|
||||
admin,
|
||||
/**Server owner. (Able to use all commands including `/stats reset`) */
|
||||
owner,
|
||||
/**Bot owner or all users from dev team. (Able to use all commands including `/stats reset`) */
|
||||
developer
|
||||
}
|
||||
|
||||
/**## ODPermission `class`
|
||||
* This is an open ticket permission.
|
||||
*
|
||||
* It defines a single permission level for a specific scope (global/channel & user/role)
|
||||
* These permissions only apply to commands & interactions.
|
||||
* They are not related to channel permissions in the ticket system.
|
||||
*
|
||||
* Register this class to an `ODPermissionManager` to use it!
|
||||
*/
|
||||
export class ODPermission extends ODManagerData {
|
||||
/**The scope of this permission. */
|
||||
readonly scope: ODPermissionScope
|
||||
/**The type/level of this permission. */
|
||||
readonly permission: ODPermissionType
|
||||
/**The user/role of this permission. */
|
||||
readonly value: discord.Role|discord.User
|
||||
/**The channel that this permission applies to. (`null` when global) */
|
||||
readonly channel: discord.Channel|null
|
||||
|
||||
constructor(id:ODValidId, scope:"global-user", permission:ODPermissionType, value:discord.User)
|
||||
@@ -42,18 +79,39 @@ export class ODPermission extends ODManagerData {
|
||||
}
|
||||
}
|
||||
|
||||
/**## ODPermissionSettings `interface`
|
||||
* Optional settings for the `getPermissions()` method in the `ODPermissionManager`.
|
||||
*/
|
||||
export interface ODPermissionSettings {
|
||||
/**Include permissions from the global user scope. */
|
||||
allowGlobalUserScope?:boolean,
|
||||
/**Include permissions from the global role scope. */
|
||||
allowGlobalRoleScope?:boolean,
|
||||
/**Include permissions from the channel user scope. */
|
||||
allowChannelUserScope?:boolean,
|
||||
/**Include permissions from the channel role scope. */
|
||||
allowChannelRoleScope?:boolean,
|
||||
/**Only include permissions of which the id matches this regex. */
|
||||
idRegex?:RegExp
|
||||
}
|
||||
|
||||
/**## ODPermissionCalculationCallback `type`
|
||||
* The callback of the permission calculation. (Used in `ODPermissionManager`)
|
||||
*/
|
||||
export type ODPermissionCalculationCallback = (user:discord.User, channel?:discord.Channel|null, guild?:discord.Guild|null, settings?:ODPermissionSettings|null) => Promise<ODPermissionResult>
|
||||
|
||||
/**## ODPermissionManager `class`
|
||||
* This is an open ticket permission manager.
|
||||
*
|
||||
* It manages all permissions in the bot!
|
||||
* Use the `getPermissions()` and `hasPermissions()` methods to get user perms.
|
||||
*
|
||||
* Add new permissions using the `ODPermission` class in your plugin!
|
||||
*/
|
||||
export class ODPermissionManager extends ODManager<ODPermission> {
|
||||
/**The function for calculating permissions in this manager. */
|
||||
#calculation: ODPermissionCalculationCallback|null
|
||||
/**The result which is returned when no other permissions match. (`member` by default) */
|
||||
defaultResult: ODPermissionResult = {
|
||||
level:ODPermissionLevel["member"],
|
||||
scope:"default",
|
||||
@@ -66,12 +124,15 @@ export class ODPermissionManager extends ODManager<ODPermission> {
|
||||
this.#calculation = useDefaultCalculation ? this.#defaultCalculation : null
|
||||
}
|
||||
|
||||
/**Edit the permission calculation function in this manager. */
|
||||
setCalculation(calculation:ODPermissionCalculationCallback){
|
||||
this.#calculation = calculation
|
||||
}
|
||||
/**Edit the result which is returned when no other permissions match. (`member` by default) */
|
||||
setDefaultResult(result:ODPermissionResult){
|
||||
this.defaultResult = result
|
||||
}
|
||||
/**Get an `ODPermissionResult` based on a few context factors. Use `hasPermissions()` to simplify the result. */
|
||||
getPermissions(user:discord.User, channel?:discord.Channel|null, guild?:discord.Guild|null, settings?:ODPermissionSettings|null): Promise<ODPermissionResult> {
|
||||
try{
|
||||
if (!this.#calculation) throw new ODSystemError("ODPermissionManager:getPermissions() => missing perms calculation")
|
||||
@@ -81,6 +142,7 @@ export class ODPermissionManager extends ODManager<ODPermission> {
|
||||
throw new ODSystemError("ODPermissionManager:getPermissions() => failed perms calculation")
|
||||
}
|
||||
}
|
||||
/**Simplifies the `ODPermissionResult` returned from `getPermissions()` and returns a boolean to check if the user matches the required permissions. */
|
||||
hasPermissions(minimum:ODPermissionType, data:ODPermissionResult){
|
||||
if (minimum == "member") return true
|
||||
else if (minimum == "support") return (data.level >= ODPermissionLevel["support"])
|
||||
@@ -90,6 +152,7 @@ export class ODPermissionManager extends ODManager<ODPermission> {
|
||||
else if (minimum == "developer") return (data.level >= ODPermissionLevel["developer"])
|
||||
else throw new ODSystemError("Invalid minimum permission type at ODPermissionManager.hasPermissions()")
|
||||
}
|
||||
/**Check for permissions. (default calculation) */
|
||||
async #defaultCalculation(user:discord.User,channel?:discord.Channel|null,guild?:discord.Guild|null, settings?:ODPermissionSettings|null): Promise<ODPermissionResult> {
|
||||
const globalCalc = await this.#defaultGlobalCalculation(user,channel,guild,settings)
|
||||
const channelCalc = await this.#defaultChannelCalculation(user,channel,guild,settings)
|
||||
@@ -97,7 +160,7 @@ export class ODPermissionManager extends ODManager<ODPermission> {
|
||||
if (globalCalc.level > channelCalc.level) return globalCalc
|
||||
else return channelCalc
|
||||
}
|
||||
/**Check for global permissions. Then this result can be compared with the channel one. */
|
||||
/**Check for global permissions. Result will be compared with the channel perms in `#defaultCalculation()`. */
|
||||
async #defaultGlobalCalculation(user:discord.User,channel?:discord.Channel|null,guild?:discord.Guild|null, settings?:ODPermissionSettings|null): Promise<ODPermissionResult> {
|
||||
const idRegex = (settings && typeof settings.idRegex != "undefined") ? settings.idRegex : null
|
||||
const allowGlobalUserScope = (settings && typeof settings.allowGlobalUserScope != "undefined") ? settings.allowGlobalUserScope : true
|
||||
@@ -160,7 +223,7 @@ export class ODPermissionManager extends ODManager<ODPermission> {
|
||||
//spread result to prevent accidental referencing
|
||||
return {...this.defaultResult}
|
||||
}
|
||||
/**Check for channel permissions. Then this result can be compared with the global one. */
|
||||
/**Check for channel permissions. Result will be compared with the global perms in `#defaultCalculation()`. */
|
||||
async #defaultChannelCalculation(user:discord.User,channel?:discord.Channel|null,guild?:discord.Guild|null, settings?:ODPermissionSettings|null): Promise<ODPermissionResult> {
|
||||
const idRegex = (settings && typeof settings.idRegex != "undefined") ? settings.idRegex : null
|
||||
const allowChannelUserScope = (settings && typeof settings.allowChannelUserScope != "undefined") ? settings.allowChannelUserScope : true
|
||||
@@ -220,7 +283,7 @@ export class ODPermissionManager extends ODManager<ODPermission> {
|
||||
}
|
||||
}
|
||||
|
||||
//spread result to prevent accidental referencing
|
||||
//spread result to prevent accidental modification because of referencing
|
||||
return {...this.defaultResult}
|
||||
}
|
||||
}
|
||||
@@ -5,13 +5,29 @@ import { ODId, ODManager, ODManagerData, ODSystemError, ODValidId, ODVersion } f
|
||||
import nodepath from "path"
|
||||
import { ODConsolePluginMessage, ODConsoleWarningMessage, ODDebugger } from "./console"
|
||||
|
||||
/**## ODUnknownCrashedPlugin `interface`
|
||||
* Basic details for a plugin that crashed while loading the `plugin.json` file.
|
||||
*/
|
||||
export interface ODUnknownCrashedPlugin {
|
||||
/**The name of the plugin. (path when plugin crashed before `name` was loaded) */
|
||||
name:string,
|
||||
/**The description of the plugin. (when found before crashing) */
|
||||
description:string
|
||||
}
|
||||
|
||||
/**## ODPluginManager `class`
|
||||
* This is an open ticket plugin manager.
|
||||
*
|
||||
* It manages all active plugins in the bot!
|
||||
* It also contains all "plugin classes" which are managers registered by plugins.
|
||||
* These are accessible via the `openticket.plugins.classes` global.
|
||||
*
|
||||
* Use `isPluginLoaded()` to check if a plugin has been loaded.
|
||||
*/
|
||||
export class ODPluginManager extends ODManager<ODPlugin> {
|
||||
/**A manager for all custom managers registered by plugins. */
|
||||
classes: ODPluginClassManager
|
||||
/**A list of basic details from all plugins that crashed while loading the `plugin.json` file. */
|
||||
unknownCrashedPlugins: ODUnknownCrashedPlugin[] = []
|
||||
|
||||
constructor(debug:ODDebugger){
|
||||
@@ -19,7 +35,7 @@ export class ODPluginManager extends ODManager<ODPlugin> {
|
||||
this.classes = new ODPluginClassManager(debug)
|
||||
}
|
||||
|
||||
/**Check if a plugin has loaded successfully.*/
|
||||
/**Check if a plugin has been loaded successfully and is available for usage.*/
|
||||
isPluginLoaded(id:ODValidId): boolean {
|
||||
const newId = new ODId(id)
|
||||
const plugin = this.get(newId)
|
||||
@@ -27,43 +43,84 @@ export class ODPluginManager extends ODManager<ODPlugin> {
|
||||
}
|
||||
}
|
||||
|
||||
/**## ODPluginData `interface`
|
||||
* Parsed data from the `plugin.json` file in a plugin.
|
||||
*/
|
||||
export interface ODPluginData {
|
||||
/**The name of this plugin (shown on startup) */
|
||||
name:string,
|
||||
/**The id of this plugin. (Must be identical to directory name) */
|
||||
id:string,
|
||||
/**The version of this plugin. */
|
||||
version:string,
|
||||
/**The location of the start file of the plugin relative to the rootDir of the plugin */
|
||||
startFile:string,
|
||||
|
||||
/**Is this plugin enabled? */
|
||||
enabled:boolean,
|
||||
/**The priority of this plugin. Higher priority will load before lower priority. */
|
||||
priority:number,
|
||||
/**A list of events to register to the `openticket.events` global before loading any plugins. This way, plugins with a higher priority are able to use events from this plugin as well! */
|
||||
events:string[]
|
||||
|
||||
/**Npm dependencies which are required for this plugin to work. */
|
||||
npmDependencies:string[],
|
||||
/**Plugins which are required for this plugin to work. */
|
||||
requiredPlugins:string[],
|
||||
/**Plugins which are incompatible with this plugin. */
|
||||
incompatiblePlugins:string[],
|
||||
|
||||
/**Additional details about this plugin. */
|
||||
details:ODPluginDetails
|
||||
}
|
||||
|
||||
/**## ODPluginDetails `interface`
|
||||
* Additional details in the `plugin.json` file from a plugin.
|
||||
*/
|
||||
export interface ODPluginDetails {
|
||||
/**The author of the plugin. */
|
||||
author:string,
|
||||
/**A short description of this plugin. */
|
||||
shortDescription:string,
|
||||
/**A large description of this plugin. */
|
||||
longDescription:string,
|
||||
/**A URL to a cover image of this plugin. (currently unused) */
|
||||
imageUrl:string,
|
||||
/**A URL to the website/project page of this plugin. (currently unused) */
|
||||
projectUrl:string,
|
||||
/**A list of tags/categories that this plugin affects. */
|
||||
tags:string[]
|
||||
}
|
||||
|
||||
/**## ODPlugin `class`
|
||||
* This is an open ticket plugin.
|
||||
*
|
||||
* It represents a single plugin in the `./plugins/` directory.
|
||||
* All plugins are accessible via the `openticket.plugins` global.
|
||||
*
|
||||
* Don't re-execute plugins which are already enabled! It might break the bot or plugin.
|
||||
*/
|
||||
export class ODPlugin extends ODManagerData {
|
||||
/**The name of the directory of this plugin. (same as id) */
|
||||
dir: string
|
||||
/**All plugin data found in the `plugin.json` file. */
|
||||
data: ODPluginData
|
||||
/**The name of this plugin. */
|
||||
name: string
|
||||
/**The priority of this plugin. */
|
||||
priority: number
|
||||
/**The version of this plugin. */
|
||||
version: ODVersion
|
||||
/**The additional details of this plugin. */
|
||||
details: ODPluginDetails
|
||||
|
||||
/**Is this plugin enabled? */
|
||||
enabled: boolean
|
||||
/**Did this plugin execute successfully?. */
|
||||
executed: boolean
|
||||
/**Did this plugin crash? (A reason is available in the `crashReason`) */
|
||||
crashed: boolean
|
||||
/**The reason which caused this plugin to crash. */
|
||||
crashReason: null|"incompatible.plugin"|"missing.plugin"|"missing.dependency"|"executed" = null
|
||||
|
||||
constructor(dir:string, jsondata:ODPluginData){
|
||||
@@ -160,6 +217,16 @@ export class ODPlugin extends ODManagerData {
|
||||
}
|
||||
}
|
||||
|
||||
/**## ODPluginClassManager `class`
|
||||
* This is an open ticket plugin class manager.
|
||||
*
|
||||
* It manages all managers registered by plugins!
|
||||
* Plugins are able to register their own managers, handlers, functions, classes, ... here.
|
||||
* By doing this, other plugins are also able to make use of it.
|
||||
* This can be useful for plugins that want to extend other plugins.
|
||||
*
|
||||
* Use `isPluginLoaded()` to check if a plugin has been loaded before trying to access the manager.
|
||||
*/
|
||||
export class ODPluginClassManager extends ODManager<ODManagerData> {
|
||||
constructor(debug:ODDebugger){
|
||||
super(debug,"plugin class")
|
||||
|
||||
@@ -8,12 +8,21 @@ import { ODButtonResponderInstance } from "./responder"
|
||||
import * as discord from "discord.js"
|
||||
import { ODWorkerManager } from "./worker"
|
||||
|
||||
export type ODVerifyBarCallback = (responder:ODButtonResponderInstance,customData?:string) => void|Promise<void>
|
||||
|
||||
/**## ODVerifyBar `class`
|
||||
* This is an open ticket verifybar.
|
||||
*
|
||||
* It is contains 2 sets of workers and a lot of utilities for the (✅ ❌) verifybars in the bot.
|
||||
*
|
||||
* It doesn't contain the code which activates or spawns the verifybars!
|
||||
*/
|
||||
export class ODVerifyBar extends ODManagerData {
|
||||
/**All workers that will run when the verifybar is accepted. */
|
||||
success: ODWorkerManager<ODButtonResponderInstance,"verifybar",{data:string|null,verifybarMessage:discord.Message<boolean>|null}>
|
||||
/**All workers that will run when the verifybar is stopped. */
|
||||
failure: ODWorkerManager<ODButtonResponderInstance,"verifybar",{data:string|null,verifybarMessage:discord.Message<boolean>|null}>
|
||||
/**The message that will be built wen activating this verifybar. */
|
||||
message: ODMessage<"verifybar",{guild:discord.Guild|null,channel:discord.TextBasedChannel,user:discord.User,verifybar:ODVerifyBar,originalMessage:discord.Message<boolean>}>
|
||||
/**When disabled, it will skip the verifybar and instantly fire the `success` workers. */
|
||||
enabled: boolean
|
||||
|
||||
constructor(id:ODValidId, message:ODMessage<"verifybar",{guild:discord.Guild|null,channel:discord.TextBasedChannel,user:discord.User,originalMessage:discord.Message<boolean>}>, enabled?:boolean){
|
||||
@@ -24,6 +33,7 @@ export class ODVerifyBar extends ODManagerData {
|
||||
this.enabled = enabled ?? true
|
||||
}
|
||||
|
||||
/**Build the message and reply to a button with this verifybar. */
|
||||
async activate(responder:ODButtonResponderInstance){
|
||||
if (this.enabled){
|
||||
//show verifybar
|
||||
@@ -36,6 +46,14 @@ export class ODVerifyBar extends ODManagerData {
|
||||
}
|
||||
}
|
||||
|
||||
/**## ODVerifyBarManager `class`
|
||||
* This is an open ticket verifybar manager.
|
||||
*
|
||||
* It contains all (✅ ❌) verifybars in the bot.
|
||||
* The `ODVerifyBar` classes contain `ODWorkerManager`'s that will be fired when the continue/stop buttons are pressed.
|
||||
*
|
||||
* It doesn't contain the code which activates the verifybars! This should be implemented by your own.
|
||||
*/
|
||||
export class ODVerifyBarManager extends ODManager<ODVerifyBar> {
|
||||
constructor(debug:ODDebugger){
|
||||
super(debug,"verifybar")
|
||||
|
||||
Reference in New Issue
Block a user