OAuth2 APIs!

This commit is contained in:
Zachary Vacura 2016-09-17 11:32:22 -05:00
parent 86095f6c7b
commit 0d35196af7
9 changed files with 182 additions and 7 deletions

View file

@ -29,11 +29,14 @@ class Client {
/// The logged in user.
ClientUser user;
/// The bot's OAuth2 app.
ClientOAuth2Application app;
void _heartbeat() {
this._socket.add(JSON.encode({"op": 1,"d": this._lastS}));
}
void _handleMsg(msg) {
Future _handleMsg(msg) async {
var json = JSON.decode(msg);
this._handlers['debug'].forEach((function) => function(json));
@ -80,6 +83,12 @@ class Client {
if (this.user.bot) {
this._api.headers['Authorization'] = "Bot ${this.token}";
var r = await this._api.get('oauth2/applications/@me');
Map res = JSON.decode(r.body);
if (r.statusCode == 200) {
this.app = new ClientOAuth2Application(res);
}
} else {
this._api.headers['Authorization'] = this.token;
}
@ -151,7 +160,7 @@ class Client {
options = new MessageOptions();
}
if (options.disableEveryone || this.options.disableEveryone) {
if (options.disableEveryone || (options.disableEveryone == null && this.options.disableEveryone)) {
content = content.replaceAll("@everyone", "@\u200Beveryone").replaceAll("@here", "@\u200Bhere");
}
@ -169,11 +178,11 @@ class Client {
///
/// Throws an [Exception] if the HTTP request errored.
/// Client.sendMessage("channel id", "message id");
Future deleteMessage(String channel, String message) async {
Future<bool> deleteMessage(String channel, String message) async {
var r = await this._api.delete('channels/$channel/messages/$message');
if (r.statusCode == 204) {
return;
return true;
} else {
throw new Exception("'deleteMessage' error.");
}
@ -282,4 +291,42 @@ class Client {
throw new Exception("'getMessage' is only usable by bot accounts.");
}
}
/// Gets an [OAuth2Info] object. Only usable by user accounts.
///
/// Throws an [Exception] if the HTTP request errored or if the client user
/// is a bot.
/// Client.getOAuth2Info("app id");
Future<OAuth2Info> getOAuth2Info(String id) async {
if (!this.user.bot) {
var r = await this._api.get('oauth2/authorize?client_id=$id&scope=bot');
Map res = JSON.decode(r.body);
if (r.statusCode == 200) {
return new OAuth2Info(res);
} else {
throw new Exception("${res['code']}:${res['message']}");
}
} else {
throw new Exception("'getMessage' is only usable by user accounts.");
}
}
/// Gets an [OAuth2Info] object. Only usable by user accounts.
///
/// Throws an [Exception] if the HTTP request errored or if the client user
/// is a bot.
/// Client.getOAuth2Info("app id");
Future<bool> oauth2Authorize(String app, String guild, [int permissions = 0]) async {
if (!this.user.bot) {
var r = await this._api.post('oauth2/authorize?client_id=$app&scope=bot', {"guild_id": guild, "permissions": permissions, "authorize": true});
Map res = JSON.decode(r.body);
if (r.statusCode == 200) {
return true;
} else {
throw new Exception("${res['code']}:${res['message']}");
}
} else {
throw new Exception("'getMessage' is only usable by user accounts.");
}
}
}

View file

@ -1,5 +1,6 @@
export 'objects/Attachment.dart';
export 'objects/Channel.dart';
export 'objects/ClientOAuth2Application.dart';
export 'objects/ClientOptions.dart';
export 'objects/ClientUser.dart';
export 'objects/Embed.dart';
@ -12,4 +13,7 @@ export 'objects/InviteGuild.dart';
export 'objects/Member.dart';
export 'objects/MessageOptions.dart';
export 'objects/Message.dart';
export 'objects/OAuth2Application.dart';
export 'objects/OAuth2Guild.dart';
export 'objects/OAuth2Info.dart';
export 'objects/User.dart';

View file

@ -0,0 +1,39 @@
import '../objects.dart';
/// The client's OAuth2 app, if the client is a bot.
class ClientOAuth2Application {
/// The app's description.
String description;
/// The app's icon hash.
String icon;
/// The app's ID.
String id;
/// The app's name.
String name;
/// The app's RPC origins.
List rpcOrigins;
/// The app's flags.
int flags;
/// The app's owner.
User owner;
/// A timestamp for when the app was created
double createdAt;
ClientOAuth2Application(Map data) {
this.description = data['description'];
this.icon = data['icon'];
this.id = data['id'];
this.name = data['name'];
this.rpcOrigins = data['rpcOrigins'];
this.flags = data['flags'];
this.owner = new User(data['owner']);
this.createdAt = (int.parse(this.id) / 4194304) + 1420070400000;
}
}

View file

@ -63,7 +63,7 @@ class Guild {
this.memberCount = data['member_count'];
this.verificationLevel = data['verification_level'];
this.notificationLevel = data['default_message_notifications'];
this.mfaLevel = data['default_message_notifications'];
this.mfaLevel = data['mfa_level'];
this.embedEnabled = data['embed_enabled'];
this.ownerID = data['owner_id'];
this.createdAt = (int.parse(this.id) / 4194304) + 1420070400000;

View file

@ -8,5 +8,5 @@ class MessageOptions {
String nonce;
/// Whether or not to disable @everyone and @here mentions for the message.
bool disableEveryone = false;
bool disableEveryone;
}

View file

@ -0,0 +1,31 @@
import '../objects.dart';
/// An OAuth2 application.
class OAuth2Application {
/// The app's description.
String description;
/// The app's icon hash.
String icon;
/// The app's ID.
String id;
/// The app's name.
String name;
/// The app's RPC origins.
List rpcOrigins;
/// A timestamp for when the app was created.
double createdAt;
OAuth2Application(Map data) {
this.description = data['description'];
this.icon = data['icon'];
this.id = data['id'];
this.name = data['name'];
this.rpcOrigins = data['rpcOrigins'];
this.createdAt = (int.parse(this.id) / 4194304) + 1420070400000;
}
}

View file

@ -0,0 +1,27 @@
import '../objects.dart';
/// A mini guild object with permissions for [OAuth2Info].
class OAuth2Guild {
/// The permissions you have on that guild.
int permissions;
/// The guild's icon hash.
String icon;
/// The guild's ID.
String id;
/// The guild's name
String name;
/// A timestamp for when the guild was created.
double createdAt;
OAuth2Guild(Map data) {
this.permissions = data['permissions'];
this.icon = data['icon'];
this.id = data['id'];
this.name = data['name'];
this.createdAt = (int.parse(this.id) / 4194304) + 1420070400000;
}
}

View file

@ -0,0 +1,27 @@
import '../objects.dart';
/// Info about a OAuth2 app, bot, user, and possible guilds that that bot can
/// be invited to.
class OAuth2Info {
/// The OAuth2 app.
OAuth2Application app;
/// The app's bot.
User bot;
/// You.
User me;
/// A list of mini guild objects with permissions for every guild you are on.
List<OAuth2Guild> guilds = [];
OAuth2Info(Map data) {
this.app = new OAuth2Application(data['application']);
this.bot = new User(data['bot']);
this.me = new User(data['user']);
data['guilds'].forEach((v) {
guilds.add(new OAuth2Guild(v));
});
}
}

View file

@ -1,5 +1,5 @@
name: discord_dart
version: 0.3.1
version: 0.4.0
description: A Discord library for Dart
author: Hackzzila <admin@hackzzila.com>
homepage: https://github.com/Hackzzila/Discord-Dart