/
TwitchPubSubBuilder.java
135 lines (116 loc) · 3.79 KB
/
TwitchPubSubBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package com.github.twitch4j.pubsub;
import com.github.philippheuer.events4j.api.service.IEventHandler;
import com.github.philippheuer.events4j.core.EventManager;
import com.github.philippheuer.events4j.simple.SimpleEventHandler;
import com.github.twitch4j.client.websocket.WebsocketConnection;
import com.github.twitch4j.client.websocket.WebsocketConnectionConfig;
import com.github.twitch4j.common.config.ProxyConfig;
import com.github.twitch4j.common.util.EventManagerUtils;
import com.github.twitch4j.common.util.ThreadUtils;
import com.github.twitch4j.util.IBackoffStrategy;
import lombok.*;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import java.util.Collection;
import java.util.HashSet;
import java.util.concurrent.ScheduledThreadPoolExecutor;
/**
* Twitch PubSub Builder
*/
@Slf4j
@Getter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class TwitchPubSubBuilder {
/**
* WebsocketConnection
* <p>
* can be used to inject a mocked connection into the TwitchChat instance
*/
@With(AccessLevel.PROTECTED)
private WebsocketConnection websocketConnection = null;
/**
* Event Manager
*/
@With
private EventManager eventManager = null;
/**
* EventManager
*/
@With
private Class<? extends IEventHandler> defaultEventHandler = SimpleEventHandler.class;
/**
* Scheduler Thread Pool Executor
*/
@With
private ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = null;
/**
* Proxy Configuration
*/
@With
private ProxyConfig proxyConfig = null;
/**
* User IDs of Bot Owners for applying {@link com.github.twitch4j.common.enums.CommandPermission#OWNER}
*/
@Setter
@Accessors(chain = true)
private Collection<String> botOwnerIds = new HashSet<>();
/**
* WebSocket RFC Ping Period in ms (0 = disabled)
*/
@With
private int wsPingPeriod = 15_000;
/**
* Websocket Close Delay in ms (0 = minimum)
* @see WebsocketConnectionConfig#closeDelay()
*/
@With
private int wsCloseDelay = 1_000;
/**
* WebSocket Connection Backoff Strategy
*/
@With
private IBackoffStrategy connectionBackoffStrategy = null;
/**
* Initialize the builder
*
* @return Twitch PubSub Builder
*/
public static TwitchPubSubBuilder builder() {
return new TwitchPubSubBuilder();
}
/**
* Twitch PubSub Client
*
* @return TwitchPubSub
*/
public TwitchPubSub build() {
log.debug("PubSub: Initializing Module ...");
if (scheduledThreadPoolExecutor == null)
scheduledThreadPoolExecutor = ThreadUtils.getDefaultScheduledThreadPoolExecutor("twitch4j-pubsub-" + RandomStringUtils.random(4, true, true), TwitchPubSub.REQUIRED_THREAD_COUNT);
// Initialize/Check EventManager
eventManager = EventManagerUtils.validateOrInitializeEventManager(eventManager, defaultEventHandler);
return new TwitchPubSub(this.websocketConnection, this.eventManager, scheduledThreadPoolExecutor, this.proxyConfig, this.botOwnerIds, this.wsPingPeriod, this.connectionBackoffStrategy, this.wsCloseDelay);
}
/**
* With a Bot Owner's User ID
*
* @param userId the user id
* @return TwitchPubSubBuilder
*/
public TwitchPubSubBuilder withBotOwnerId(String userId) {
this.botOwnerIds.add(userId);
return this;
}
/**
* With multiple Bot Owner User IDs
*
* @param botOwnerIds the user ids
* @return TwitchPubSubBuilder
*/
public TwitchPubSubBuilder withBotOwnerIds(Collection<String> botOwnerIds) {
this.botOwnerIds.addAll(botOwnerIds);
return this;
}
}