forked from reactiflux/reactibot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.ts
More file actions
1433 lines (1343 loc) · 49.4 KB
/
commands.ts
File metadata and controls
1433 lines (1343 loc) · 49.4 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* eslint-disable @typescript-eslint/no-use-before-define */
import fetch from "node-fetch";
import {
APIEmbed,
ChannelType,
EmbedType,
Message,
OmitPartialGroupDMChannel,
PartialGroupDMChannel,
TextChannel,
} from "discord.js";
import cooldown from "./cooldown.js";
import type { ChannelHandlers } from "../types/index.d.ts";
import { getMessage, isStaff } from "../helpers/discord.js";
import {
extractSearchKey,
getReactDocsContent,
getReactDocsSearchKey,
} from "../helpers/react-docs.js";
import dedent from "dedent";
export const EMBED_COLOR = 7506394;
type Categories = "Reactiflux" | "Communication" | "Web" | "React/Redux";
type Command = {
words: string[];
help: string;
category: Categories;
handleMessage: (msg: OmitPartialGroupDMChannel<Message>) => void;
cooldown?: number;
};
const sortedCategories: Categories[] = [
"Reactiflux",
"Communication",
"Web",
"React/Redux",
];
const commandsList: Command[] = [
{
words: [`!commands`],
help: `lists all available commands`,
category: "Reactiflux",
handleMessage: (msg) => {
const commandsMessage = createCommandsMessage();
msg.reply({
embeds: [
{
title: "Available Help Commands",
type: EmbedType.Rich,
description: commandsMessage,
color: EMBED_COLOR,
},
],
});
},
},
{
words: [`!conduct`],
help: `informs user's of code of conduct`,
category: "Reactiflux",
handleMessage: (msg) => {
msg.reply({
embeds: [
{
title: "Code of Conduct",
type: EmbedType.Rich,
description: `Reactiflux is the largest chat community of React developers. We make a deliberate effort to have a light touch when it comes to moderating, but we do have some expectations of how our members will behave. Please read the [full Reactiflux Code of Conduct](https://www.reactiflux.com/conduct)`,
color: EMBED_COLOR,
},
],
});
},
},
{
words: [`!contribution`],
help: `informs user's of contributing to React codebase`,
category: "Communication",
handleMessage: (msg) => {
msg.reply({
embeds: [
{
title: "Contributing to React",
type: EmbedType.Rich,
description: `
React is a very large and complex project. It has a steep learning curve, involves a lot of internal tooling and architecture, and often requires deep context to make meaningful contributions.
If you’re looking to gain experience in open source, we recommend starting with projects that are more beginner-friendly but still widely used and respected in the React ecosystem. Great alternatives include:
`,
fields: [
{
name: "Open Source Alternatives",
value: `
- [TanStack](https://github.com/tanstack)
- [Preact](https://github.com/preactjs/preact)
- [Remix](https://github.com/remix-run/remix)
- [SolidJS](https://github.com/solidjs/solid)
- [React Hook Form](https://github.com/react-hook-form/react-hook-form)
- [freeCodeCamp](https://github.com/freeCodeCamp/freeCodeCamp)
- [Daily.dev](https://github.com/dailydotdev/daily)
`,
inline: true,
},
],
},
],
});
},
},
{
words: [`!crosspost`],
help: `provides a no cross-post message`,
category: "Communication",
handleMessage: (msg) => {
msg.channel.send({
embeds: [
{
title: "Please Avoid Cross-Posting",
type: EmbedType.Rich,
description:
"Just a friendly reminder: please avoid posting the same message in multiple channels. Choose the channel that best fits your question and allow some time for a response. If you don’t hear back after a while, feel free to bump your message.",
color: EMBED_COLOR,
},
],
});
},
},
{
words: [`!promotion`],
help: `informs user's of self-promotion guidelines`,
category: "Reactiflux",
handleMessage: (msg) => {
msg.reply({
embeds: [
{
title: "Self Promotion",
type: EmbedType.Rich,
description: `Reactiflux is a peer group, not an advertising channel or a free audience. Please review [our guidelines around self-promotion](https://www.reactiflux.com/promotion)`,
color: EMBED_COLOR,
},
],
});
},
},
{
words: [`!rrlinks`],
help: `shares a repository of helpful links regarding React and Redux`,
category: "React/Redux",
handleMessage: (msg) => {
msg.channel.send({
embeds: [
{
title: "Helpful links",
type: EmbedType.Rich,
description: `Reactiflux's Mark Erikson has put together [a curated list of useful React & Redux links](https://github.com/markerikson/react-redux-links) for developers of all skill levels.`,
color: EMBED_COLOR,
},
],
});
},
},
{
words: [`!xy`],
help: `explains the XY problem`,
category: "Communication",
handleMessage: (msg) => {
msg.channel.send({
embeds: [
{
title: "The XY Issue",
type: EmbedType.Rich,
description: `You may be experiencing an [XY problem](http://xyproblem.info/). Try to explain your end goal, instead of the error you got stuck on. Maybe there's a better way to approach the problem.`,
color: EMBED_COLOR,
},
],
});
},
},
{
words: [`!courses`],
help: `provides a curated list of quality React courses and learning platforms`,
category: "React/Redux",
handleMessage: (msg) => {
msg.channel.send({
embeds: [
{
title: "Quality React Courses & Learning Platforms",
type: EmbedType.Rich,
description: `Here are some highly recommended courses and platforms for learning React, from beginner to advanced levels:`,
fields: [
{
name: "Premium Courses",
value: `
- [Joy of React](https://www.joyofreact.com/) - Comprehensive React course
- [Epic React](https://epicreact.dev/) - Advanced React patterns and techniques
- [Advanced React](https://www.advanced-react.com/) - By Nadia Makarevich
- [Jack Herrington's Courses](https://www.youtube.com/@jherr) - Various React topics
`,
inline: true,
},
{
name: "Learning Platforms",
value: `
- [Frontend Masters](https://frontendmasters.com/) - Professional-grade courses
- [React.dev](https://react.dev/) - Official React documentation
- [Reactiflux Learning Resources](https://www.reactiflux.com/learning) - Curated links
`,
inline: true,
},
],
color: EMBED_COLOR,
},
],
});
},
},
{
words: [`!ymnnr`],
help: `links to the You Might Not Need Redux article`,
category: "React/Redux",
handleMessage: (msg) => {
msg.channel.send({
embeds: [
{
title: "You Might Not Need Redux",
type: EmbedType.Rich,
description: `People often choose Redux before they need it. “What if our app doesn't scale without it?"
Read more about this in the [article "You Might Not Need Redux" by Dan Abramov](https://medium.com/@dan_abramov/you-might-not-need-redux-be46360cf367)`,
color: EMBED_COLOR,
},
],
});
},
},
{
words: [`!derived`],
help: `Links to the React docs advice to avoid copying props to state`,
category: "React/Redux",
handleMessage: (msg) => {
msg.channel.send({
embeds: [
{
title:
"You might not need getDerivedStateFrom props or state at all!",
type: EmbedType.Rich,
description: `Copying data from React props to component state is usually not necessary, and should generally be avoided. The React team offered advice on when "derived state" may actually be needed in their [article "You Probably Don't Need Derived State"](https://legacy.reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html)`,
color: EMBED_COLOR,
},
],
});
},
},
{
words: [`!su`, `!stateupdates`],
help: `Explains the implications involved with state updates being asynchronous`,
category: "React/Redux",
handleMessage: (msg) => {
msg.channel.send({
embeds: [
{
title: "State Updates May Be Asynchronous",
type: EmbedType.Rich,
description: `Often times you run into an issue like this
\`\`\`js
const handleEvent = e => {
setState(e.target.value);
console.log(state);
}
\`\`\`
where \`state\` is not the most up to date value when you log it. This is caused by state updates being asynchronous.
Check out these resources for more information:
- [React.dev: Queueing a Series of State Updates](https://react.dev/learn/queueing-a-series-of-state-updates)
- [ReactJS.org: State Updates May Be Asynchronous](https://legacy.reactjs.org/docs/state-and-lifecycle.html#state-updates-may-be-asynchronous)
- [Blogged Answers: Render Batching and Timing](https://blog.isquaredsoftware.com/2020/05/blogged-answers-a-mostly-complete-guide-to-react-rendering-behavior/#render-batching-and-timing)`,
color: EMBED_COLOR,
},
],
});
},
},
{
words: [`!bind`],
help: `explains how and why to bind in React applications`,
category: "React/Redux",
handleMessage: (msg) => {
msg.channel.send({
embeds: [
{
title: "Binding functions",
type: EmbedType.Rich,
description: `In JavaScript, a class function will not be bound to the instance of the class, this is why you often see messages saying that you can't access something of undefined.
In order to fix this, you need to bind your function, either in constructor:
\`\`\`js
class YourComponent extends React.Component {
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
// you can access \`this\` here, because we've
// bound the function in constructor
}
}
\`\`\`
or by using class properties babel plugin (it works in create-react-app by default!)
\`\`\`js
class YourComponent extends React.Component {
handleClick = () => {
// you can access \`this\` here just fine
}
}
\`\`\`
Check out [this article on "Why and how to bind methods in your React component classes?"](https://reactkungfu.com/2015/07/why-and-how-to-bind-methods-in-your-react-component-classes/) for more information.`,
color: EMBED_COLOR,
},
],
});
},
},
{
words: [`!lift`],
help: `links to the React docs regarding the common need to "lift" state`,
category: "React/Redux",
handleMessage: (msg) => {
msg.channel.send({
embeds: [
{
title: "Lifting State Up",
type: EmbedType.Rich,
description: `Often, several components need to reflect the same changing data. We recommend lifting the shared state up to their closest common ancestor.
Learn more about lifting state in the [React.dev article about "Sharing State Between Components"](https://react.dev/learn/sharing-state-between-components).`,
color: EMBED_COLOR,
},
],
});
},
},
{
words: [`!ask`],
help: `explains how to ask questions`,
category: "Reactiflux",
handleMessage: (msg) => {
msg.channel.send({
embeds: [
{
title: "Asking to ask",
type: EmbedType.Rich,
description: `Instead of asking to ask, ask your question instead. People can help you better if they know your question.
Bad: "hey can anyone help me?"
Bad: "anyone good with redux?"
Good:
> I'm trying to fire a redux action from my component, but it's not getting to the reducer.
> \`\`\`js
> // snippet of code
> \`\`\`
> I'm seeing an error, but I don't know if it's related.
> \`Uncaught TypeError: undefined is not a function\`
Have a look at these resources on how to ask good questions:
- [Coding Killed the Cat: "How to Ask for Programming Help"](http://wp.me/p2oIwo-26)
- [Stack Overflow: "How do I ask a good question?"](https://stackoverflow.com/help/how-to-ask)
- [Eric S. Raymond; "How To Ask Questions The Smart Way"](https://git.io/JKscV)
`,
color: EMBED_COLOR,
},
],
});
},
},
{
words: [`!security`],
help: `general information around managing security for a web application.`,
category: "Reactiflux",
handleMessage: (msg) => {
msg.channel.send({
embeds: [
{
title: "Security Tips",
type: EmbedType.Rich,
description: `Managing security in a web application requires a proactive approach.
Some points to consider:
- Don't use create-react-app it is [no longer supported](https://react.dev/blog/2025/02/14/sunsetting-create-react-app).
- Set up automated alerts via a service like [dependabot](https://docs.github.com/en/code-security/getting-started/dependabot-quickstart-guide) to be notified of new disclosures.
- Review packages either by inspecting the code or using a service like [Snyk](https://security.snyk.io/vuln/npm).
- Apply [secure coding principles and practices](https://owasp.org/www-project-secure-coding-practices-quick-reference-guide/stable-en/02-checklist/05-checklist.html) - there are [free for open source tool](https://owasp.org/www-community/Free_for_Open_Source_Application_Security_Tools) like [Sonar Qube](https://www.sonarsource.com/open-source-editions/sonarqube-community-edition/) which help find common vulnerabilities like SQL injection, cross-site scripting (XSS), path traversal, and insecure configurations.
- Proactively keep your technology up to date – (everything not just packages).
- Keep your identity safe online, don't reuse passwords, enable multi-factor authentication and use a password manager service.
If you are ever unsure just ask! Better to be safe then sorry.
`,
color: EMBED_COLOR,
},
],
});
},
},
{
words: [`!code`, `!gist`],
help: `explains how to attach code`,
category: "Reactiflux",
handleMessage: (msg) => {
msg.channel.send({
embeds: [
{
title: "Attaching Code",
image: {
url: "https://media1.tenor.com/images/a23c33a91cb8d026b83488f1673495fd/tenor.gif?itemid=27632534",
},
type: EmbedType.Rich,
description: `
Please don't post code in screenshots or post unformatted code. Instead, use one of these preferred methods to share code:
\\\`\\\`\\\`js
// short code snippets go here
\\\`\\\`\\\`
Please look at the gif attached below
Link a Gist to upload entire files: https://gist.github.com
Link a Code Sandbox to share runnable examples: https://codesandbox.io/s
Link a Code Sandbox to an existing GitHub repo: https://codesandbox.io/s/github/<username>/<reponame>
Link a TypeScript Playground to share types: https://www.typescriptlang.org/play
Link a Snack to share React Native examples: https://snack.expo.io
`,
color: EMBED_COLOR,
},
],
});
},
},
{
words: [`!ping`],
help: `explains how to ping politely`,
category: "Reactiflux",
handleMessage: (msg) => {
msg.channel.send({
embeds: [
{
title: "Don’t ping or DM other devs you aren’t actively talking to",
type: EmbedType.Rich,
description: `It’s very tempting to try to get more attention to your question by @-mentioning one of the high profile(or recently active) members of Reactiflux, but please don’t. They may not actually be online, they may not be able to help, and they may be in a completely different timezone–nobody likes push notifications at 3am from an impatient stranger.
Similarly, don’t DM other members without asking first. All of the same problems as @-mentioning apply, and private conversations can’t help anyone else. Your questions are likely not unique, and other people can learn from them when they’re kept public.`,
color: EMBED_COLOR,
},
],
});
},
},
{
words: [`!inputs`],
help: `provides links to uncontrolled vs controlled components`,
category: "React/Redux",
handleMessage: (msg) => {
msg.channel.send({
embeds: [
{
title: "Uncontrolled vs Controlled components",
type: EmbedType.Rich,
description: `In React, inputs can either be uncontrolled (traditional input) or be controlled via state.
Here's an article explaining the difference between the two: https://goshakkk.name/controlled-vs-uncontrolled-inputs-react/
`,
color: EMBED_COLOR,
},
],
});
},
},
{
words: [`!move`],
help: `allows you to move the conversation to another channel \n\t(usage: \`!move #toChannel @person1 @person2 @person3\`)`,
category: "Reactiflux",
handleMessage: (msg) => {
const [, newChannel] = msg.content.split(" ");
try {
const targetChannel = msg.guild?.channels.cache.get(
newChannel.replace("<#", "").replace(">", ""),
) as TextChannel;
if (!msg.mentions.members) return;
targetChannel.send(
`${msg.author} has opened a portal from ${
msg.channel
} summoning ${msg.mentions.members.map((i) => i).join(" ")}`,
);
} catch (e) {
console.log("Something went wrong when summoning a portal: ", e);
}
},
},
{
words: [`!mdn`],
help: `allows you to search something on MDN, usage: !mdn Array.prototype.map`,
category: "Web",
handleMessage: async (msg) => {
const [, ...args] = msg.content.split(" ");
const query = args.join(" ");
const [fetchMsg, res] = await Promise.all([
msg.channel.send(`Fetching "${query}"...`),
fetch(
`https://developer.mozilla.org/api/v1/search?highlight=false&locale=en-us&q=${query}`,
),
]);
const { documents } = (await res.json()) as {
documents: (
| { title: string; excerpt: string; mdn_url: string }
| undefined
)[];
};
const [topResult] = documents;
if (!topResult) {
fetchMsg.edit(`Could not find anything on MDN for '${query}'`);
return;
}
const { title, excerpt: description, mdn_url: mdnUrl } = topResult;
await msg.channel.send({
content: "-# also did you know there's `/mdn` too",
embeds: [
{
author: {
name: "MDN",
url: "https://developer.mozilla.org",
icon_url:
"https://developer.mozilla.org/favicon-48x48.cbbd161b.png",
},
title,
description,
color: 0x83d0f2,
url: `https://developer.mozilla.org${mdnUrl}`,
},
],
});
fetchMsg.delete();
},
},
{
words: ["!react-docs", "!docs"],
help: "Allows you to search the React docs, usage: !docs useState",
category: "Web",
handleMessage: async (msg) => {
const search = extractSearchKey(msg.content);
const searchKey = getReactDocsSearchKey(search);
if (!searchKey) {
msg.channel.send({
embeds: generateReactDocsErrorEmbeds(search),
});
return;
}
const [fetchMsg, content] = await Promise.all([
msg.channel.send(`Looking up documentation for **'${search}'**...`),
getReactDocsContent(searchKey),
]);
if (!content) {
fetchMsg.edit({
embeds: generateReactDocsErrorEmbeds(search),
});
return;
}
await msg.channel.send({
embeds: [
{
title: `${searchKey}`,
type: EmbedType.Rich,
description: content,
color: EMBED_COLOR,
url: `https://react.dev/reference/${searchKey}`,
author: {
name: "React documentation",
icon_url:
"https://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/React-icon.svg/1150px-React-icon.svg.png",
url: "https://react.dev/",
},
},
],
});
fetchMsg.delete();
},
},
{
words: [`!appideas`],
help: `provides a link to the best curated app ideas for beginners to advanced devs`,
category: "Web",
handleMessage: (msg) => {
msg.channel.send({
embeds: [
{
title: "Florinpop17s Curated App Ideas!",
type: EmbedType.Rich,
description: `Sometimes it's tough finding inspiration, luckily this guy listed a bunch of stuff for you to pick from for your next project! Well sorted progression to confidence in web dev.
https://github.com/florinpop17/app-ideas
`,
color: EMBED_COLOR,
},
],
});
},
},
{
words: [`!cors`],
help: `provides a link to what CORS is and how to fix it`,
category: "Web",
handleMessage: (msg) => {
msg.channel.send({
embeds: [
{
title: "Understanding CORS",
type: EmbedType.Rich,
description: `Cross-Origin Resource Sharing (CORS) is a mechanism that lets remote servers restrict which origin (i.e your website) can access it.
Read more:
- [BTM's "Understanding CORS"](https://medium.com/@baphemot/understanding-cors-18ad6b478e2b)
- [A Guide to Cross-Origin Resource Sharing](https://auth0.com/blog/cors-tutorial-a-guide-to-cross-origin-resource-sharing/)
- [How to win at CORS](https://jakearchibald.com/2021/cors/)
`,
color: EMBED_COLOR,
},
],
});
},
},
{
words: [`!imm`, `!immutability`],
help: `provides resources for helping with immutability`,
category: "React/Redux",
handleMessage: (msg) => {
msg.channel.send({
embeds: [
{
title: "Immutable updates",
type: EmbedType.Rich,
description: `Immutable updates involve modifying data by creating new, updated objects instead of modifying the original object directly.
You should not modify existing data directly in React or Redux, as mutating data can lead to bugs.
- [Immutability in React and Redux](https://daveceddia.com/react-redux-immutability-guide/)
- [React Docs: Updating Objects in State](https://react.dev/learn/updating-objects-in-state)
- [React Docs: Updating Arrays in State](https://react.dev/learn/updating-arrays-in-state)
- [Redux Toolkit, Immutability and Redux](https://redux-toolkit.js.org/usage/immer-reducers#immutability-and-redux)
`,
color: EMBED_COLOR,
},
],
});
},
},
{
words: [`!redux`],
help: `Info and when and why to use Redux`,
category: "React/Redux",
handleMessage: (msg) => {
msg.channel.send({
embeds: [
{
title: "When should you use Redux?",
type: EmbedType.Rich,
description: `Redux is still the most widely used state management tool for React, but it's important to always ask "what problems am I trying to solve?", and choose tools that solve those problems. Redux, Context, React Query, and Apollo all solve different problems, with some overlap.
See these articles for advice on what Redux does and when it makes sense to use it:
[Redux - Not Dead Yet! (2018)](https://blog.isquaredsoftware.com/2018/03/redux-not-dead-yet/)
[Why React Context is Not a "State Management" Tool](https://blog.isquaredsoftware.com/2021/01/context-redux-differences/)
[When (and when not) to reach for Redux](https://changelog.com/posts/when-and-when-not-to-reach-for-redux)
[Idiomatic Redux](https://blog.isquaredsoftware.com/2017/05/idiomatic-redux-tao-of-redux-part-1/)
`,
color: EMBED_COLOR,
},
],
});
},
},
{
words: ["!context"],
help: `Differences between Redux and Context`,
category: "React/Redux",
handleMessage: (msg) => {
msg.channel.send({
embeds: [
{
title: "What are the differences between Redux and Context?",
type: EmbedType.Rich,
description: `Redux and Context are different tools that solve different problems, with some overlap.
Context is a Dependency Injection tool for a single value.
Redux is a tool for predictable state management outside React.
See these articles for more details on the differences:
- [Blogged Answers: Why React Context is Not a "State Management" Tool](https://blog.isquaredsoftware.com/2021/01/context-redux-differences/)
- [Blogged Answers: React, Redux, and Context Behavior](https://blog.isquaredsoftware.com/2020/01/blogged-answers-react-redux-and-context-behavior/)
- [Blogged Answers: Redux - Not Dead Yet!](https://blog.isquaredsoftware.com/2018/03/redux-not-dead-yet/)
- [When (and when not) to reach for Redux - Mark Erikson](https://changelog.com/posts/when-and-when-not-to-reach-for-redux)
`,
color: EMBED_COLOR,
},
],
});
},
},
{
words: [`!render`],
help: `Explanation of how React rendering behavior works`,
category: "React/Redux",
handleMessage: (msg) => {
msg.channel.send({
embeds: [
{
title: "How does React rendering behavior work?",
type: EmbedType.Rich,
description: `There are several common misunderstandings about how React renders components. It's important to know that:
- React re-renders components recursively by default
- State updates must be immutable
- Updates are usually batched together
- Context updates always cause components to re-render
See this post for a detailed explanation of how React rendering actually works:
https://blog.isquaredsoftware.com/2020/05/blogged-answers-a-mostly-complete-guide-to-react-rendering-behavior/
`,
color: EMBED_COLOR,
},
],
});
},
},
{
words: [`!jwt`],
help: `Describes reasoning for and against the use of JWT tokens againt using standard sessions`,
category: "Web",
handleMessage: (msg) => {
msg.channel.send({
embeds: [
{
title:
"Is JWT the right approach for my application's authentication?",
type: EmbedType.Rich,
description: `
Most of the time, JWTs aren't the best approach for working with backend authentication, despite the multitude of tutorials that use JWT. Sessions have been used for decades, with a lot of back end frameworks supporting them out of the box.
That said there are also scenarios when using a JWT token is the best approach:
- When using a third party auth service ( OpenID, Auth0, Firestore)
- Service to service calls
- Distributed architectures ( i.e Microservices)
See below to help you decide which works best for you:
- [JWT is a Bad Default - Evert Pot](https://evertpot.com/jwt-is-a-bad-default)
- [JWT are Dangerous for User Sessions - Raja Rao](https://redis.com/blog/json-web-tokens-jwt-are-dangerous-for-user-sessions)
- [Authentication Cheat Sheet - OWASP](https://cheatsheetseries.owasp.org/cheatsheets/Authentication_Cheat_Sheet.html)
- [Session Management Cheat Sheet - OWASP](https://cheatsheetseries.owasp.org/cheatsheets/Session_Management_Cheat_Sheet.html)
- [JSON Web Token Cheat Sheet for Java - OWASP](https://cheatsheetseries.owasp.org/cheatsheets/JSON_Web_Token_for_Java_Cheat_Sheet.html)
- [Gist - samsch](https://gist.github.com/samsch/a5c99b9faaac9f131967e8a6d61682b0)
`,
color: EMBED_COLOR,
},
],
});
},
},
{
words: [`!formatting`, `!prettier`],
help: `describes Prettier and explains how to use it to format code`,
category: "Reactiflux",
handleMessage: (msg) => {
msg.channel.send({
embeds: [
{
title: "Formatting code with Prettier",
type: EmbedType.Rich,
description: `Inconsistent indentation and syntax can make it more difficult to understand code, create churn from style debates, and cause logic and syntax errors.
Prettier is a modern and well-supported formatter that completely reformats your code to be more readable and follow best practices.
To format some code without installing anything, use the playground: https://prettier.io/playground/
To enforce its style in your projects, use the CLI: https://prettier.io/docs/en/install.html
To integrate it into your editor: https://prettier.io/docs/en/editors.html`,
color: EMBED_COLOR,
},
],
});
},
},
{
words: [`!gender`],
help: `reminds users to use gender-neutral language`,
category: "Communication",
handleMessage: (msg) => {
msg.channel.send({
embeds: [
{
title: "Please use gender neutral language by default",
type: EmbedType.Rich,
description: `Unless someone has made their pronouns known, please use gender neutral language.
- Instead of "hey guys," try "hey folks", "hey all", or similar
- Use "they/them/theirs" if you aren't sure of someone's pronouns
- "thanks friend" instead of "thanks man"`,
color: EMBED_COLOR,
},
],
});
},
},
{
words: [`!reactts`],
help: `Resources and tips for using React + TypeScript together`,
category: "React/Redux",
handleMessage: (msg) => {
msg.channel.send({
embeds: [
{
title: "Resources for React + TypeScript",
type: EmbedType.Rich,
description: `The best resource for how to use TypeScript and React together is the React TypeScript CheatSheet. It has advice on how to type function components, hooks, event handlers, and much more:
https://react-typescript-cheatsheet.netlify.app/
Also, we advise against using the \`React.FC\` type for function components. Instead, declare the type of \`props\` directly, like:
\`function MyComp(props: MyCompProps) {}\`:
See this issue for details on why to avoid \`React.FC\`:
https://github.com/facebook/create-react-app/pull/8177
`,
color: EMBED_COLOR,
},
],
});
},
},
{
words: [`!hooks`, `!learn`],
help: `Resources for Learning React`,
category: "React/Redux",
handleMessage: (msg) => {
msg.channel.send({
embeds: [
{
title: "Learning React",
type: EmbedType.Rich,
description: `The [official React docs](https://react.dev) are the best resource for learning React.
The [Reactiflux "Learning Resources" page](https://www.reactiflux.com/learning) has curated links for learning JS, React, Redux, and TS:`,
},
],
});
},
},
{
words: [`!nw`, `!notworking`],
help: `gives some tips on how to improve your chances at getting an answer`,
category: "Communication",
handleMessage: (msg) => {
msg.channel.send({
embeds: [
{
title: "State your problem",
type: EmbedType.Rich,
description: `To improve your chances at getting help, it's important to describe the behavior you're seeing and how it differs from your expectations. Simply saying something "doesn't work" requires too many assumptions on the helper's part, and could lead both of you astray.
Instead:
- Tell us what you're trying to do.
- Show us what you did with code.
- Tell us what happened. Show us errors. Describe what unexpected behavior you're seeing.`,
color: EMBED_COLOR,
},
],
});
},
},
{
words: [`!laptop`],
help: `gives some advice about what laptop to use for web development`,
category: "Reactiflux",
handleMessage: (msg) => {
msg.channel.send({
embeds: [
{
title: "",
type: EmbedType.Rich,
description: `Web development is generally not a highly taxing process, so the laptop you get may matter less than you think. Any operating system is fine for general web development.
A few things to consider when getting a laptop:
- Memory may be important if you plan on running containers (common as part of typical development flows) or emulators for mobile devices. Look for a laptop with at least 16GB of memory.
- Consumer-targeted laptops tend to be less repairable and receive less support over long periods of time than business laptops.
Here are a few recommendations for laptops:
- Dell Latitude or XPS
- Lenovo Thinkpad
- Apple Macbook (avoid models with butterfly keyboards)
These lines are popular so there's generally a lot of resources for working on them; their makers have refurbished stores and they are also widely available used if you're on a budget.
`,
color: EMBED_COLOR,
},
],
});
},
},
{
words: ["!lock"],
help: "",
category: "Communication",
handleMessage: async (msg) => {
if (!msg.guild || !isStaff(msg.member)) {
return;
}
// permission overwrites can only be applied on Guild Text Channels
if (msg.channel.type === ChannelType.GuildText) {
const { channel: guildTextChannel } = msg;
await guildTextChannel.permissionOverwrites.create(
guildTextChannel.guild.roles.everyone,
{
SendMessages: false,
CreatePublicThreads: false,
CreatePrivateThreads: false,
SendMessagesInThreads: false,
},
);
guildTextChannel.send("This channel has been locked by a moderator");
}
},
},
{
words: ["!unlock"],
help: "",
category: "Communication",
handleMessage: async (msg) => {
if (!msg.guild || !isStaff(msg.member)) {
return;
}
// permission overwrites can only be applied on Guild Text Channels
if (msg.channel.type === ChannelType.GuildText) {
const { channel: guildTextChannel } = msg;
guildTextChannel.permissionOverwrites.create(
guildTextChannel.guild.roles.everyone,
{
SendMessages: null, // null will inherit the permission from the parent channel category
},
);
}
},
},
{
words: [`!const`],
help: `explains that const values are not immutable`,
category: "Web",
handleMessage: (msg) => {
msg.channel.send({
embeds: [
{
title: "Using const variables",
type: EmbedType.Rich,
description: `As its name suggests, you cannot reassign a const variable.
\`\`\`js
const a = 'hello';
a = 'world'; // ❌ Uncaught TypeError: Assignment to constant variable.
\`\`\`
However, that does not mean the value of a const variable is immutable.
For example, you could mutate an object's property
\`\`\`js
const obj = {
hello: 'world'
};
obj.hello = 'reactiflux'; // ✅
\`\`\`
or you could mutate an array's elements
\`\`\`js
const arr = ['hello'];
arr.push('world'); // ✅
arr[1] = 'reactiflux'; // ✅
console.log(arr); // ['hello', 'reactiflux']