LibAC-dart/lib/discord/networking/user.dart

60 lines
1.7 KiB
Dart

import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:libac_dart/discord/networking/endpoints.dart';
import '../structures/user.dart';
class UserPackets {
/// Requests the current user from Discord's servers
static Future<User> getCurrentUser() async {
Dio dio = Dio(DiscordSessionSettings.getOptions);
var reply = await dio.get(
"${DiscordEndpoints.BaseURL}${DiscordEndpoints.Users}${DiscordEndpoints.ME}");
return User.fromJson(reply.data as String);
}
/// Requests the user from Discord's servers
///
/// [id] is expected to be a valid Discord User ID
static Future<User> getUser(int id) async {
Dio dio = Dio(DiscordSessionSettings.getOptions);
var reply = await dio
.get("${DiscordEndpoints.BaseURL}${DiscordEndpoints.Users}/${id}");
return User.fromJson(reply.data as String);
}
/// Updates any values that were set in the packet, and returns the updated user
///
/// Fires a User Update gateway event
static Future<User> updateCurrentUser(ModifyCurrentUserPacket mcup) async {
Dio dio = Dio(DiscordSessionSettings.getOptions);
var reply = await dio.patch(
"${DiscordEndpoints.BaseURL}${DiscordEndpoints.Users}${DiscordEndpoints.ME}",
data: mcup.encode());
return User.fromJson(reply.data as String);
}
}
class ModifyCurrentUserPacket {
String? username;
String? avatar;
String? banner;
ModifyCurrentUserPacket({this.username, this.avatar, this.banner});
String encode() {
return json.encode(toJson());
}
Map<String, dynamic> toJson() {
return {
if (username != null) "username": username,
if (avatar != null) "avatar": avatar,
if (banner != null) "banner": banner,
};
}
}