스킵 구현

This commit is contained in:
2023-12-20 19:45:25 +09:00
parent 6503fd167b
commit 8a987320e0
775 changed files with 162601 additions and 135 deletions

View File

@@ -0,0 +1,21 @@
\page attach-file Attaching a File to a Message
To attach a local file to an message, you can use dpp::utility::read_file. It's a helper function from D++ that allows you to read the file's content and sent it to discord.
An example of this:
\include{cpp} attachments1.cpp
Attachments via URL aren't possible. But there's a workaround for this! You can download the file and then attach it to the message.
Amazingly, D++ also has a function for this! You can use dpp::cluster::request to make HTTP requests, allowing you to go ahead and pull the content from a URL.
The following example program shows how to request a file and attach it to a message:
\include{cpp} attachments2.cpp
Here's another example of how to add a local image to an embed.
Upload the image in the same message as the embed and then reference it in the embed.
\include{cpp} attachments3.cpp

View File

@@ -0,0 +1,11 @@
\page detecting-messages Listening to messages
Sometimes, you may want to listen out for a message, rather than a command. This could be used for many cases like a spam filter, a bot that would respond to movie quotes with gifs, or a chat bot! However, in this page, we'll be using this to create a moderation filter (detect bad words).
\warning As of August 30th, 2022, Discord made Message Content a privileged intent. Whilst this means you can still use prefixed messages as commands, Discord does not encourage this and heavily suggests you use \ref slashcommands "slash commands". If you wish to create commands, use \ref slashcommands "slash commands", not messages.
\include{cpp} detecting_messages.cpp
If all went well, you should have something like this!
\image html badwordfilter.png

View File

@@ -0,0 +1,15 @@
\page editing-channels-and-messages Editing Channels and Messages
Sometimes we need to update an object, such as message or channel. At first, it might seem confusing, but it's actually really simple! You just need to use an object with identical properties you don't need to update. NOTE: your bot can't edit messages sent by others.
\note This example uses callback functions. To see more information about them, visit \ref callback-functions.
\include{cpp} editing_messages.cpp
Before editing:
\image html stuff_edit1.png
After editing:
\image html stuff_edit2.png

View File

@@ -0,0 +1,9 @@
\page embed-message Sending Embeds
If you've been in a server and used a bot or even seen a message from a bot, you might have seen a special message type, often sent by these bots. These are called embeds! In this section, we will show how to create an embed and reply to a user, using our newly created embed!
\include{cpp} embeds.cpp
The code will send the following message.
\image html embed.png

View File

