diff --git a/plugins/example-plugin/index.ts b/plugins/example-plugin/index.ts index 68b5b6a..1103a09 100644 --- a/plugins/example-plugin/index.ts +++ b/plugins/example-plugin/index.ts @@ -1,14 +1,17 @@ import {api, opendiscord, utilities} from "#opendiscord" import * as discord from "discord.js" +if (opendiscord.project != "openticket") throw new api.ODPluginError("This plugin only works in Open Ticket.") -///////////////////////////////////////////// -//// This plugin is not enabled yet! //// -//// Enable it in the plugin.json file! //// -///////////////////////////////////////////// +/** ============= Warning! ============= + * + * This plugin is not yet enabled and will not run by default. + * Please enable this plugin in the "plugin.json" file! + * + * ===================================== + */ -if (opendiscord.project != "openticket") throw new api.ODPluginError("This plugin only works in Open Ticket!") -//Add Typescript autocomplete support for plugin data. (!!!OPTIONAL!!!) +//Add optional Typescript type-safety. This improves the developer experience. declare module "#opendiscord-types" { export interface ODPluginManagerIdMappings { "example-plugin":api.ODPlugin @@ -18,29 +21,48 @@ declare module "#opendiscord-types" { } } -//Let's register the example config. This way it's available for all plugins & systems. -opendiscord.events.get("onConfigLoad").listen((configManager) => { - configManager.add(new api.ODJsonConfig("example-plugin:config","config.json","./plugins/example-plugin/")) - /*===== What did we do? ===== - - "example-plugin:config" Is the ID of this config. You can use this id troughout the bot to access this config file. Even in other plugins. - - "config.json" Is the FILE of this config. It is just the filename. - - "./plugins/example-plugin/" Is the DIRECTORY of this config. By default it's "./config/", but we want to change it to point at the plugin directory. +//REGISTER CONFIG +opendiscord.events.get("onConfigLoad").listen((configs) => { + //Let's register our example config. The plugin, all systems and even other plugins can access it. + configs.add(new api.ODJsonConfig("example-plugin:config","config.json","./plugins/example-plugin/")) + /* ===== What did we do? ===== + * "example-plugin:config" --> Is the ID of this config. It will be used throughout the code to access this config file. + * "config.json" --> Is the FILENAME of this config with extension. + * "./plugins/example-plugin/" --> Is the DIRECTORY of this config. It is set to "./config/" by default, but we want to change it to our plugin directory. */ +}) - //Let's also log it to the console to let us know it worked! - const ourConfig = configManager.get("example-plugin:config") +//CONFIG READY FOR USAGE +opendiscord.events.get("afterConfigsInitiated").listen((configs) => { + //After the "afterConfigsInitiated" event is fired, configs can be read without problems. + //Almost all events happen after this one, so you don't need to worry about it. + + const ourConfig = configs.get("example-plugin:config") opendiscord.log("The example config loaded successfully!","plugin",[ - {key:"var-1",value:ourConfig.data.testVariable1.toString()}, - {key:"var-2",value:ourConfig.data.testVariable2.toString()}, - {key:"var-3",value:ourConfig.data.testVariable3.toString()} + {key:"test-property-1",value:ourConfig.data.testVariable1.toString()}, + {key:"test-property-2",value:ourConfig.data.testVariable2.toString()}, + {key:"test-property-3",value:ourConfig.data.testVariable3.toString()} ]) }) +//LISTEN FOR TICKET EVENTS opendiscord.events.get("onTicketCreate").listen((creator) => { - //This is logged before the ticket is created (after the button is pressed) + //Do something before a ticket is created (just after the button press) opendiscord.log("Ticket is getting created...","plugin") }) opendiscord.events.get("afterTicketCreated").listen((ticket,creator,channel) => { - //This is logged after the ticket is created + //Do something after a ticket is created and ready for usage opendiscord.log("Ticket ready!","plugin") +}) +opendiscord.events.get("onTicketClose").listen((ticket,closer,channel,reason) => { + //Do something when a ticket will be closed + opendiscord.log("Ticket is getting closed...","plugin") +}) + +//MODIFY BUILT-IN OPEN TICKET MESSAGES +opendiscord.events.get("afterEmbedBuildersLoaded").listen((embeds) => { + //Let's modify the close message to include a URL in the embed. + embeds.get("opendiscord:close-message").workers.add(new api.ODWorker("example-plugin:testing",0,(instance,params,origin,cancel) => { + instance.setUrl("https://example.com") + })) }) \ No newline at end of file