-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
185 lines (155 loc) · 7.89 KB
/
index.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
import React from 'react'
import { Plugin } from '@vizality/entities'
import { patch, unpatch } from '@vizality/patcher'
import { getModule, messages, getModuleByDisplayName } from '@vizality/webpack'
import { findInReactTree } from '@vizality/util/react'
const { receiveMessage } = messages
const HeaderBarButton = require('./components/HeaderBarButton')
const TextContainerButton = require('./components/TextContainerButton')
export default class GrammarNazi extends Plugin {
async start() {
vizality.api.commands.registerCommand({
command: 'addword',
aliases: ['aw', 'aword'],
description: 'Add a key/value pair to the custom dictionary.',
usage: '{c} "key" "value"',
executor: (args) => this.addDict(args)
})
vizality.api.commands.registerCommand({
command: 'removeword',
aliases: ['rm', 'rmword'],
description: 'Remove a key/value pair from the custom dictionary.',
usage: '{c} "key"',
executor: (args) => this.removeDict(args)
})
vizality.api.commands.registerCommand({
command: 'listwords',
aliases: ['lw', 'dictionary', 'dict'],
description: 'View the current custom dictionary.',
usage: '{c}',
executor: () => this.viewDict()
})
/* Stylesheet */
this.injectStyles("style.scss")
/* Define Settings */
if (this.settings.get('customDictionary') === undefined) this.settings.set('customDictionary', {})
let settingsArray = ['nazify', 'punctuation', 'capitalization', 'dictionary', 'location']
for (let i = 0; i < settingsArray.length; i++) {
if (this.settings.get(settingsArray[i]) === undefined) this.settings.set(settingsArray[i], false)
}
/* Inject on Message Send */
const MessageEvents = await getModule('sendMessage')
patch('message-send', MessageEvents, 'sendMessage', (args) => {
let text = args[1].content.trim()
let split = text.split(' ')
let customDictionary = this.settings.get('customDictionary')
if (text.indexOf('```') === -1 && this.settings.get('nazify') && text.charAt(0) !== '.') {
if (this.settings.get('dictionary')) text = split.map(c => c in customDictionary ? customDictionary[c] : c).join(' ')
if (this.settings.get('punctuation') && (/[a-z0-9]$/gmi).test(text) && split[split.length-1].indexOf('http') === -1 ) text += '.'
if (this.settings.get('capitalization') && text.indexOf('http') != 0) text = text.charAt(0).toUpperCase() + text.substring(1)}
args[1].content = text
return args
}, true)
/* Inject Toggle Button */
const ChannelTextAreaContainer = getModule((m) => m.type && m.type.render && m.type.render.displayName === 'ChannelTextAreaContainer', false)
patch('chat-button', ChannelTextAreaContainer.type, 'render', (args, res) => {
if (this.settings.get('location') === 'channel-text-area-container') {
const props = findInReactTree(res, (r) => r && r.className && r.className.indexOf('buttons-') == 0)
props.children.unshift(<TextContainerButton settings={this.settings}/>)
}
return res
})
ChannelTextAreaContainer.type.render.displayName = 'ChannelTextAreaContainer'
const HeaderBarContainer = await getModuleByDisplayName('HeaderBarContainer')
patch('header-bar', HeaderBarContainer.prototype, 'render', (args, res)=> {
if (this.settings.get('location') === 'header-bar-container')
res.props.toolbar.props.children.unshift(<HeaderBarButton settings={this.settings} bartype={HeaderBarContainer.Icon}/>)
return res
})
}
stop() {
vizality.api.commands.unregisterCommand('addword')
vizality.api.commands.unregisterCommand('removeword')
vizality.api.commands.unregisterCommand('listwords')
unpatch('message-send')
unpatch('chat-button')
unpatch('header-bar')
document.querySelectorAll('.toggle-button').forEach(e => e.style.display = 'none')
}
async addDict(args) {
/* Custom Bot Attributes */
const { BOT_AVATARS } = await getModule('BOT_AVATARS')
const { createBotMessage } = await getModule('createBotMessage')
const { getChannelId } = getModule('getLastSelectedChannelId', false)
const receivedMessage = createBotMessage(getChannelId(), {})
BOT_AVATARS.GrammarNaziAvatar = 'https://i.imgur.com/wUcHvh0.png'
receivedMessage.author.username = 'Grammar Nazi'
receivedMessage.author.avatar = 'GrammarNaziAvatar'
/* String Formatting */
let newargs = []
let text = args.join(' ')
newargs[0] = text.substring(0, text.indexOf('" "')).replace(/"/g, '')
newargs[1] = text.substring(text.indexOf('" "') + 2, text.length).replace(/"/g, '')
if (newargs[0].length < 1 || newargs[1] == ' ') {
receivedMessage.content = 'Insufficent arguments; both a keyword and value must be supplied.'
return receiveMessage(receivedMessage.channel_id, receivedMessage)
}
/* Duplicate Check */
let customDictionary = this.settings.get('customDictionary')
if (newargs[0] in customDictionary) {
receivedMessage.content = `Entry "${newargs[0]}" already exists!`
return receiveMessage(receivedMessage.channel_id, receivedMessage)
}
/* Save to Dictionary */
customDictionary[newargs[0]] = newargs[1]
this.settings.set('customDictionary', customDictionary)
receivedMessage.content = `Entry "${newargs[0]}" successfully created with value of "${newargs[1]}".`
return receiveMessage(receivedMessage.channel_id, receivedMessage)
}
async removeDict(args) {
/* Custom Bot Attributes */
const { BOT_AVATARS } = await getModule('BOT_AVATARS')
const { createBotMessage } = await getModule('createBotMessage')
const { getChannelId } = getModule('getLastSelectedChannelId', false)
const receivedMessage = createBotMessage(getChannelId(), {})
BOT_AVATARS.GrammarNaziAvatar = 'https://i.imgur.com/wUcHvh0.png'
receivedMessage.author.username = 'Grammar Nazi'
receivedMessage.author.avatar = 'GrammarNaziAvatar'
/* String Formatting */
let customDictionary = this.settings.get('customDictionary')
let text = args.join(' ').replace(/"/gm, '')
/* Arguments Check */
if (!(args.join(' ').includes('"'))) {
receivedMessage.content = 'Insufficent arguments; please provide a keyword that exists in the dictionary.'
return receiveMessage(receivedMessage.channel_id, receivedMessage)
}
/* Remove from Dictionary */
if (text in customDictionary) {
delete customDictionary[text]
this.settings.set('customDictionary', customDictionary)
receivedMessage.content = `Entry ${args[0]} was successfully deleted!`
return receiveMessage(receivedMessage.channel_id, receivedMessage)
} else {
receivedMessage.content = `Entry ${args[0]} does not exist.`
return receiveMessage(receivedMessage.channel_id, receivedMessage)
}
}
async viewDict() {
/* Custom Bot Attributes */
const { BOT_AVATARS } = await getModule('BOT_AVATARS')
const { createBotMessage } = await getModule('createBotMessage')
const { getChannelId } = getModule('getLastSelectedChannelId', false)
const receivedMessage = createBotMessage(getChannelId(), {})
BOT_AVATARS.GrammarNaziAvatar = 'https://i.imgur.com/wUcHvh0.png'
receivedMessage.author.username = 'Grammar Nazi'
receivedMessage.author.avatar = 'GrammarNaziAvatar'
/* Write Message */
let customDictionary = this.settings.get('customDictionary')
let dictionary = '> '
for (let i in customDictionary) {
dictionary += i + ' : ' + customDictionary[i] + '\n> '
}
receivedMessage.content = dictionary
return receiveMessage(receivedMessage.channel_id, receivedMessage)
}
}