Skip to content

Commit

Permalink
begin refactoring to our namespace and code style
Browse files Browse the repository at this point in the history
  • Loading branch information
braindigitalis committed Sep 26, 2024
1 parent bebe921 commit 0d6aff8
Show file tree
Hide file tree
Showing 62 changed files with 457 additions and 514 deletions.
2 changes: 1 addition & 1 deletion buildtools/make_vcpkg.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
$vcpkg->constructPortAndVersionFile()
);
if (!empty($sha512)) {
/* Now check out master */
/* now check out master */
if (!$vcpkg->checkoutRepository()) {
exit(1);
}
Expand Down
2 changes: 1 addition & 1 deletion docpages/advanced_reference/automating-with-jenkins.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Making sure you have your project files in the workspace directory (you can see

Running the builds is the same as any other time, but we'll still cover it! However, we won't cover running it in background and whatnot, that part is completely down to you.

First, you need to get into the `jenkins` user. If you're like me and don't have the Jenkins user password, you can login with your normal login (that has sudo perms) and do `sudo su - jenkins`. Once logged in, you'll be in `/var/lib/jenkins/`. From here, you'll want to do `cd workspace/DiscordBot/` (make sure to replace "DiscordBot" with your bot's name. Remember, you can tab-complete this) and then `cd build`. Now, you can simply do `./DiscordBot`!
First, you need to get into the `jenkins` user. If you're like me and don't have the Jenkins user password, you can login with your normal login (that has sudo perms) and do `sudo su - jenkins`. Once logged in, you'll be in `/var/lib/jenkins/`. From here, you'll want to do `cd workspace/DiscordBot/` (make sure to replace "DiscordBot" with your bot's name. Remember, you can tab-complete this) and then `cd build`. now, you can simply do `./DiscordBot`!

\warning If you are going to be running the bot at the same time as builds, I would heavily advise that you copy the bot (if it's not statically linked, then copy the entire build directory) to a different location. This is so you can pick and choose when the bot gets updated (and even means you can run experimental builds as opposed to stable builds) but also means you avoid any risk of the bot crashing during build (as Jenkins will be overwriting your executable and libraries).

Expand Down
2 changes: 1 addition & 1 deletion docpages/advanced_reference/lambdas_and_locals.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ It is important to remember that when you put a lambda callback onto a function

To explain this situation and how it causes issues, I'd like you to imagine the age-old magic trick, where a magician sets a fine table full of cutlery, pots, pans and wine. He indicates to the audience that this is authentic, then with a whip of his wrist, he whips the tablecloth away, leaving the cutlery and other tableware in place (if he is any good as a magician!)

Now imagine the following code scenario. We will describe this code scenario as the magic trick above, in the steps below:
now imagine the following code scenario. We will describe this code scenario as the magic trick above, in the steps below:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~cpp
bot.on_message_create([&bot](const dpp::message_create_t & event) {
Expand Down
2 changes: 1 addition & 1 deletion docpages/advanced_reference/separate-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,4 @@ int main() {

And there we go! How tidy is that?

Now, the possibilities to this are not limited. If you wish to do this twice (as I explained at first), you can simply have another class and just copy the `bot.on_message_create` line below in the `main.cpp` file and then you can change it to reference the second class, meaning you have two message events firing in two separate classes!
now, the possibilities to this are not limited. If you wish to do this twice (as I explained at first), you can simply have another class and just copy the `bot.on_message_create` line below in the `main.cpp` file and then you can change it to reference the second class, meaning you have two message events firing in two separate classes!
2 changes: 1 addition & 1 deletion docpages/advanced_reference/voice_model.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ digraph "Example Directory" {
"Discord" -> "HTTP/1.1 204 No Content...";
"Discord" -> "HTTP/1.1 101 Switching Protocols";
label = "Now, we're waiting for a response from Discord.\nIf we receive 204, we'll start initiating voiceconn. However, if we receive 101, then we can do all the voice stuff.";
label = "now, we're waiting for a response from Discord.\nIf we receive 204, we'll start initiating voiceconn. However, if we receive 101, then we can do all the voice stuff.";
}

subgraph cluster_4 {
Expand Down
2 changes: 1 addition & 1 deletion docpages/example_programs/misc/making_threads.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ After that, you'll be able to see your bot send a message in your thread!

Those of you who are familar with sending messages in regular channels may have also noticed that sending messages to threads is the same as sending a general message. This is because threads are basically channels with a couple more features!

Now, we're going to cover how to lock a thread! With this, you'll also learn how to edit threads in general, meaning you can go forward and learn how to change even more stuff about threads, as much as your heart desires!
now, we're going to cover how to lock a thread! With this, you'll also learn how to edit threads in general, meaning you can go forward and learn how to change even more stuff about threads, as much as your heart desires!

\include{cpp} making_threads3.cpp

Expand Down
2 changes: 1 addition & 1 deletion docpages/example_programs/misc/setting_status.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ If all went well, your bot should now be online and say this on members list!
If you want to make your bot show as Do Not Disturb, then you could change dpp::ps_online to dpp::ps_dnd.
You can also play around with dpp::at_game, changing it to something like dpp::at_custom or dpp::at_listening!

Now, let's cover setting the bot status to say `Playing with x guilds!` every two minutes.
now, let's cover setting the bot status to say `Playing with x guilds!` every two minutes.

\note This example uses timers to update the status every 2 minutes. If you aren't familiar with D++'s own timers, please read \ref using_timers "this page on timers" before you continue.

Expand Down
2 changes: 1 addition & 1 deletion docpages/example_programs/misc/using-emojis.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ First - Sending emojis. You have to use its mention, which depends on the type.

\include{cpp} using_emojis1.cpp

Now, our bot will send our epic emojis!
now, our bot will send our epic emojis!

\image html using_emojis1.png

Expand Down
2 changes: 1 addition & 1 deletion docpages/example_programs/misc/using_timers.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Now, let's make the same timer a one-shot timer, meaning it will only run once!

\include{cpp} timers_example2.cpp

Great! Now we've learnt the basics of timers and how to stop them!
Great! now we've learnt the basics of timers and how to stop them!

To finish off, let's make a timer that you can start and stop with commands. This example will store the timer in a map where the user is the owner of the timer!

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ After editing the channel:
\image html stuff_edit4.png

## Editing embeds
Now let's send an embed and edit it. If a message has one `content` field, it can have a few `embed` fields, up to 10 to be precise. So we first get the embed we want and edit and change its description.
now let's send an embed and edit it. If a message has one `content` field, it can have a few `embed` fields, up to 10 to be precise. So we first get the embed we want and edit and change its description.

\include{cpp} editing_messages3.cpp

Expand Down
2 changes: 1 addition & 1 deletion docpages/example_programs/the_basics/private-messaging.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Sometimes it's simply not enough to ping someone in a server with a message, and

\include{cpp} private_messaging.cpp

That's it! Now, you should have something like this:
That's it! now, you should have something like this:

\image html privatemessageexample.png
\image html privatemessageexample2.png
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
\page callback-functions Using Callback Functions

When you create or get an object from Discord, you send the request to its API and in return you get either an error or the object you requested/created. You can pass a function to API calls as the callback function. This means that when the request completes, and you get a response from the API, your callback function executes. You must be careful with lambda captures! Good practice would be not capturing variables by reference unless you have to, since when the request completes and the function executes, the variables can already be destructed. Advanced reference can be found [here](https://dpp.dev/lambdas-and-locals.html). Now, let's see callback functions in action:
When you create or get an object from Discord, you send the request to its API and in return you get either an error or the object you requested/created. You can pass a function to API calls as the callback function. This means that when the request completes, and you get a response from the API, your callback function executes. You must be careful with lambda captures! Good practice would be not capturing variables by reference unless you have to, since when the request completes and the function executes, the variables can already be destructed. Advanced reference can be found [here](https://dpp.dev/lambdas-and-locals.html). now, let's see callback functions in action:

\include{cpp} callbacks.cpp

Expand Down
2 changes: 1 addition & 1 deletion docpages/install/install-windows-vs-zip.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ To add D++ to a Visual Studio project, using **Visual Studio 2019** or **Visual
12. Again within the same window, go to the input section, under the linker category, and add '**dpp.lib;**' to the start of the libraries to include, as shown below:
\image html zip_vsproj_11.png

13. Now you can paste some code into the editor, completely replacing the 'hello world' application that Visual Studio made for you. The example code here is the basic bot from the first example on this site. You should at this point also double check that the architecture you have selected (in this case x86) matches the version of the dll/lib files you downloaded from the website. This is **important** as if you mismatch them the compilation will just fail.
13. now you can paste some code into the editor, completely replacing the 'hello world' application that Visual Studio made for you. The example code here is the basic bot from the first example on this site. You should at this point also double check that the architecture you have selected (in this case x86) matches the version of the dll/lib files you downloaded from the website. This is **important** as if you mismatch them the compilation will just fail.
\image html zip_vsproj_12.png

14. Go to the build menu and choose Build Solution (A handy shortcut for this is to just press **F7**):
Expand Down
2 changes: 1 addition & 1 deletion docpages/make_a_bot/clion.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Now, you can open your `main.cpp` file. If you have code there, then you're one

\include{cpp} firstbot.cpp

Now, you can go ahead and hit the green "Run" button in the top-right to run the bot.
now, you can go ahead and hit the green "Run" button in the top-right to run the bot.

**Congratulations, you've successfully set up a bot!**

Expand Down
2 changes: 1 addition & 1 deletion docpages/make_a_bot/cmake.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ find_package_handle_standard_args(DPP DEFAULT_MSG DPP_LIBRARIES DPP_INCLUDE_DIR)

## 4. Build the bot.

Now that we have our all our cmake stuff setup and we've got our code in place, we can initalise CMake. You'll want to go inside the `build/` directory and do `cmake ..`.
now that we have our all our cmake stuff setup and we've got our code in place, we can initalise CMake. You'll want to go inside the `build/` directory and do `cmake ..`.

Once that's completed, you'll want to head back to your up-most folder (where all the folders are for your bot) and run `cmake --build build/ -j4` (replace -j4 with however many threads you want to use). This will start compiling your bot and creating the executable.

Expand Down
2 changes: 1 addition & 1 deletion docpages/make_a_bot/meson.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ meson setup builddir
meson compile -C builddir
```

Now, your Meson project should be all setup!
now, your Meson project should be all setup!

**Have fun!**
2 changes: 1 addition & 1 deletion docpages/make_a_bot/replit.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ g++ -o bot main.cpp -ldpp -lpthread -L./usr/lib -I./usr/include -std=c++17
LD_PRELOAD=./usr/lib/libdpp.so ./bot
```

Now that your bot is running, you have to keep it online. Replit automatically puts repls to sleep after some time, so you will need to ping a webserver. Unfortunately, Replit is sometimes limiting, and this is one of the only free workarounds to this issue.
now that your bot is running, you have to keep it online. Replit automatically puts repls to sleep after some time, so you will need to ping a webserver. Unfortunately, Replit is sometimes limiting, and this is one of the only free workarounds to this issue.

1. Start a http server. This can be through any webserver, but as a simple solution, use python's built in http.server:
```bash
Expand Down
2 changes: 1 addition & 1 deletion docpages/make_a_bot/token.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ To create a new application, take the steps as follows:
2. Next, enter a name for the application in the pop-up and press the "Create" button.
\image html create_application_confirm_popup.png
In this example we named it "D++ Test Bot".
3. Move on by click the "Bot" tab in the left-hand side of the screen. Now click the "Add Bot" button on the right and confirm that you want to add the bot to your application.
3. Move on by click the "Bot" tab in the left-hand side of the screen. now click the "Add Bot" button on the right and confirm that you want to add the bot to your application.
\image html create_application_add_bot.png
On the resulting screen, you’ll note a page with information regarding your new bot. You can edit your bot name, description, and avatar here if you want to. If you wish to read the message content from messages, you need to enable the message content intent in the "Privileged Gateway Intents" section.
\image html create_application_bot_overview.png
Expand Down
2 changes: 1 addition & 1 deletion docpages/make_a_bot/windows_wsl.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ This tutorial teaches you how to create a lightweight environment for D++ develo
6. Congratulations, you've successfully installed all dependencies! Now comes the real fun: Setting up the environment! For this tutorial we'll use a as small as possible setup, so you might create a more advanced one for production bots.
7. Create a new directory, inside your home directory, using `mkdir MyBot`. Then, you want to open that directory using `cd MyBot`.
8. Now that you've a directory to work in, type `touch mybot.cxx` to create a file you can work in!
9. Now, head on over to Visual Studio Code. Press `CTRL+SHIFT+P` and type `Remote-WSL: New WSL Window` (You don't have to type all of it, it will auto-suggest it!). This will bring up a new window. In the new window, choose `open folder` and choose the directory you've created prior (It should be within your home directory). Press OK and now you have your Folder opened as a Workspace!
9. now, head on over to Visual Studio Code. Press `CTRL+SHIFT+P` and type `Remote-WSL: New WSL Window` (You don't have to type all of it, it will auto-suggest it!). This will bring up a new window. In the new window, choose `open folder` and choose the directory you've created prior (It should be within your home directory). Press OK and now you have your Folder opened as a Workspace!
10. Add code to your CXX file (We suggest using the \ref firstbot "first bot page" if this is your first time!) and compile it by running `g++ -std=c++17 *.cxx -o bot -ldpp` in the same folder as your cxx file. This will create a "bot" file!
11. You can now start your bot by typing `./bot`!

Expand Down
2 changes: 1 addition & 1 deletion include/dpp/wsclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ enum ws_state : uint8_t {
HTTP_HEADERS,

/**
* @brief Connected as a websocket, and "upgraded". Now talking using binary frames.
* @brief Connected as a websocket, and "upgraded". now talking using binary frames.
*/
CONNECTED
};
Expand Down
21 changes: 10 additions & 11 deletions src/dpp/dave/array_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
#include <cassert>
#include <vector>

namespace discord {
namespace dave {
namespace dpp::dave {

template <typename T>
class ArrayView {
class array_view {
public:
ArrayView() = default;
ArrayView(T* data, size_t size)
array_view() = default;
array_view(T* data, size_t size)
: data_(data)
, size_(size)
{
Expand All @@ -28,16 +27,16 @@ class ArrayView {
};

template <typename T>
inline ArrayView<T> MakeArrayView(T* data, size_t size)
inline array_view<T> make_array_view(T* data, size_t size)
{
return ArrayView<T>(data, size);
return array_view<T>(data, size);
}

template <typename T>
inline ArrayView<T> MakeArrayView(std::vector<T>& data)
inline array_view<T> make_array_view(std::vector<T>& data)
{
return ArrayView<T>(data.data(), data.size());
return array_view<T>(data.data(), data.size());
}

} // namespace dave
} // namespace discord
} // namespace dpp::dave

13 changes: 13 additions & 0 deletions src/dpp/dave/cipher_interface.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "cipher_interface.h"
#include "openssl_aead_cipher.h"

namespace dpp::dave {

std::unique_ptr<cipher_interface> create_cipher(const EncryptionKey& encryptionKey)
{
auto cipher = std::make_unique<openssl_aead_cipher>(encryptionKey);
return cipher->IsValid() ? std::move(cipher) : nullptr;
}

} // namespace dpp::dave

29 changes: 29 additions & 0 deletions src/dpp/dave/cipher_interface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include <memory>

#include "common.h"
#include "array_view.h"

namespace dpp::dave {

class cipher_interface {
public:
virtual ~cipher_interface() = default;

virtual bool Encrypt(array_view<uint8_t> ciphertextBufferOut,
array_view<const uint8_t> plaintextBuffer,
array_view<const uint8_t> nonceBuffer,
array_view<const uint8_t> additionalData,
array_view<uint8_t> tagBufferOut) = 0;
virtual bool Decrypt(array_view<uint8_t> plaintextBufferOut,
array_view<const uint8_t> ciphertextBuffer,
array_view<const uint8_t> tagBuffer,
array_view<const uint8_t> nonceBuffer,
array_view<const uint8_t> additionalData) = 0;
};

std::unique_ptr<cipher_interface> create_cipher(const EncryptionKey& encryptionKey);

} // namespace dpp::dave

23 changes: 11 additions & 12 deletions src/dpp/dave/clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@

#include <chrono>

namespace discord {
namespace dave {
namespace dpp::dave {

class IClock {
class clock_interface {
public:
using BaseClock = std::chrono::steady_clock;
using TimePoint = BaseClock::time_point;
using Duration = BaseClock::duration;
using base_clock = std::chrono::steady_clock;
using time_point = base_clock::time_point;
using clock_duration = base_clock::duration;

virtual ~IClock() = default;
virtual TimePoint Now() const = 0;
virtual ~clock_interface() = default;
virtual time_point now() const = 0;
};

class Clock : public IClock {
class Clock : public clock_interface {
public:
TimePoint Now() const override { return BaseClock::now(); }
time_point now() const override { return base_clock::now(); }
};

} // namespace dave
} // namespace discord
} // namespace dpp::dave

Loading

0 comments on commit 0d6aff8

Please sign in to comment.