@kingsworld/plugin-cron 1.0.3 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +122 -0
- package/package.json +1 -1
package/README.md
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
# @kingsworld/plugin-cron
|
2
|
+
|
3
|
+
A simple [@sapphire/framework](https://www.npmjs.com/package/@sapphire/framework) plugin that aims to make use of the [cron](https://www.npmjs.com/package/cron) package and allow users to make cron jobs within their Sapphire discord bot.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
```sh
|
8
|
+
yarn add @kingsworld/plugin-cron @sapphire/framework
|
9
|
+
```
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
Make sure to register the plegin before creating the client
|
14
|
+
|
15
|
+
```ts
|
16
|
+
import "@kingsworld/plugin-cron/register";
|
17
|
+
```
|
18
|
+
|
19
|
+
If you would like to set the default cron job timezone for all your cron jobs, you can do so within the client options. A list of TZ identifers can be found on [Wikipedia](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones).
|
20
|
+
|
21
|
+
```ts
|
22
|
+
new SapphireClient({
|
23
|
+
...otherClientOptions,
|
24
|
+
cron: {
|
25
|
+
// the cron object is optional
|
26
|
+
defaultTimezone: "Europe/London", // the cron pacakge defaults to UTC
|
27
|
+
},
|
28
|
+
});
|
29
|
+
```
|
30
|
+
|
31
|
+
However, if you would like to do so for a single task, you can do so in the cron task options
|
32
|
+
|
33
|
+
```js
|
34
|
+
{
|
35
|
+
cronTime: "* * * * *", // every minute
|
36
|
+
timeZone: "Europe/London"
|
37
|
+
}
|
38
|
+
```
|
39
|
+
|
40
|
+
### Creating a task
|
41
|
+
|
42
|
+
Cron Tasks come with their own store, just like Sapphire commands, listeners, and so on. They must be located within a `cron-tasks` directory that is located alongside your other stores.
|
43
|
+
|
44
|
+
```
|
45
|
+
src
|
46
|
+
├── listeners
|
47
|
+
| └── ready.ts
|
48
|
+
└── cron-tasks
|
49
|
+
└── ping.ts
|
50
|
+
```
|
51
|
+
|
52
|
+
Using decorators from [@sapphire/decorators](https://www.npmjs.com/package/@sapphire/decorators):
|
53
|
+
|
54
|
+
```ts
|
55
|
+
import { ApplyOptions } from "@sapphire/decorators";
|
56
|
+
import { CronTask } from "@kingsworld/plugin-cron";
|
57
|
+
|
58
|
+
@ApplyOptions<CronTask.Options>({
|
59
|
+
cronTime: "* * * * *",
|
60
|
+
})
|
61
|
+
export class PingPong extends CronTask {
|
62
|
+
run() {
|
63
|
+
this.container.logger.info("Ping Pong! 🏓");
|
64
|
+
}
|
65
|
+
}
|
66
|
+
```
|
67
|
+
|
68
|
+
Using the command constructor:
|
69
|
+
|
70
|
+
```ts
|
71
|
+
import { CronTask } from "@kingsworld/plugin-cron";
|
72
|
+
|
73
|
+
export class PingPong extends CronTask {
|
74
|
+
constructor(context: CronTask.Context, options: CronTask.Options) {
|
75
|
+
super(context, {
|
76
|
+
...options,
|
77
|
+
cronTime: "* * * * *",
|
78
|
+
});
|
79
|
+
}
|
80
|
+
|
81
|
+
run() {
|
82
|
+
this.container.logger.info("Ping Pong! 🏓");
|
83
|
+
}
|
84
|
+
}
|
85
|
+
```
|
86
|
+
|
87
|
+
### Frequently Asked Questions
|
88
|
+
|
89
|
+
##### What does the `this.info()`, `this.error()`, `this.warn()`, `this.debug()`, and `this.trace()` methods do instead the CronTask#run() method?
|
90
|
+
|
91
|
+
These methods are small helpers towards Sapphire's logger that prefixes logs with `CronTask[$name]`. The helpers are optional, however, I find them useful when using them in my own projects.
|
92
|
+
|
93
|
+
```ts
|
94
|
+
export class PingPong extends CronTask {
|
95
|
+
run() {
|
96
|
+
this.info("Ping Pong! 🏓"); // YYYY-MM-DD HH:MM:SS - INFO - CronTask[ping] Ping Pong! 🏓
|
97
|
+
this.error("Ping Pong! 🏓"); // YYYY-MM-DD HH:MM:SS - ERROR - CronTask[ping] Ping Pong! 🏓
|
98
|
+
this.warn("Ping Pong! 🏓"); // YYYY-MM-DD HH:MM:SS - WARN - CronTask[ping] Ping Pong! 🏓
|
99
|
+
this.debug("Ping Pong! 🏓"); // YYYY-MM-DD HH:MM:SS - DEBUG - CronTask[ping] Ping Pong! 🏓
|
100
|
+
this.trace("Ping Pong! 🏓"); // YYYY-MM-DD HH:MM:SS - TRACE - CronTask[ping] Ping Pong! 🏓
|
101
|
+
}
|
102
|
+
}
|
103
|
+
```
|
104
|
+
|
105
|
+
### Contributing
|
106
|
+
|
107
|
+
#### Getting started
|
108
|
+
|
109
|
+
First, clone the repo using git SSH, the GitHub CLI, or HTTP
|
110
|
+
|
111
|
+
```sh
|
112
|
+
git clone git@github.com:Kings-World/sapphire-plugins.git # ssh
|
113
|
+
git clone https://github.com/Kings-World/sapphire-plugins.git # http
|
114
|
+
gh repo clone Kings-World/sapphire-plugins # github cli
|
115
|
+
```
|
116
|
+
|
117
|
+
Finally, install the dependencies using Yarn
|
118
|
+
|
119
|
+
```sh
|
120
|
+
yarn # shorthand
|
121
|
+
yarn install # full command
|
122
|
+
```
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@kingsworld/plugin-cron",
|
3
3
|
"description": "A simple @sapphire/framework plugin that aims to make use of the cron package and allow users to make cron jobs within their Sapphire discord bot.",
|
4
|
-
"version": "1.0.
|
4
|
+
"version": "1.0.5",
|
5
5
|
"author": "Seren_Modz 21 <seren@kings-world.net>",
|
6
6
|
"license": "MIT",
|
7
7
|
"main": "dist/index.js",
|