@@ -0,0 +1,124 @@
\page firstbot Creating Your First Bot
In this example we will create a C++ version of the [discord.js](https://discord.js.org/#/) example program.
The two programs can be seen side by side below:
<table>
<tr>
<th>D++</th>
<th>Discord.js</td>
</tr>
<tr>
<td>
\include{cpp} firstbot.cpp
</td>
<td>
~~~~~~~~~~~~~~~js
let Discord = require('discord.js');
let BOT_TOKEN = 'add your token here';
let bot = new Discord.Client({ intents: [] });
bot.on('interactionCreate', (interaction) => {
if (interaction.isCommand() && interaction.commandName === 'ping') {
interaction.reply({content: 'Pong!'});
}
});
bot.once('ready', async () => {
await client.commands.create({
name: 'ping',
description: "Ping pong!"
});
});
bot.login(BOT_TOKEN);
~~~~~~~~~~~~~~~
</td>
</tr>
</table>
Let's break this program down step by step:
## 1. Start with an empty C++ program
Make sure to include the header file for the D++ library with the instruction `#include <dpp/dpp.h>`!
\include{cpp} firstbot1.cpp
## 2. Create an instance of dpp::cluster
To make use of the library you must create a dpp::cluster object. This object is the main object in your program like the `Discord.Client` object in Discord.js.
You can instantiate this class as shown below. Remember to put your bot token in the constant!
\include{cpp} firstbot2.cpp
## 3. Attach to an event
To have a bot that does something, you should attach to some events. Let's start by attaching to the `on_ready` event (dpp::cluster::on_ready) which will notify your program when the bot is connected. In this event, we will register a slash command called 'ping'. Note that we must wrap our registration of the command in a template called dpp::run_once which prevents it from being re-run every time your bot does a full reconnection (e.g. if the connection fails).
\include{cpp} firstbot3.cpp
## 4. Attach to another event to receive slash commands
If you want to handle a slash command, you should also attach your program to the `on_slashcommand` event (dpp::cluster::on_slashcommand) which is basically the same as the Discord.js `interactionCreate` event. Lets add this to the program before the `on_ready` event:
\include{cpp} firstbot4.cpp
## 5. Add some content to the events
Attaching to an event is a good start, but to make a bot you should actually put some program code into the interaction event. We will add some code to the `on_slashcommand` to look for our slash command '/ping' and reply with `Pong!`:
\include{cpp} firstbot5.cpp
Let's break down the code in the `on_slashcommand` event so that we can discuss what it is doing:
~~~~~~~~~~~~~~~~~~~~~~~cpp
bot.on_slashcommand([](const dpp::slashcommand_t& event) {
if (event.command.get_command_name() == "ping") {
event.reply("Pong!");
}
});
~~~~~~~~~~~~~~~~~~~~~~~
This code is simply comparing the command name `event.command.get_command_name()` (dpp::interaction::get_command_name) against the value in a constant string value `"ping"`. If they match, then the `event.reply` method is called.
The `event.reply` function (dpp::slashcommand_t::reply) replies to a slash command with a message. There are many ways to call this function to send embed messages, upload files, and more, but for this simple demonstration we will just send some message text.
## 6. Add code to start the bot!
To make the bot start, we must call the dpp::cluster::start method, e.g. in our program by using `bot.start(dpp::st_wait)`.
We also add a line to tell the library to output all its log information to the console, `bot.on_log(dpp::utility::cout_logger());` - if you wanted to do something more advanced, you can replace this parameter with a lambda just like all other events.
The parameter which we set to false indicates if the function should return once all shards are created. Passing dpp::st_wait here tells the program you do not need to do anything once `bot.start` is called.
\include{cpp} firstbot6.cpp
## 7. Compile and run your bot
Compile your bot using `g++ -std=c++17 -o bot bot.cpp -ldpp` (if your .cpp file is called `bot.cpp`) and run it with `./bot`.
## 8. Inviting your bot to your server
When you invite your bot, you must use the `applications.commands` and `bots` scopes to ensure your bot can create guild slash commands. For example:
```url
https://discord.com/oauth2/authorize?client_id=YOUR-BOTS-ID-HERE&scope=bot+applications.commands&permissions=BOT-PERMISSIONS-HERE
```
Replace `YOUR-BOTS-ID-HERE` with your bot's user ID, and `BOT-PERMISSIONS-HERE` with the permissions your bot requires.
**Congratulations** - you now have a working bot using the D++ library!

View File

@@ -0,0 +1,12 @@
\page private-messaging Sending private messages
Sometimes it's simply not enough to ping someone in a server with a message, and we get that. That's why you can private message people! This tutorial will cover how to make a command that will either message the author of the command or message a specified user!
\note This tutorial makes use of callbacks. For more information about that, visit \ref callback-functions "Using Callback Functions".
\include{cpp} private_messaging.cpp
That's it! Now, you should have something like this:
\image html privatemessageexample.png
\image html privatemessageexample2.png

View File

@@ -0,0 +1,18 @@
\page using-cache Using Cache
Sometimes you may need information that is not directly available in the event callback object.
To handle this DPP maintains a cache of commonly used data for you.
@note As of August 30th, 2022, Discord made Guild Members a privileged intent. You must have GUILD_MEMBERS intent enabled for your app from discord developers portal to be able to look for members in cache.
Below is an example showing how to get a user from the cache
\include{cpp} using_cache.cpp
DPP caches more than just users, which you can get using the below-mentioned functions:
- `dpp::find_role()`
- `dpp::find_channel()`
- `dpp::find_emoji()`
- `dpp::find_guild()`
- `dpp::find_guild_member()`

View File

@@ -0,0 +1,9 @@
\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:
\include{cpp} callbacks.cpp
This is the result:
\image html callback_functions.png

View File

@@ -0,0 +1,9 @@
\page webhooks Webhooks
Webhooks are a simple way to post messages from other apps and websites into Discord. They allow getting automated messages and data updates sent to a text channel in your server. [Read more](https://support.discord.com/hc/en-us/articles/228383668) in this article about Webhooks.
The following code shows how to send messages in a channel using a webhook.
\include{cpp} webhooks.cpp
The above is just a very simple example. You can also send embed messages. All you have to do is to add an embed to the message you want to send. If you want to, you can also send it into a thread.