Skip to content

Commit 4db05a6

Browse files
committed
Feature : Command : Add cooldown
1 parent 2755cf3 commit 4db05a6

File tree

3 files changed

+44
-15
lines changed

3 files changed

+44
-15
lines changed

bot-features/src/main/kotlin/fr/delphes/features/twitch/command/NewCommand.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ import fr.delphes.rework.feature.FeatureRuntime
1414
import fr.delphes.rework.feature.SimpleFeatureRuntime
1515
import fr.delphes.state.State
1616
import fr.delphes.state.StateProvider
17+
import fr.delphes.state.state.TimeState
1718
import fr.delphes.twitch.TwitchChannel
19+
import java.time.Duration
1820

1921
@DynamicForm("twitch-command-form")
2022
data class NewCommand(
@@ -23,22 +25,28 @@ data class NewCommand(
2325
override val channel: TwitchChannel,
2426
@FieldDescription("Command to trigger the response")
2527
val command: String,
28+
@FieldDescription("Cooldown")
29+
val cooldown: Duration = Duration.ZERO,
2630
@FieldDescription("Actions to do when the command is triggered")
2731
val actions: List<OutgoingEvent> = emptyList(),
2832
) : TwitchFeature, FeatureDefinition {
2933
override val id: FeatureId = FeatureId()
34+
private val lastCallStateId = TimeState.id("cooldown-${id.value}")
35+
3036
override fun buildRuntime(stateManager: StateProvider): FeatureRuntime {
3137
return SimpleFeatureRuntime.whichHandle<CommandAsked>(id) {
32-
if (event.data.command.triggerMessage == command) {
38+
val lastCallState = stateManager.getState(lastCallStateId)
39+
if (event.data.command.triggerMessage == command && lastCallState.hasMoreThan(cooldown)) {
3340
actions.forEach { action ->
3441
executeOutgoingEvent(action)
3542
}
43+
lastCallState.putNow()
3644
}
3745
}
3846
}
3947

4048
override fun getSpecificStates(stateProvider: StateProvider): List<State> {
41-
return emptyList()
49+
return listOf(TimeState.withClockFrom(stateProvider, lastCallStateId.qualifier))
4250
}
4351

4452
override val commands = listOf(Command(command))

bot-ui/src/common/dynamicForm/field/duration/FieldDuration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export class FieldDuration implements Field<Duration> {
2121
public readonly description: string,
2222
public readonly fieldName: string,
2323
public readonly initialValue: Duration,
24-
public readonly actualValue: Duration = initialValue
24+
public actualValue: Duration = initialValue
2525
) {
2626
}
2727

bot-ui/src/common/dynamicForm/field/duration/FieldDurationEditView.vue

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,45 @@
1919

2020
<script lang="ts" setup>
2121
import UiTextfield from "@/common/designSystem/form/textfield/UiTextfield.vue";
22-
import {FieldDuration} from "@/common/dynamicForm/field/duration/FieldDuration";
23-
import {PropType, ref} from "vue";
22+
import {FieldString} from "@/common/dynamicForm/field/string/FieldString";
23+
import {Duration} from "@/common/utils/Duration";
24+
import {computed, PropType} from "vue";
2425
25-
const emits = defineEmits<{
26-
modifyDescriptor: [descriptor: FieldDuration]
27-
}>()
28-
29-
const props = defineProps({
26+
defineProps({
3027
field: {
31-
type: Object as PropType<FieldDuration>,
28+
type: Object as PropType<FieldString>,
3229
required: true,
33-
}
30+
},
3431
})
32+
const model = defineModel<Duration>({required: true})
3533
36-
const actualHours = ref(props.field.actualValue.hours)
34+
console.log(model.value)
3735
38-
const actualMinutes = ref(props.field.actualValue.minutes)
36+
const actualHours = computed<number>({
37+
get() {
38+
return model.value.hours
39+
},
40+
set(value: number) {
41+
model.value = model.value.withHours(value)
42+
}
43+
})
3944
40-
const actualSeconds = ref(props.field.actualValue.seconds)
45+
const actualMinutes = computed<number>({
46+
get() {
47+
return model.value.minutes
48+
},
49+
set(value: number) {
50+
model.value = model.value.withMinutes(value)
51+
}
52+
})
53+
54+
const actualSeconds = computed<number>({
55+
get() {
56+
return model.value.seconds
57+
},
58+
set(value: number) {
59+
model.value = model.value.withSeconds(value)
60+
}
61+
})
4162
4263
</script>

0 commit comments

Comments
 (0)