Skip to content

Commit 65a9063

Browse files
committedNov 21, 2022
clinet: Add essentials feature and fix template
1 parent 2ab0729 commit 65a9063

File tree

5 files changed

+100
-51
lines changed

5 files changed

+100
-51
lines changed
 

‎.gitignore

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Bot configuration
2-
config.json
3-
config.json.bak
2+
*.json
3+
*.json.old
4+
*.json.bak
45

56
# Bot save states
67
state/

‎bot.go

+51-35
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616

1717
//Clinet's features
1818
"github.com/Clinet/clinet_features_dumpctx"
19+
"github.com/Clinet/clinet_features_essentials"
1920
"github.com/Clinet/clinet_features_hellodolly"
2021
"github.com/Clinet/clinet_features_moderation"
2122
"github.com/Clinet/clinet_features_voice"
@@ -37,58 +38,73 @@ func doBot() {
3738
//For some reason we don't automatically exit as planned when we return to main()
3839
defer os.Exit(0)
3940

40-
log.Info("Loading configuration...")
41-
cfg, err := config.LoadConfig(configFile, config.ConfigTypeJSON)
41+
log.Info("Loading features...")
42+
cfg, err := config.LoadConfig(featuresFile, config.ConfigTypeJSON)
4243
if err != nil {
43-
log.Error("Error loading configuration: ", err)
44+
log.Error("Error loading features: ", err)
4445
return
4546
}
4647

47-
log.Info("Syncing configuration...")
48-
cfg.SaveTo(configFile, config.ConfigTypeJSON)
49-
50-
if writeConfigTemplate {
51-
log.Info("Updating configuration template...")
52-
templateCfg := config.NewConfig()
53-
templateCfg.Features = append(templateCfg.Features, &features.Feature{Name: "example", Toggle: true})
54-
templateCfg.SaveTo("config.template.json", config.ConfigTypeJSON)
48+
if cfg.Features == nil || len(cfg.Features) == 0 {
49+
log.Error("No features found in configuration!")
50+
return
5551
}
5652

53+
log.Debug("Syncing format of features file...")
54+
cfg.SaveTo(featuresFile, config.ConfigTypeJSON)
55+
56+
//Initialize Clinet using the configuration provided
57+
log.Info("Initializing instance of Clinet...")
5758
clinet = bot.NewBot(cfg)
59+
60+
//Clinet is effectively online at this stage, so defer shutdown in case of errors below
5861
defer clinet.Shutdown()
5962

60-
log.Debug("Registering features...")
61-
if err := clinet.RegisterFeature(dumpctx.Feature); err != nil {
62-
log.Fatal(err)
63-
}
64-
if err := clinet.RegisterFeature(hellodolly.Feature); err != nil {
65-
log.Fatal(err)
66-
}
67-
if err := clinet.RegisterFeature(moderation.Feature); err != nil {
68-
log.Fatal(err)
69-
}
70-
if err := clinet.RegisterFeature(voice.Feature); err != nil {
71-
log.Fatal(err)
72-
}
63+
//Register the conversation services to handle queries
64+
log.Debug("Registering conversation services...")
65+
log.Trace("- duckduckgo")
66+
logFatalError(clinet.RegisterFeature(duckduckgo.Feature))
67+
log.Trace("- wolframalpha")
68+
logFatalError(clinet.RegisterFeature(wolframalpha.Feature))
7369

74-
if err := clinet.RegisterConvoService("duckduckgo", duckduckgo.DuckDuckGo); err != nil {
75-
log.Fatal(err)
76-
}
77-
if err := clinet.RegisterConvoService("wolframalpha", wolframalpha.WolframAlpha); err != nil {
78-
log.Fatal(err)
70+
//Register the features to handle commands
71+
log.Debug("Registering features...")
72+
log.Trace("- dumpctx")
73+
logFatalError(clinet.RegisterFeature(dumpctx.Feature))
74+
log.Trace("- hellodolly")
75+
logFatalError(clinet.RegisterFeature(hellodolly.Feature))
76+
log.Trace("- moderation")
77+
logFatalError(clinet.RegisterFeature(moderation.Feature))
78+
log.Trace("- voice")
79+
logFatalError(clinet.RegisterFeature(voice.Feature))
80+
log.Trace("- essentials")
81+
logFatalError(clinet.RegisterFeature(essentials.Feature)) //ALWAYS REGISTER ESSENTIALS LAST BEFORE CHAT SERVICES!
82+
83+
log.Debug("Registering chat services...")
84+
log.Trace("- discord")
85+
logFatalError(clinet.RegisterFeature(discord.Feature))
86+
log.Trace("- guilded")
87+
logFatalError(clinet.RegisterFeature(guilded.Feature))
88+
89+
if writeFeaturesTemplate {
90+
log.Debug("Updating features template...")
91+
templateCfg := config.NewConfig()
92+
templateCfg.Features = features.FM.Features
93+
templateCfg.SaveTo("features.template.json", config.ConfigTypeJSON)
7994
}
8095

81-
if err := clinet.RegisterService("discord", discord.Discord); err != nil {
82-
log.Fatal(err)
83-
}
84-
if err := clinet.RegisterService("guilded", guilded.Guilded); err != nil {
85-
log.Fatal(err)
86-
}
96+
log.Info("Clinet is now online!")
8797

8898
log.Debug("Waiting for SIGINT syscall signal...")
8999
sc := make(chan os.Signal, 1)
90100
signal.Notify(sc, syscall.SIGINT)
91101
<-sc
92102

93103
log.Info("Good-bye!")
104+
}
105+
106+
func logFatalError(err error) {
107+
if err != nil {
108+
log.Fatal(err)
109+
}
94110
}

‎config.template.json

-8
This file was deleted.

‎features.template.json

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"features": [
3+
{
4+
"toggle": true,
5+
"name": "duckduckgo"
6+
},
7+
{
8+
"toggle": true,
9+
"name": "wolframalpha"
10+
},
11+
{
12+
"toggle": true,
13+
"name": "dumpctx"
14+
},
15+
{
16+
"toggle": true,
17+
"name": "hellodolly"
18+
},
19+
{
20+
"toggle": true,
21+
"name": "moderation"
22+
},
23+
{
24+
"toggle": true,
25+
"name": "voice"
26+
},
27+
{
28+
"toggle": true,
29+
"name": "essentials"
30+
},
31+
{
32+
"toggle": true,
33+
"name": "discord"
34+
},
35+
{
36+
"toggle": true,
37+
"name": "guilded"
38+
}
39+
]
40+
}

‎main.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,22 @@ import (
77

88
var (
99
//Various command-line flags
10-
configFile string //path to bot configuration
11-
writeConfigTemplate bool //if true, write the current configuration template to config.template.json
12-
verbosity int //0 = default (info, warning, error), 1 = 0 + debug, 2 = 1 + trace
10+
featuresFile string //path to bot feature toggles
11+
writeFeaturesTemplate bool //if true, write the current features template to features.template.json
12+
verbosity int //0 = default (info, warning, error), 1 = 0 + debug, 2 = 1 + trace
1313
)
1414

1515
func init() {
1616
watchdog.Header = "Clinet © JoshuaDoes 2017-2022."
1717
watchdog.Footer = "Good-bye!"
18-
watchdog.ImmediateSpawn = false //We want to display Header for a short time
18+
watchdog.ImmediateSpawn = true //We want to display Header for a short time
1919
watchdog.KillOldMain = true //We want to be the only live instance of this bot
2020
}
2121

2222
func main() {
2323
//Apply all command-line flags
24-
pflag.StringVar(&configFile, "config", "config.json", "path to bot configuration")
25-
pflag.BoolVar(&writeConfigTemplate, "writeConfigTemplate", true, "write the current configuration template to config.template.json")
24+
pflag.StringVar(&featuresFile, "features", "features.json", "path to bot feature toggles")
25+
pflag.BoolVar(&writeFeaturesTemplate, "writeFeaturesTemplate", true, "write the current features template to features.template.json")
2626
pflag.IntVar(&verbosity, "verbosity", 0, "sets the verbosity level; 0 = default, 1 = debug, 2 = trace")
2727

2828
if watchdog.Parse() {

0 commit comments

Comments
 (0)
Please sign in to comment.