1
+ //! A [Guild]'s preferences (aka: configuration)
2
+ //!
3
+ //! Botanist is shipped with many features, some of which
4
+ //! work out of the box (ex: `ping`). Other on the other hand
5
+ //! rely on guild-specific datum. This data as a whole is called
6
+ //! the [Guild]'s preferences or configuration.
7
+ //! It notably includes the privilege (see [`Privilege`]) system
8
+ //! but also conviniences such as welcome messages, administration
9
+ //! channels or advertisement policy.
10
+ //!
11
+ //! [Guild]: serenity::model::guild::Guild
12
+
1
13
use crate :: as_pg_array;
2
14
use async_recursion:: async_recursion;
3
15
use serenity:: model:: id:: { ChannelId , GuildId , RoleId } ;
4
16
use sqlx:: { query, Executor , Postgres , Row } ;
5
17
use std:: convert:: TryFrom ;
6
18
use thiserror:: Error ;
7
19
8
- /// Errors originating from the GuildConfig wrapper
20
+ /// Errors originating from the ` GuildConfig` wrapper
9
21
#[ derive( Error , Debug ) ]
10
22
pub enum GuildConfigError {
11
23
#[ error( "`{field:?}` can't be over 2000 chracters" ) ]
@@ -22,8 +34,27 @@ type Result<Return> = std::result::Result<Return, GuildConfigError>;
22
34
23
35
/// Wraps around a `guilds` row
24
36
///
25
- /// Most methods returning a [`std::result::Result`] do so only because the query to the DB may fail
26
- /// If another reason may cause it to fail, it will be documented
37
+ /// [`GuildConfig`] provides an API covering every common use-case. When it doesn't piecing methods
38
+ /// together is simple and safe. As much as possible it tries to issue as little queries as possible
39
+ /// so you can confidently use the methods.
40
+ ///
41
+ /// # Connection (`conn`) parameter.
42
+ ///
43
+ /// For the sake of flexibility as little constraints as possible were put on the methods.
44
+ /// The major example of this is the `conn` parameter which generally accepts anything that
45
+ /// implements: [`sqlx::Executor`]. This means both [`sqlx::PgPool`] and
46
+ /// [`sqlx::PgConnection`] can be used. However some methods need to issue multiple queries.
47
+ /// As such it requires a `conn` that implements [`Copy`]. In those cases simply pass a `&PgPool`
48
+ ///
49
+ /// # Errors
50
+ ///
51
+ /// For simplicty's sake only methods that can give other errors than [`sqlx::Error`] have a section
52
+ /// detailing the error.
53
+ ///
54
+ /// All methods provided by [`Self`] return a `Result` which's [`Err`] variant is
55
+ /// [`GuildConfigError`]. One of the later's variant wraps around [`sqlx::Error`] which is returned by
56
+ /// every [`sqlx`] method that interacts with the database. These are all about database errors, which for the
57
+ /// user of the library, should only be caused by incorrect setup (see [`crate`]).
27
58
#[ derive( Debug ) ]
28
59
pub struct GuildConfig ( GuildId ) ;
29
60
@@ -56,7 +87,7 @@ impl GuildConfig {
56
87
///
57
88
/// # Errors
58
89
///
59
- /// Errors if a row with the same `id` already exists in the DB
90
+ /// Errors with [`GuildConfigError::AlreadyExists`] if a row with the same `id` already exists in the DB
60
91
pub async fn new < ' a , ' b , PgExec : Executor < ' a , Database = Postgres > + Copy > (
61
92
conn : PgExec ,
62
93
builder : GuildConfigBuilder < ' b > ,
@@ -450,6 +481,13 @@ impl ToString for Privilege {
450
481
}
451
482
}
452
483
484
+ /// Builder for new configuration entries
485
+ ///
486
+ /// This should only be used when the bot joins a new [Guild].
487
+ /// This builder is used to quickly whip up a new configuration with sensible defaults
488
+ /// that can be easilly overriden. For how to use see [`GuildConfig::new()`] and the tests.
489
+ ///
490
+ /// [Guild]: serenity::model::guild::Guild`
453
491
#[ derive( Debug ) ]
454
492
pub struct GuildConfigBuilder < ' a > {
455
493
id : GuildId ,
0 commit comments