Skip to content

Commit

Permalink
Merge pull request #4 from mineral-dart/emoji
Browse files Browse the repository at this point in the history
Feat: Implement emoji
  • Loading branch information
LeadcodeDev authored May 26, 2022
2 parents 9923c71 + 08ef12c commit 901d8d6
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/console.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Console {
print("[ $p ] $message");
}

static getErrorMessage ({ String? prefix = 'error', required String message }) {
static String getErrorMessage ({ String? prefix = 'error', required String message }) {
String p = ColorList.red(prefix!);
return "[ $p ] $message";
}
Expand Down
1 change: 1 addition & 0 deletions lib/exception.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ library exception;
import 'package:mineral/console.dart';

part 'src/exceptions/token_exception.dart';
part 'src/exceptions/empty_parameter_exception.dart';
40 changes: 39 additions & 1 deletion lib/src/api/emoji.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Emoji {
bool managed;
bool animated;
bool available;
EmojiManager manager;

Emoji({
required this.id,
Expand All @@ -19,9 +20,45 @@ class Emoji {
required this.managed,
required this.animated,
required this.available,
required this.manager,
});

factory Emoji.from({ required MemberManager memberManager, required RoleManager roleManager, required dynamic payload }) {
Future<void> setLabel (String label) async {
Http http = ioc.singleton(Service.http);
Response response = await http.patch("/guilds/${manager.guildId}/emojis/$id", { 'name': label });

if (response.statusCode == 200) {
this.label = label;
}
}

Future<void> setRoles (List<Snowflake> roles) async {
Http http = ioc.singleton(Service.http);
Response response = await http.patch("/guilds/${manager.guildId}/emojis/$id", { 'roles': roles });

if (response.statusCode == 200) {
List<Role> _roles = [];
for (Snowflake id in roles) {
Role? role = manager.guild?.roles.cache.get(id);
if (role != null) {
_roles.add(role);
}
}

this.roles = _roles;
}
}

Future<void> delete () async {
Http http = ioc.singleton(Service.http);
Response response = await http.destroy("/guilds/${manager.guildId}/emojis/$id");

if (response.statusCode == 200) {
manager.cache.remove(id);
}
}

factory Emoji.from({ required MemberManager memberManager, required RoleManager roleManager, required EmojiManager emojiManager, required dynamic payload }) {
List<Role> roles = [];
for (dynamic id in payload['roles']) {
Role? role = roleManager.cache.get(id);
Expand All @@ -39,6 +76,7 @@ class Emoji {
managed: payload['managed'] ?? false,
animated: payload['animated'] ?? false,
available: payload['available'] ?? false,
manager: emojiManager
);
}
}
28 changes: 28 additions & 0 deletions lib/src/api/managers/emoji_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import 'dart:convert';
import 'package:http/http.dart';
import 'package:mineral/api.dart';
import 'package:mineral/core.dart';
import 'package:mineral/exception.dart';
import 'package:mineral/helper.dart';
import 'package:mineral/src/api/managers/cache_manager.dart';

class EmojiManager implements CacheManager<Emoji> {
Expand All @@ -26,6 +28,7 @@ class EmojiManager implements CacheManager<Emoji> {
Emoji emoji = Emoji.from(
memberManager: guild!.members,
roleManager: guild!.roles,
emojiManager: this,
payload: element
);

Expand All @@ -34,4 +37,29 @@ class EmojiManager implements CacheManager<Emoji> {

return cache;
}

Future<Emoji> create ({ required String label, required String path, List<Snowflake>? roles }) async {
if (path.isEmpty) {
throw EmptyParameterException(cause: 'Parameter "path" cannot be null or empty');
}

Http http = ioc.singleton(Service.http);
String image = await Helper.getPicture(path);
Response response = await http.post("/guilds/$guildId/emojis", {
'name': label,
'image': image,
'roles': roles ?? [],
});

Emoji emoji = Emoji.from(
memberManager: guild!.members,
roleManager: guild!.roles,
emojiManager: this,
payload: jsonDecode(response.body),
);

cache.putIfAbsent(emoji.id, () => emoji);

return emoji;
}
}
12 changes: 12 additions & 0 deletions lib/src/exceptions/empty_parameter_exception.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
part of exception;

class EmptyParameterException implements Exception {
String prefix = 'INVALID PARAMETER';
String cause;
EmptyParameterException({ required this.cause });

@override
String toString () {
return Console.getErrorMessage(prefix: prefix, message: cause);
}
}
2 changes: 2 additions & 0 deletions lib/src/internal/websockets/packets/guild_create.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class GuildCreate implements WebsocketPacket {
Emoji emoji = Emoji.from(
memberManager: memberManager,
roleManager: roleManager,
emojiManager: emojiManager,
payload: payload
);

Expand Down Expand Up @@ -85,6 +86,7 @@ class GuildCreate implements WebsocketPacket {
guild.systemChannel = guild.channels.cache.get<TextChannel>(guild.systemChannelId);
guild.rulesChannel = guild.channels.cache.get<TextChannel>(guild.rulesChannelId);
guild.publicUpdatesChannel = guild.channels.cache.get<TextChannel>(guild.publicUpdatesChannelId);
guild.emojis.guild = guild;

manager.emit(EventList.guildCreate, { 'guild': guild });
}
Expand Down

0 comments on commit 901d8d6

Please sign in to comment.