aboutsummaryrefslogtreecommitdiff
path: root/articles/2020-11-13-systems-languages-for-discord-bots.md
blob: b6a0ea8cbd328655749de41846c92edfa9e1a000 (plain) (blame)
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
---
title: Systems languages for Discord bots
route: /systems-languages-for-discord-bots
date: 2020-11-13
description: How writing a Discord bot in a systems level language is not as hard as you might think.
---

You've read the title, now your probably confused, don't worry, it's not that hard.

Typically, you'd expect to write a Discord bot in something more *abstracted* like [Python](https://www.python.org/) or [Node.js](https://nodejs.org/en/), however, using [Rust](https://www.rust-lang.org/) for a Discord bot is amazing.

Believe me, I've had my fair share of Node.js Discord bots, I've tried [Discord.js](https://discord.js.org/#/), [Akairo](https://discord-akairo.github.io/#/), [Eris](https://abal.moe/Eris/), all being various NPM packages for making Discord bots. However, they're all held back by the fact that JavaScript is a single-threaded language, not even mentioning the slowness of interpretation.
*Also Python bots, but we don't talk about that...*

When you're making a ***hopefully scalable***, Discord bot, speed needs to be something you factor in. But in Rust, that's not a problem, not only does Rust have blazing fast performance, on the level of C, implementing multi-threaded capability to a application is insanely simple, meaning if you need to do some heavy calculating but don't want an inherent slowdown for the rest of the users, just spin up a new thread.

As well, despite Rust being a systems level language, and systems languages commonly being attributed to being *difficult* or *taking a long time to learn*, Rust is a very modern language with ease in mind. If you are coming from something like [TypeScript](https://www.typescriptlang.org/), Rust will be a piece of cake.

For instance, if I want to implement a for-loop which only prints an output when the numbers are either 3, 6, or 9, in TypeScript, I could do;

# Input
```ts
for (let i = 0; i < 10; i++) {
  switch (i) {
    3: console.log("3"); break;
    6: console.log("6"); break;
    9: console.log("9"); break;
  }
}
```
# Output
```sh
> node example.js
3
6
9
```

*Very un-optimized, just making it simple for the example.*

You could now do the same thing in Rust like so;

# Input
```rust
for i in 0..10 {
  match i {
    3 => println!("3"),
    6 => println!("6");
    9 => println!("9");
    _ => { }
    // Rust is a memory safe language.
    // Meaning all possibilities have to be handled.
    // In JavaScript, this could be represented as
    // default: break;
  }
}
```

# Output
```sh
> cargo run
3
6
9
```

As you can see, they are not that far off. As well, Rust has the advantage of having an amazing compiler, some might call it obtrusive, or a love-hate relationship, but it's only there to help you. No more runtime errors (arguable), because it catches all syntactical errors, and *most* semantic errors. If it catches **any** errors, it simply won't compile, most of the time it even gives you the solution!

In another comparison, typing is **insanely** similar, take a look at this.

# Example One
```rust
let value: String = "Hello!".to_string();
```

# Example Two
```ts
let value: string = "Hello!";
```

Which one do you think is Rust, which one TypeScript? Well, example one was Rust -- ***somewhat obviously***, but you see, the typing system is very similar, so you would catch on in no time.

<small>If you would like more information on either of the typings, here is a list of [Rust's typings](https://doc.rust-lang.org/reference/types.html), and [TypeScript's typings](https://blog.angular-university.io/typescript-2-type-system-how-do-type-definitions-work-in-npm-when-to-use-types-and-why-what-are-compiler-opt-in-types/).</small>

Now, you might be hooked, but where to start? I suggest you take a look at [Serenity](https://github.com/serenity-rs/serenity). Serenity is a Rust crate (simular to a NPM package), that is a all-in-one, Discord wrapper for bots. They even have [Discord](https://discord.gg/9X7vCus) server which you could join if you need help!

<small>Honourable mention: [Twilight](https://github.com/twilight-rs/twilight), I do not have any experience, but I've heard it's somewhat like a simplified version of Serenity.</small>

Though I will not go into the ins and outs of Serenity for now, feel free to do some research of your own.

Also worth mentioning, since Serenity is in Rust, you can use other Rust crates to finish up some cool custom features!

Well, I hope you enjoyed this quick little write-up, I'll probably be absent for a bit as I have work that I need to finish up, but nonetheless, I hope you stay for more!