Skip to content

Commit fe6d8b1

Browse files
committed
improved sinusbot configuration
1 parent e9214c5 commit fe6d8b1

File tree

3 files changed

+92
-59
lines changed

3 files changed

+92
-59
lines changed

example/example.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,39 @@
1-
//reference the global definition file by default its ./node_modules/sinusbot-scripting-engine/typings/global.d.ts
1+
//reference the global definition file by default its sinusbot/typings/global.d.ts
22
///<reference path="../typings/global.d.ts" />
33
import { Client } from "@sinusbot/Client"
44

5-
registerPlugin({
5+
registerPlugin<{
6+
//here the config interface will be declared
7+
foo: { bar: string, baz: number }[]
8+
}>({
69
name: "typescript",
710
version: "1.0.0",
811
description: "typescript description",
9-
author: "Multivitamin <david.kartnaller@gmail.com>"
10-
}, () => {
12+
author: "Multivitamin <david.kartnaller@gmail.com>",
13+
vars: [{
14+
type: "array" as const, //the as const will be needed otherwise typescript dislikes it
15+
name: "foo",
16+
title: "foo config",
17+
default: [],
18+
vars: [{
19+
type: "string" as const,
20+
name: "bar",
21+
title: "bar config",
22+
default: ""
23+
}, {
24+
type: "string" as const,
25+
name: "baz",
26+
title: "baz config",
27+
default: ""
28+
}]
29+
}]
30+
}, (_, { foo }) => {
1131

1232
const engine = require("engine")
1333
const backend = require("backend")
1434
const event = require("event")
1535

36+
console.log({ foo })
1637

1738
event.on("load", () => {
1839
let botClient: Client|null = null

typings/global.d.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { crypto } from "./modules/crypto"
1717

1818
import { command } from "./external/command"
1919

20-
import { SinusbotMeta } from "./meta"
20+
import type { SinusbotMeta } from "./meta"
2121

2222
export interface Module {
2323
exports: any
@@ -39,11 +39,13 @@ declare global {
3939
* @param meta basic script informations
4040
* @param callback script environment
4141
*/
42-
function registerPlugin<T extends SinusbotMeta>(
43-
meta: T,
44-
callback: (sinusbot: null, config: Record<string, any>, meta: T) => void
42+
function registerPlugin<T>(
43+
meta: SinusbotMeta,
44+
callback: (sinusbot: null, config: T, meta: SinusbotMeta) => void
4545
): void
4646

47+
/** enumerations */
48+
4749
function clearInterval(interval: number): void
4850
function clearTimeout(interval: number): void
4951
function setInterval(callback: (...args: any[]) => void, interval: number, ...args: any[]): number

typings/meta.d.ts

Lines changed: 61 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,18 @@ export interface SinusbotMeta {
44
description: string,
55
author: string,
66
hidden?: boolean,
7-
requiredModules?: string[],
7+
requiredModules?: ModulesWithSpecialPermission[],
88
backends?: Backends[],
99
engine?: string,
10-
vars?: Config<any>[],
10+
vars?: ConfigEntries[],
1111
autorun?: boolean,
1212
enableweb?: boolean
1313
}
1414

15-
export declare type Backends = "ts3"|"discord"
15+
export type ModulesWithSpecialPermission = "http"|"net"|"ws"|"db"|"fs"|"graphic"
16+
export type Backends = "ts3"|"discord"
1617

17-
18-
export interface Config<T> {
19-
type: string
20-
/** key name in the config object */
21-
name: string,
22-
/** display name in the webinterface */
23-
title: string,
24-
/** default variable if nothing has been set */
25-
default?: T,
26-
/** placeholder data in the webinterface */
27-
placeholder?: string
28-
indent?: number,
29-
//displays a config option depending on the config name and the value
30-
conditions?: {
31-
field: string,
32-
value: any
33-
}[]
34-
options?: string[]
35-
vars?: Config<any>[]
36-
}
37-
38-
//maybe for future sinusbot versions
39-
export type ConfigEntry =
18+
export type ConfigEntries =
4019
StringEntry |
4120
StringsEntry |
4221
PasswordEntry |
@@ -47,50 +26,81 @@ export type ConfigEntry =
4726
ChannelEntry |
4827
CheckboxEntry |
4928
SelectEntry |
50-
ArrayEntry<any>
29+
ArrayEntry
30+
31+
export type ConfigTypes =
32+
"string" |
33+
"strings" |
34+
"password" |
35+
"multiline" |
36+
"number" |
37+
"track" |
38+
"tracks" |
39+
"channel" |
40+
"checkbox" |
41+
"select" |
42+
"array"
43+
44+
export interface BaseConfig<T> {
45+
readonly type: ConfigTypes
46+
/** key name in the config object */
47+
readonly name: string,
48+
/** display name in the webinterface */
49+
readonly title: string,
50+
/** default variable if nothing has been set */
51+
readonly default: T,
52+
/** placeholder data in the webinterface */
53+
readonly placeholder?: string
54+
readonly indent?: number,
55+
//displays a config option depending on the config name and the value
56+
readonly conditions?: {
57+
readonly field: string,
58+
readonly value: any
59+
}[]
60+
}
5161

52-
export interface StringEntry extends Config<String> {
53-
type: "string",
62+
export interface StringEntry extends BaseConfig<String> {
63+
readonly type: "string"
5464
}
5565

56-
export interface StringsEntry extends Config<String[]> {
57-
type: "strings",
66+
export interface StringsEntry extends BaseConfig<String[]> {
67+
readonly type: "strings"
5868
}
5969

60-
export interface PasswordEntry extends Config<String> {
61-
type: "password",
70+
export interface PasswordEntry extends BaseConfig<String> {
71+
readonly type: "password"
6272
}
6373

64-
export interface MultilineEntry extends Config<String> {
65-
type: "multiline",
74+
export interface MultilineEntry extends BaseConfig<String> {
75+
readonly type: "multiline"
6676
}
6777

68-
export interface NumberEntry extends Config<Number> {
69-
type: "number",
78+
export interface NumberEntry extends BaseConfig<Number> {
79+
readonly type: "number"
7080
}
7181

72-
export interface TrackEntry extends Config<never> {
73-
type: "track",
82+
export interface TrackEntry extends BaseConfig<never> {
83+
readonly type: "track"
7484
}
7585

76-
export interface TracksEntry extends Config<never> {
77-
type: "tracks",
86+
export interface TracksEntry extends BaseConfig<never> {
87+
readonly type: "tracks"
7888
}
7989

80-
export interface ChannelEntry extends Config<never> {
81-
type: "channel",
90+
export interface ChannelEntry extends BaseConfig<never> {
91+
readonly type: "channel"
8292
}
8393

84-
export interface CheckboxEntry extends Config<Number> {
85-
type: "checkbox",
94+
export interface CheckboxEntry extends BaseConfig<Number> {
95+
readonly type: "checkbox"
8696
}
8797

88-
export interface SelectEntry extends Config<Number> {
89-
type: "select",
90-
options: string[]
98+
export interface SelectEntry extends BaseConfig<Number> {
99+
readonly type: "select"
100+
readonly options: string[]
91101
}
92102

93-
export interface ArrayEntry<T> extends Config<T[]> {
94-
type: "array",
95-
vars: ConfigEntry[]
103+
export interface ArrayEntry extends BaseConfig<any[]> {
104+
readonly type: "array"
105+
readonly vars: ConfigEntries[]
96106
}

0 commit comments

Comments
 (0)