diff --git a/lib/browser.dart b/lib/browser.dart index 242ea1b..4b780b4 100644 --- a/lib/browser.dart +++ b/lib/browser.dart @@ -8,9 +8,9 @@ import 'package:http_utils/http_utils.dart' as http_utils; import 'package:http_parser/http_parser.dart' as http_parser; import 'package:http/http.dart' as http; import 'package:http/browser_client.dart' as http_browser; -//import 'package:events/events.dart' as events; part 'src/Client.dart'; +part 'src/webhook.dart'; part 'src/internal/_BaseObj.dart'; part 'src/internal/_Constants.dart'; @@ -76,9 +76,10 @@ part 'src/errors/HttpError.dart'; part 'src/errors/InvalidTokenError.dart'; part 'src/errors/InvalidShardError.dart'; +final bool _browser = true; + class _WebSocket { html.WebSocket _socket; - bool browser = true; _WebSocket(); @@ -105,7 +106,6 @@ class _WebSocket { class _HttpClient { http_browser.BrowserClient client; - bool browser = true; _HttpClient() { this.client = new http_browser.BrowserClient(); diff --git a/lib/discord.dart b/lib/discord.dart index 46e489b..e0220cd 100644 --- a/lib/discord.dart +++ b/lib/discord.dart @@ -7,10 +7,9 @@ import 'dart:collection'; import 'package:http_utils/http_utils.dart' as http_utils; import 'package:http_parser/http_parser.dart' as http_parser; import 'package:http/http.dart' as http; -import 'package:events/events.dart' as events; part 'src/Client.dart'; -part 'src/ss.dart'; +part 'src/webhook.dart'; part 'src/internal/_BaseObj.dart'; part 'src/internal/_Constants.dart'; @@ -76,9 +75,10 @@ part 'src/errors/HttpError.dart'; part 'src/errors/InvalidTokenError.dart'; part 'src/errors/InvalidShardError.dart'; +final bool _browser = false; + class _WebSocket { WebSocket _socket; - bool browser = false; _WebSocket(); @@ -100,7 +100,6 @@ class _WebSocket { class _HttpClient { http.Client client; - bool browser = false; _HttpClient() { this.client = new http.Client(); diff --git a/lib/src/Client.dart b/lib/src/Client.dart index b546fc4..6f8ecac 100644 --- a/lib/src/Client.dart +++ b/lib/src/Client.dart @@ -9,7 +9,6 @@ class Client { _Http _http; _WS _ws; _EventController _events; - _Util _util; /// The logged in user. ClientUser user; @@ -31,11 +30,7 @@ class Client { bool ready = false; /// The current version. - String version = "0.13.3"; - - /// The client's SS manager, null if the client is not sharded, [SSServer] if - /// the current shard is 0, [SSClient] otherwise. - dynamic ss; + String version = _Constants.version; /// The client's internal shards. Map shards; @@ -116,16 +111,7 @@ class Client { this._http = new _Http(this); this._events = new _EventController(this); - this._util = new _Util(); this._ws = new _WS(this); - - /*if (this._options.shardCount > 1) { - if (this._options.shardIds.contains(0)) { - this.ss = new SSServer(this); - } else { - this.ss = new SSClient(this); - } - }*/ } /// The client's uptime. @@ -136,11 +122,6 @@ class Client { await this._ws.close(); this._http.httpClient.close(); await this._events.destroy(); - /*if (this.ss is SSServer) { - await this.ss.close(); - } else if (this.ss is SSClient) { - this.ss.destroy(); - }*/ return null; } @@ -149,7 +130,7 @@ class Client { /// Throws an [Exception] if the HTTP request errored. /// Client.getUser("user id"); Future getUser(dynamic user) async { - final String id = this._util.resolve('user', user); + final String id = _Util.resolve('user', user); final _HttpResponse r = await this._http.get('/users/$id'); return new User._new(this, r.json as Map); @@ -169,7 +150,7 @@ class Client { /// Throws an [Exception] if the HTTP request errored /// Client.getOAuth2Info("app id"); Future getOAuth2Info(dynamic app) async { - final String id = this._util.resolve('app', app); + final String id = _Util.resolve('app', app); final _HttpResponse r = await this._http.get('/oauth2/authorize?client_id=$id&scope=bot'); diff --git a/lib/src/internal/_Constants.dart b/lib/src/internal/_Constants.dart index 349684b..c811736 100644 --- a/lib/src/internal/_Constants.dart +++ b/lib/src/internal/_Constants.dart @@ -19,6 +19,7 @@ class _OPCodes { /// The client constants. class _Constants { static const String host = "https://discordapp.com/api/v6"; + static const String version = "0.14.0"; /// The gateway OP codes. static const Map opCodes = const { diff --git a/lib/src/internal/_Http.dart b/lib/src/internal/_Http.dart index d17151e..4caaf92 100644 --- a/lib/src/internal/_Http.dart +++ b/lib/src/internal/_Http.dart @@ -1,7 +1,7 @@ part of discord; class _HttpRequest { - _HttpClient httpClient; + _Http http; String uri; String method; Map headers; @@ -10,35 +10,36 @@ class _HttpRequest { StreamController streamController; Stream stream; - _HttpRequest( - this.httpClient, this.uri, this.method, this.headers, this.body) { + _HttpRequest(this.http, this.uri, this.method, this.headers, this.body) { this.streamController = new StreamController(); this.stream = streamController.stream; if (this.method == "GET") { this.execute = () async { return await this + .http .httpClient - .get("${_Constants.host}$uri", headers: this.headers); + .get("${this.http.host}$uri", headers: this.headers); }; } else if (this.method == "POST") { this.execute = () async { - return await this.httpClient.post("${_Constants.host}$uri", + return await this.http.httpClient.post("${this.http.host}$uri", body: JSON.encode(this.body), headers: this.headers); }; } else if (this.method == "PATCH") { this.execute = () async { - return await this.httpClient.patch("${_Constants.host}$uri", + return await this.http.httpClient.patch("${this.http.host}$uri", body: JSON.encode(this.body), headers: this.headers); }; } else if (this.method == "PUT") { this.execute = () async { - return await this.httpClient.put("${_Constants.host}$uri", + return await this.http.httpClient.put("${this.http.host}$uri", body: JSON.encode(this.body), headers: this.headers); }; } else if (this.method == "DELETE") { this.execute = () async { return await this + .http .httpClient .delete("${_Constants.host}$uri", headers: this.headers); }; @@ -70,7 +71,7 @@ class _Bucket { } void execute(_HttpRequest request) { - if (request.httpClient.browser || + if (_browser || (this.ratelimitRemaining == null || this.ratelimitRemaining > 0)) { request.execute().then((http.Response r) { this.ratelimitRemaining = r.headers['x-ratelimit-remaining'] != null @@ -133,7 +134,8 @@ class _HttpResponse { /// The HTTP manager for the client. class _Http { - Client client; + dynamic client; + String host; Map buckets = {}; /// A map of headers that get sent on each HTTP request. @@ -143,22 +145,23 @@ class _Http { _HttpClient httpClient; /// Makes a new HTTP manager. - _Http(this.client) { + _Http(this.client, [this.host = _Constants.host]) { this.httpClient = new _HttpClient(); this.headers = {'Content-Type': 'application/json'}; - if (!httpClient.browser) + if (!_browser) this.headers['User-Agent'] = 'Discord Dart (https://github.com/Hackzzila/Discord-Dart, ${client.version})'; } /// Sends a GET request. Future<_HttpResponse> get(String uri, [bool beforeReady = false]) async { - if (!this.client.ready && !beforeReady) throw new ClientNotReadyError(); + if (client is Client && !this.client.ready && !beforeReady) + throw new ClientNotReadyError(); if (buckets[uri] == null) buckets[uri] = new _Bucket(uri); - await for (http.Response r in buckets[uri].push( - new _HttpRequest(this.httpClient, uri, "GET", this.headers, null))) { + await for (http.Response r in buckets[uri] + .push(new _HttpRequest(this, uri, "GET", this.headers, null))) { http_utils.ResponseStatus status = http_utils.ResponseStatus.fromStatusCode(r.statusCode); if (status.family == http_utils.ResponseStatusFamily.SUCCESSFUL) { @@ -173,11 +176,12 @@ class _Http { /// Sends a POST request. Future<_HttpResponse> post(String uri, Object content, [bool beforeReady = false]) async { - if (!this.client.ready && !beforeReady) throw new ClientNotReadyError(); + if (client is Client && !this.client.ready && !beforeReady) + throw new ClientNotReadyError(); if (buckets[uri] == null) buckets[uri] = new _Bucket(uri); - await for (http.Response r in buckets[uri].push(new _HttpRequest( - this.httpClient, uri, "POST", this.headers, content))) { + await for (http.Response r in buckets[uri] + .push(new _HttpRequest(this, uri, "POST", this.headers, content))) { http_utils.ResponseStatus status = http_utils.ResponseStatus.fromStatusCode(r.statusCode); if (status.family == http_utils.ResponseStatusFamily.SUCCESSFUL) { @@ -192,11 +196,12 @@ class _Http { /// Sends a PATCH request. Future<_HttpResponse> patch(String uri, Object content, [bool beforeReady = false]) async { - if (!this.client.ready && !beforeReady) throw new ClientNotReadyError(); + if (client is Client && !this.client.ready && !beforeReady) + throw new ClientNotReadyError(); if (buckets[uri] == null) buckets[uri] = new _Bucket(uri); - await for (http.Response r in buckets[uri].push(new _HttpRequest( - this.httpClient, uri, "PATCH", this.headers, content))) { + await for (http.Response r in buckets[uri] + .push(new _HttpRequest(this, uri, "PATCH", this.headers, content))) { http_utils.ResponseStatus status = http_utils.ResponseStatus.fromStatusCode(r.statusCode); if (status.family == http_utils.ResponseStatusFamily.SUCCESSFUL) { @@ -211,11 +216,12 @@ class _Http { /// Sends a PUT request. Future<_HttpResponse> put(String uri, Object content, [bool beforeReady = false]) async { - if (!this.client.ready && !beforeReady) throw new ClientNotReadyError(); + if (client is Client && !this.client.ready && !beforeReady) + throw new ClientNotReadyError(); if (buckets[uri] == null) buckets[uri] = new _Bucket(uri); - await for (http.Response r in buckets[uri].push( - new _HttpRequest(this.httpClient, uri, "PUT", this.headers, content))) { + await for (http.Response r in buckets[uri] + .push(new _HttpRequest(this, uri, "PUT", this.headers, content))) { http_utils.ResponseStatus status = http_utils.ResponseStatus.fromStatusCode(r.statusCode); if (status.family == http_utils.ResponseStatusFamily.SUCCESSFUL) { @@ -229,11 +235,12 @@ class _Http { /// Sends a DELETE request. Future<_HttpResponse> delete(String uri, [bool beforeReady = false]) async { - if (!this.client.ready && !beforeReady) throw new ClientNotReadyError(); + if (client is Client && !this.client.ready && !beforeReady) + throw new ClientNotReadyError(); if (buckets[uri] == null) buckets[uri] = new _Bucket(uri); - await for (http.Response r in buckets[uri].push( - new _HttpRequest(this.httpClient, uri, "DELETE", this.headers, null))) { + await for (http.Response r in buckets[uri] + .push(new _HttpRequest(this, uri, "DELETE", this.headers, null))) { http_utils.ResponseStatus status = http_utils.ResponseStatus.fromStatusCode(r.statusCode); if (status.family == http_utils.ResponseStatusFamily.SUCCESSFUL) { diff --git a/lib/src/internal/_Util.dart b/lib/src/internal/_Util.dart index fdedd0f..d1b12f4 100644 --- a/lib/src/internal/_Util.dart +++ b/lib/src/internal/_Util.dart @@ -3,13 +3,13 @@ part of discord; /// The utility functions for the client. class _Util { /// Gets a DateTime from a snowflake ID. - DateTime getDate(String id) { + static DateTime getDate(String id) { return new DateTime.fromMillisecondsSinceEpoch( ((int.parse(id) / 4194304) + 1420070400000).toInt()); } /// Resolves an object into a target object. - String resolve(String to, dynamic object) { + static String resolve(String to, dynamic object) { if (to == "channel") { if (object is Message) { return object.channel.id; diff --git a/lib/src/objects/Attachment.dart b/lib/src/objects/Attachment.dart index fbe9f8f..bab37f5 100644 --- a/lib/src/objects/Attachment.dart +++ b/lib/src/objects/Attachment.dart @@ -34,6 +34,6 @@ class Attachment extends _BaseObj { this.size = data['size']; this.height = data['height']; this.width = data['width']; - this.createdAt = this._client._util.getDate(this.id); + this.createdAt = _Util.getDate(this.id); } } diff --git a/lib/src/objects/Channel.dart b/lib/src/objects/Channel.dart index 323d1e6..629dd56 100644 --- a/lib/src/objects/Channel.dart +++ b/lib/src/objects/Channel.dart @@ -14,7 +14,7 @@ class Channel extends _BaseObj { Channel._new(Client client, Map data, this.type) : super(client) { this.id = data['id']; - this.createdAt = this._client._util.getDate(this.id); + this.createdAt = _Util.getDate(this.id); client.channels[this.id] = this; } diff --git a/lib/src/objects/DMChannel.dart b/lib/src/objects/DMChannel.dart index 59a54dd..d8b463c 100644 --- a/lib/src/objects/DMChannel.dart +++ b/lib/src/objects/DMChannel.dart @@ -77,7 +77,7 @@ class DMChannel extends Channel { /// Channel.getMessage("message id"); Future getMessage(dynamic message) async { if (this._client.user.bot) { - final String id = this._client._util.resolve('message', message); + final String id = _Util.resolve('message', message); final _HttpResponse r = await this._client._http.get('/channels/${this.id}/messages/$id'); diff --git a/lib/src/objects/GroupDMChannel.dart b/lib/src/objects/GroupDMChannel.dart index 509541d..86db778 100644 --- a/lib/src/objects/GroupDMChannel.dart +++ b/lib/src/objects/GroupDMChannel.dart @@ -75,7 +75,7 @@ class GroupDMChannel extends Channel { /// Channel.getMessage("message id"); Future getMessage(dynamic message) async { if (this._client.user.bot) { - final String id = this._client._util.resolve('message', message); + final String id = _Util.resolve('message', message); final _HttpResponse r = await this._client._http.get('/channels/${this.id}/messages/$id'); diff --git a/lib/src/objects/Guild.dart b/lib/src/objects/Guild.dart index f99e709..173e09b 100644 --- a/lib/src/objects/Guild.dart +++ b/lib/src/objects/Guild.dart @@ -83,7 +83,7 @@ class Guild extends _BaseObj { this.mfaLevel = data['mfa_level']; this.embedEnabled = data['embed_enabled']; this.ownerID = data['owner_id']; - this.createdAt = this._client._util.getDate(this.id); + this.createdAt = _Util.getDate(this.id); this.roles = new Map(); data['roles'].forEach((Map o) { @@ -224,7 +224,7 @@ class Guild extends _BaseObj { /// Throws an [Exception] if the HTTP request errored. /// Guild.getMember("user id"); Future getMember(dynamic member) async { - final String id = this._client._util.resolve('member', member); + final String id = _Util.resolve('member', member); if (this.members[member] != null) { return this.members[member]; @@ -245,7 +245,7 @@ class Guild extends _BaseObj { /// Guild.oauth2Authorize("app id"); Future oauth2Authorize(dynamic app, [int permissions = 0]) async { if (!this._client.user.bot) { - final String id = this._client._util.resolve('app', app); + final String id = _Util.resolve('app', app); await this._client._http.post( '/oauth2/authorize?client_id=$id&scope=bot', { diff --git a/lib/src/objects/InviteChannel.dart b/lib/src/objects/InviteChannel.dart index ca58a4b..44a2bf6 100644 --- a/lib/src/objects/InviteChannel.dart +++ b/lib/src/objects/InviteChannel.dart @@ -18,7 +18,7 @@ class InviteChannel extends _BaseObj { this.id = data['id']; this.name = data['name']; this.type = data['type']; - this.createdAt = this._client._util.getDate(this.id); + this.createdAt = _Util.getDate(this.id); } /// Returns a string representation of this object. diff --git a/lib/src/objects/InviteGuild.dart b/lib/src/objects/InviteGuild.dart index c628b24..b59462c 100644 --- a/lib/src/objects/InviteGuild.dart +++ b/lib/src/objects/InviteGuild.dart @@ -18,7 +18,7 @@ class InviteGuild extends _BaseObj { this.id = data['id']; this.name = data['name']; this.spash = data['splash_hash']; - this.createdAt = this._client._util.getDate(this.id); + this.createdAt = _Util.getDate(this.id); } /// Returns a string representation of this object. diff --git a/lib/src/objects/Message.dart b/lib/src/objects/Message.dart index 2faba08..396d8ce 100644 --- a/lib/src/objects/Message.dart +++ b/lib/src/objects/Message.dart @@ -79,7 +79,7 @@ class Message extends _BaseObj { this.pinned = data['pinned']; this.tts = data['tts']; this.mentionEveryone = data['mention_everyone']; - this.createdAt = this._client._util.getDate(this.id); + this.createdAt = _Util.getDate(this.id); this.channel._cacheMessage(this); this.channel.lastMessageID = this.id; diff --git a/lib/src/objects/OAuth2Application.dart b/lib/src/objects/OAuth2Application.dart index a5de67e..b16d5bc 100644 --- a/lib/src/objects/OAuth2Application.dart +++ b/lib/src/objects/OAuth2Application.dart @@ -27,7 +27,7 @@ class OAuth2Application extends _BaseObj { this.id = data['id']; this.name = data['name']; this.rpcOrigins = data['rpcOrigins'] as List; - this.createdAt = this._client._util.getDate(this.id); + this.createdAt = _Util.getDate(this.id); } /// Returns a string representation of this object. diff --git a/lib/src/objects/OAuth2Guild.dart b/lib/src/objects/OAuth2Guild.dart index 15a61cc..dfecdf6 100644 --- a/lib/src/objects/OAuth2Guild.dart +++ b/lib/src/objects/OAuth2Guild.dart @@ -22,7 +22,7 @@ class OAuth2Guild extends _BaseObj { this.icon = data['icon']; this.id = data['id']; this.name = data['name']; - this.createdAt = this._client._util.getDate(this.id); + this.createdAt = _Util.getDate(this.id); } /// Returns a string representation of this object. diff --git a/lib/src/objects/Role.dart b/lib/src/objects/Role.dart index 2c807a8..862c7c8 100644 --- a/lib/src/objects/Role.dart +++ b/lib/src/objects/Role.dart @@ -42,7 +42,7 @@ class Role extends _BaseObj { this.mentionable = data['mentionable']; this.permissions = new Permissions.fromInt(this._client, data['permissions']); - this.createdAt = this._client._util.getDate(this.id); + this.createdAt = _Util.getDate(this.id); if (data['color'] == 0) { this.color = null; diff --git a/lib/src/objects/Shard.dart b/lib/src/objects/Shard.dart index 0e95006..255f922 100644 --- a/lib/src/objects/Shard.dart +++ b/lib/src/objects/Shard.dart @@ -108,7 +108,7 @@ class Shard extends _BaseObj { this._ws.client._options.forceFetchMembers = false; } - if (!this._ws.client._http.httpClient.browser) + if (!_browser) this._ws.client._http.headers['User-Agent'] = "${this._ws.client.user.username} (https://github.com/Hackzzila/Discord-Dart, ${this._ws.client.version})"; @@ -143,19 +143,7 @@ class Shard extends _BaseObj { break; case 'MESSAGE_CREATE': - MessageEvent msgEvent = - new MessageEvent._new(this._ws.client, json); - /*if (this._ws.client.ready && - msgEvent.message.channel.type == "private" && - this._ws.client.ss is SSServer) { - for (Socket socket in this._ws.client.ss.sockets) { - socket.write(JSON.encode({ - "op": 3, - "t": _ws.client._token, - "d": json - })); - } - }*/ + new MessageEvent._new(this._ws.client, json); break; case 'MESSAGE_DELETE': diff --git a/lib/src/objects/TextChannel.dart b/lib/src/objects/TextChannel.dart index 10a6f8c..a227d75 100644 --- a/lib/src/objects/TextChannel.dart +++ b/lib/src/objects/TextChannel.dart @@ -85,7 +85,7 @@ class TextChannel extends GuildChannel { /// Channel.getMessage("message id"); Future getMessage(dynamic message) async { if (this._client.user.bot) { - final String id = this._client._util.resolve('message', message); + final String id = _Util.resolve('message', message); final _HttpResponse r = await this._client._http.get('/channels/${this.id}/messages/$id'); diff --git a/lib/src/objects/User.dart b/lib/src/objects/User.dart index 1d79259..e65b861 100644 --- a/lib/src/objects/User.dart +++ b/lib/src/objects/User.dart @@ -38,7 +38,7 @@ class User extends _BaseObj { this.avatarURL = "${_Constants.host}/users/${this.id}/avatars/${this.avatar}.jpg"; this.mention = "<@${this.id}>"; - this.createdAt = this._client._util.getDate(this.id); + this.createdAt = _Util.getDate(this.id); // This will not be set at all in some cases. if (data['bot'] == true) { @@ -102,7 +102,7 @@ class User extends _BaseObj { /// Channel.getMessage("message id"); Future getMessage(dynamic message) async { if (this._client.user.bot) { - final String id = this._client._util.resolve('message', message); + final String id = _Util.resolve('message', message); DMChannel channel = await getChannel(); String channelId = channel.id; diff --git a/lib/src/objects/Webhook.dart b/lib/src/objects/Webhook.dart index cc5be6f..d5f1fdf 100644 --- a/lib/src/objects/Webhook.dart +++ b/lib/src/objects/Webhook.dart @@ -1,13 +1,7 @@ part of discord; /// A user. -class Webhook { - /// The client. - Client client; - - /// A map of all of the properties. - Map map = {}; - +class Webhook extends _BaseObj { /// The webhook's name. String name; @@ -20,29 +14,31 @@ class Webhook { /// The webhook's channel id. String channelId; - /// The webhook's channel, if the client has that channel in it's cache. + /// The webhook's channel, if this is accessed using a normal client and the client has that channel in it's cache. TextChannel channel; /// The webhook's guild id. String guildId; - /// The webhook's guild, if the client has that guild in it's cache. + /// The webhook's guild, if this is accessed using a normal client and the client has that guild in it's cache. Guild guild; /// When the webhook was created; DateTime createdAt; /// Constructs a new [User]. - Webhook(this.client, Map data) { - this.name = this.map['name'] = data['name']; - this.id = this.map['id'] = data['id']; - this.token = this.map['token'] = data['token']; - this.channelId = this.map['channelId'] = data['channel_id']; - this.guildId = this.map['guildId'] = data['guild_id']; - this.createdAt = this.map['createdAt'] = this.client._util.getDate(this.id); - - this.channel = this.client.channels[this.channelId]; - this.guild = this.client.guilds[this.guildId]; + Webhook._new(Client client, Map data) : super(client) { + this.name = data['name']; + this.id = data['id']; + this.token = data['token']; + this.channelId = data['channel_id']; + this.guildId = data['guild_id']; + this.createdAt = _Util.getDate(this.id); + + if (client != null) { + this.channel = this._client.channels[this.channelId]; + this.guild = this._client.guilds[this.guildId]; + } } /// Returns a string representation of this object. diff --git a/lib/src/webhook.dart b/lib/src/webhook.dart index 30763a7..cdef343 100644 --- a/lib/src/webhook.dart +++ b/lib/src/webhook.dart @@ -1,163 +1,64 @@ -/*import 'dart:async'; -import 'dart:convert'; -import '../discord.dart'; -import 'internal/util.dart'; -import 'package:http/http.dart' as http_lib; - -/// The HTTP manager for the client. -class HTTP { - /// The base API URL. - String host; - - /// A map of headers that get sent on each HTTP request. - Map headers; - - /// The HTTP client. - http_lib.Client http; - - /// Makes a new HTTP manager. - HTTP(Client client) { - this.http = new http_lib.Client(); - this.headers = { - 'User-Agent': - 'Discord Dart Webhooks (https://github.com/Hackzzila/Discord-Dart, ${client.version})', - 'Content-Type': 'application/json' - }; - } - - /// Sends a GET request. - Future get() async { - return await this.http.get(this.host, headers: this.headers); - } - - /// Sends a POST request. - Future post(Object content) async { - return await this - .http - .post(this.host, body: JSON.encode(content), headers: this.headers); - } - - /// Sends a PATCH request. - Future patch(Object content) async { - return await this - .http - .patch(this.host, body: JSON.encode(content), headers: this.headers); - } - - /// Sends a DELETE request. - Future delete() async { - return await this.http.delete(this.host, headers: this.headers); - } -} +part of discord; /// A webhook client. -class Client { +class WebhookClient { /// The HTTP client. - HTTP http; + _Http http; /// The version. - String version = "0.11.1+dev"; + String version = _Constants.version; /// Emitted when data is received about the webhook and all properties are filled. - Stream onReady; + Stream onReady; - /// The webhook's name. - String name; - - /// The webhook's id. - String id; - - /// The webhook's token. - String token; - - /// The webhook's channel id. - String channelId; - - /// The webhook's guild id. - String guildId; - - /// When the webhook was created; - DateTime createdAt; + /// The webhook; + Webhook webhook; /// Sets up a new webhook client. - Client({url, id, token}) { - StreamController onReady = new StreamController.broadcast(); + WebhookClient({url, id, token}) { + StreamController onReady = new StreamController.broadcast(); this.onReady = onReady.stream; - this.http = new HTTP(this); + this.http = new _Http(this); if (url == null) { this.http.host = "https://discordapp.com/api/webhooks/$id/$token"; } else { this.http.host = url; } - this.http.get().then((http_lib.Response r) { - if (r.statusCode == 200) { - dynamic data = JSON.decode(r.body); - this.name = data['name']; - this.id = data['id']; - this.token = data['token']; - this.channelId = data['channel_id']; - this.guildId = data['guild_id']; - - Util util = new Util(); - this.createdAt = util.getDate(this.id); - onReady.add(null); - } else { - throw new HttpError(r); - } + this.http.get("").then((_HttpResponse r) { + this.webhook = new Webhook._new(null, r.json as Map); + onReady.add(this.webhook); }); } /// Sends a message with the webhook. Future sendMessage( {String content, - Null file, List> embeds, String username, String avatarUrl, bool tts}) async { - http_lib.Response r = await this.http.post({ + await this.http.post("", { "content": content, "username": username, "avatar_url": avatarUrl, "tts": tts, "embeds": embeds }); - if (r.statusCode == 204) { - return null; - } else { - throw new HttpError(r); - } + return null; } /// Deletes the webhook. Future delete() async { - http_lib.Response r = await this.http.delete(); - if (r.statusCode == 204) { - return null; - } else { - throw new HttpError(r); - } + await this.http.delete(""); + return null; } /// Edits the webhook. - Future edit({String name, List avatar}) async { - http_lib.Response r = - await this.http.patch({"name": name, "avatar": avatar}); - - if (r.statusCode == 200) { - dynamic data = JSON.decode(r.body); - this.name = data['name']; - this.id = data['id']; - this.token = data['token']; - this.channelId = data['channel_id']; - this.guildId = data['guild_id']; - - Util util = new Util(); - this.createdAt = util.getDate(this.id); - } else { - throw new HttpError(r); - } + Future edit({String name, List avatar}) async { + _HttpResponse r = + await this.http.patch("", {"name": name, "avatar": avatar}); + this.webhook = new Webhook._new(null, r.json as Map); + return this.webhook; } } -*/ diff --git a/lib/webhook.dart b/lib/webhook.dart deleted file mode 100644 index 6f0d6e2..0000000 --- a/lib/webhook.dart +++ /dev/null @@ -1 +0,0 @@ -export 'src/webhook.dart'; diff --git a/pubspec.yaml b/pubspec.yaml index bc95391..55efe45 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,11 +1,10 @@ name: discord -version: 0.13.3 +version: 0.14.0 description: A Discord library for Dart author: Hackzzila homepage: https://hackzzila.github.io/Discord-Dart/ dependencies: http: ">=0.11.3+9 <0.12.0" - events: ">=0.1.2+1 <0.2.0" http_utils: ">=1.4.0 <2.0.0" intl: ">=0.14.0 <0.15.0" http_parser: ">=3.0.2 <4.0.0"