-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
command.js
245 lines (234 loc) · 7.24 KB
/
command.js
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
const languages = require( './languages' );
const defaultLang = "en";
const isMod = ( channelName, userstate ) => userstate.mod || "#" + userstate.username == channelName
const isHomeChannel = ( channelName, { botChannelName } ) => channelName == botChannelName
function runCommand( channel, userstate, message, app ) {
const { prefixRegex, channels } = app
const command = message.split( /\s/ )[ 0 ].replace( prefixRegex, '' ).toLowerCase()
if( commands.hasOwnProperty( command ) ) {
const commandRunner = commands[ command ]
if( !authenticate( commandRunner, channel, userstate, app ) ) return
commandRunner( app, channel, channels[ channel ], userstate, message )
}
}
function authenticate( runner, channel, userstate, app ) {
if( runner.modOnly && !isMod( channel, userstate ) ) return false
if( runner.homeOnly && !isHomeChannel( channel, app ) ) return false
return true
}
const commands = {}
const firstKeys = []
function add( keys, fn, opts = {} ) {
keys.forEach( key => {
key = key.toLowerCase()
if( key in commands ) {
throw new Error( `${ key } already exists in commands` )
}
commands[ key ] = Object.assign( fn, opts )
} );
firstKeys.push( Array.from( keys ).sort( ( a, b ) => a.length - b.length )[ 0 ] )
}
function usageMapper( key ) {
const runner = commands[ key ]
if( runner.usage ) key = `${ key } ${ runner.usage }`
return '!' + key
}
add( [ "join" ],
(
{ channels, store, client },
channelName,
_,
{ username, [ "display-name" ]: display },
message
) => {
const userChannel = "#" + username
if( !channels[ userChannel ] ) {
client.join( userChannel )
.then( ( data ) => {
const [ , lang = defaultLang ] = message.split( /\s+/ )
channels[ data ] = {
lang: lang,
color: false,
uncensored: false,
langshow: false
};
store.put( "channels", channels );
client.say( userChannel, "/me Hello! I am ready to translate" );
client.say( channelName, "/me Okay, " + display );
} )
.catch( e => {
client.say( channelName, `@${ username } Something went wrong` );
console.log( `Something went wrong when trying to join ${ username }'s channel: `, err );
} );
} else {
client.say( channelName, "/me Already there" )
}
},
{
homeOnly: true,
description: {
en: 'join your channel'
}
}
)
add( [ "lang", "language" ],
( { channels, store, client }, channelName, channelConfig, userstate, message ) => {
const [ , targetLanguage = defaultLang ] = message.split( /\s+/ );
if( languages.isSupported( targetLanguage ) ) {
channelConfig.lang = languages.getCode( targetLanguage );
store.put( "channels", channels );
client.say( channelName, "/me Language was set to " + languages[ channelConfig.lang ] );
}
},
{
modOnly: true, usage: '[language]',
description: {
en: 'update target language on channel'
}
}
)
add( [ "languagelist", "langlist" ],
( { client }, channelName ) => {
const supportedlanguages = Object.keys( languages ).filter( lang => lang != "auto" && lang != "isSupported" && lang != "getCode" ).join( ", " );
client.say( channelName, "My supported languages are: " + supportedlanguages );
},
{
modOnly: true,
description: {
en: 'list available languages'
}
}
)
add( [ "languagecensor", "langcensor" ],
( { channels, store, client }, channelName, channelConfig ) => {
channelConfig.uncensored = !channelConfig.uncensored;
store.put( "channels", channels );
client.say( channelName,
channelConfig.uncensored
? "ChatTranslator will now allow NAUGHTY words."
: "ChatTranslator will now only allow NICE words."
);
},
{
modOnly: true,
description: {
en: 'toggle profanity censoring'
}
}
)
add( [ "languagestop", "langstop", "languageleave", "langleave" ],
( { channels, store, client }, channelName, channelConfig ) => {
delete channelConfig;
delete channels[ channelName ];
store.put( "channels", channels );
client.say( channelName, "Goodbye!!!" );
client.part( channelName );
},
{
modOnly: true,
description: {
en: 'leave current channel'
}
}
)
add( [ "languagecolor", "langcolor", "languagecolour", "langcolour" ],
( { channels, store, client }, channelName, channelConfig ) => {
channelConfig.color = !channelConfig.color;
store.put( "channels", channels );
const state = channelConfig.color ? "ENABLED" : "DISABLED"
client.say( channelName, `Chat color was ${ state }` );
},
{
modOnly: true,
description: {
en: 'toggle using /me'
}
}
)
add( [ "languagehelp", "langhelp" ],
( app, channelName, __, userstate, message ) => {
const [ , command ] = message.split( /\s+/ )
if( command && commands.hasOwnProperty( command ) ) {
const runner = commands[ command ]
if( authenticate( runner, channelName, userstate, app ) ) {
app.client.say(
channelName,
`The command ${ command } is to ${ runner.description.en }. Usage: ${ usageMapper( command ) }`
);
} else {
app.client.say(
channelName,
`The command ${ command } is not available to you`
);
}
}
else {
let commandsList = firstKeys.sort()
.filter( key => authenticate( commands[ key ], channelName, userstate, app ) )
.map( usageMapper )
.join( ', ' )
app.client.say( channelName, "My commands are " + commandsList );
}
},
{
description: {
en: 'provide help'
}
}
)
add( [ "languageshow", "langshow" ],
( { channels, store, client }, channelName, channelConfig ) => {
channelConfig.langshow = !channelConfig.langshow;
store.put( "channels", channels );
client.say( channelName,
channelConfig.langshow
? "ChatTranslator will now show the language name."
: "ChatTranslator will now only show the translated message."
);
},
{
modOnly: true,
description: {
en: 'toggle language tag'
}
}
)
add( [ "languageignore", "langignore" ],
( { channels, store, client }, channelName, channelConfig, userstate, message ) => {
var [ , username ] = message.split( /\s+/ );
if( !username ) return;
username = username.toLowerCase();
if( !channelConfig.ignore ) { channelConfig.ignore = {} };
if( channelConfig.ignore[ username ] ) {
delete channelConfig.ignore[ username ];
client.say( channelName,
"ChatTranslator will no longer ignore " + username
);
}
else {
channelConfig.ignore[ username ] = true;
client.say( channelName,
"ChatTranslator will now ignore " + username
);
}
store.put( "channels", channels );
},
{
modOnly: true,
description: {
en: 'toggle ignore user'
}
}
)
add( [ "languageinfo", "langinfo", "languageabout", "langabout" ],
( { client }, channelName ) => {
client.say( channelName, "My translations are sponsored thanks to the Microsoft Azure team! Learn more about Azure AI at: https://aka.ms/instafluff-social" );
},
{
modOnly: false,
description: {
en: 'about ChatTranslator'
}
}
)
module.exports = { runCommand, commands }