1 /**
2   The top API abstraction encompassing REST, WS/Gateway, and state tracking.
3 */
4 module dcord.client;
5 
6 import std.stdio;
7 
8 public import std.experimental.logger;
9 
10 import std.algorithm.iteration;
11 
12 import dcord.api,
13        dcord.types,
14        dcord.state,
15        dcord.gateway,
16        dcord.util.emitter;
17 
18 
19 /**
20   Struct containing configuration for Gateway sharding
21 */
22 struct ShardInfo {
23   /** The number of the current shard */
24   ushort shard = 0;
25 
26   /** Total number of shards */
27   ushort numShards = 1;
28 }
29 
30 @JSONIgnore
31 class Client {
32   /** Base log */
33   Logger log;
34 
35   /** Bot Authentication token */
36   string token;
37 
38   /** Configuration for sharding */
39   ShardInfo* shardInfo;
40 
41   /** APIClient instance */
42   APIClient api;
43 
44   /** GatewayClient instance */
45   GatewayClient gw;
46 
47   /** State instance */
48   State state;
49 
50   /** Emitter for gateway events */
51   Emitter events;
52 
53   /** Initializer */
54   this(string token, LogLevel lvl=LogLevel.all, ShardInfo* shardInfo = null) {
55     this.log = new FileLogger(stdout, lvl);
56     this.token = token;
57     this.shardInfo = shardInfo ? shardInfo : new ShardInfo();
58 
59     this.api = new APIClient(this);
60     this.gw = new GatewayClient(this);
61     this.state = new State(this);
62   }
63 
64   /**
65     Returns the current user.
66   */
67   @property User me() {
68     return this.state.me;
69   }
70 
71   /**
72     Gets an array of messages for a given channel.
73 
74     Params:
75       channelID = the channelID all the messages originate from.
76       limit = the number of messages to retrieve.
77       msgID = the message which other messages are selected, with respect to the filter
78       filter = get messages before, around, or after the supplied msgID
79   */
80   Message[] getMessages(Snowflake channelID, uint limit = 100, Snowflake msgID = 0, MessageFilter filter = MessageFilter.BEFORE) {
81     return this.api.channelsMessagesList(channelID, limit, filter, msgID);
82   }
83 
84   /**
85     Deletes an array of messages for a given channel, properly bulking them
86     if required.
87 
88     Params:
89       channelID = the channelID all the messages originate from.
90       messages = the array of messages.
91   */
92   void deleteMessages(Snowflake channelID, Message[] messages) {
93     Snowflake[] msgIDs;
94 
95     foreach(message; messages){
96       msgIDs ~= message.id;
97     }
98 
99     return deleteMessages(channelID, msgIDs);
100   }
101 
102   /**
103     Deletes an array of message IDs for a given channel, properly bulking them
104     if required.
105 
106     Params:
107       channelID = the channelID all the messages originate from
108       msgIDs = the array of message IDs
109   */
110   void deleteMessages(Snowflake channelID, Snowflake[] msgIDs) {
111     if (msgIDs.length <= 2) {
112       msgIDs.each!(x => this.api.channelsMessagesDelete(channelID, x));
113     } else {
114       this.api.channelsMessagesDeleteBulk(channelID, msgIDs);
115     }
116   }
117 
118 }