Skip to content

Commit

Permalink
Merge pull request #109 from mineral-dart/feat/threads
Browse files Browse the repository at this point in the history
Added threads (only by message)
  • Loading branch information
PandaGuerrier authored May 13, 2023
2 parents b426ebd + 42b3ea1 commit e302d53
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 16 deletions.
11 changes: 11 additions & 0 deletions lib/src/api/messages/message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@ class Message extends PartialMessage<TextBasedChannel> {
.build();
}

Future<ThreadChannel?> createThread ({ required String label, int archiveDuration = 60}) async {
Response respose = await ioc.use<DiscordApiHttpService>().post(url: '/channels/${super.channel.id}/messages/${super.id}/threads')
.payload({
'name': label,
'auto_archive_duration': archiveDuration,
})
.build();

return ThreadChannel.fromPayload(jsonDecode(respose.body));
}

Future<void> pin (Snowflake webhookId) async {
if (isPinned) {
ioc.use<ConsoleService>().warn('Message $id is already pinned');
Expand Down
50 changes: 34 additions & 16 deletions lib/src/internal/websockets/packets/guild_create_packet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import 'package:mineral/core/api.dart';
import 'package:mineral/core/events.dart';
import 'package:mineral/framework.dart';
import 'package:mineral/src/api/channels/partial_channel.dart';
import 'package:mineral/src/api/guilds/activities/guild_member_activity.dart';
import 'package:mineral/src/api/guilds/guild_member_presence.dart';
import 'package:mineral/src/api/managers/channel_manager.dart';
import 'package:mineral/src/api/managers/emoji_manager.dart';
import 'package:mineral/src/api/managers/guild_role_manager.dart';
Expand All @@ -21,6 +23,8 @@ import 'package:mineral/src/internal/services/event_service.dart';
import 'package:mineral/src/internal/websockets/websocket_packet.dart';
import 'package:mineral/src/internal/websockets/websocket_response.dart';

import '../../../api/guilds/client_status_bucket.dart';

class GuildCreatePacket with Container implements WebsocketPacket {
@override
Future<void> handle(WebsocketResponse websocketResponse) async {
Expand Down Expand Up @@ -49,9 +53,9 @@ class GuildCreatePacket with Container implements WebsocketPacket {
EmojiManager emojiManager = EmojiManager();
for(dynamic payload in websocketResponse.payload['emojis']) {
Emoji emoji = Emoji.from(
memberManager: memberManager,
emojiManager: emojiManager,
payload: payload
memberManager: memberManager,
emojiManager: emojiManager,
payload: payload
);

emojiManager.cache.putIfAbsent(emoji.id, () => emoji);
Expand All @@ -60,9 +64,9 @@ class GuildCreatePacket with Container implements WebsocketPacket {
GuildScheduledEventService guildScheduledManager = GuildScheduledEventService();
for(dynamic payload in websocketResponse.payload['guild_scheduled_events']) {
GuildScheduledEvent event = GuildScheduledEvent.from(
channelManager: channelManager,
memberManager: memberManager,
payload: payload
channelManager: channelManager,
memberManager: memberManager,
payload: payload
);

guildScheduledManager.cache.putIfAbsent(event.id, () => event);
Expand Down Expand Up @@ -93,13 +97,13 @@ class GuildCreatePacket with Container implements WebsocketPacket {
for (dynamic member in websocketResponse.payload['members']) {
User user = User.from(member['user']);
GuildMember guildMember = GuildMember.from(
roles: roleManager,
user: user,
member: member,
guild: guild,
voice: voices.containsKey(user.id)
? voices.get(user.id)!
: VoiceManager.empty(member['deaf'], member['mute'], user.id, websocketResponse.payload['guild_id'])
roles: roleManager,
user: user,
member: member,
guild: guild,
voice: voices.containsKey(user.id)
? voices.get(user.id)!
: VoiceManager.empty(member['deaf'], member['mute'], user.id, websocketResponse.payload['guild_id'])
);

memberManager.cache.putIfAbsent(guildMember.user.id, () => guildMember);
Expand All @@ -115,6 +119,20 @@ class GuildCreatePacket with Container implements WebsocketPacket {
}
}

for (dynamic payload in websocketResponse.payload['presences']) {
payload['guild_id'] = websocketResponse.payload['id'];
GuildMember member = await guild.members.resolve(payload['user']['id']);
ClientStatusBucket clientStatusBucket = ClientStatusBucket(payload['client_status']['desktop'], payload['client_status']['mobile'], payload['client_status']['web']);

List<GuildMemberActivity> activities = [];
for(dynamic activity in payload['activities']) {
activities.add(GuildMemberActivity(ActivityType.values.firstWhere((element) => element.value == activity['type']), activity['name']));
}

GuildMemberPresence guildMemberPresence = GuildMemberPresence(payload['guild_id'], payload['status'], null, clientStatusBucket, activities);
member.presence = guildMemberPresence;
}

guild.afkChannel = guild.channels.cache.get<VoiceChannel>(guild.afkChannelId);
guild.systemChannel = guild.channels.cache.get<TextChannel>(guild.systemChannelId);
guild.rulesChannel = guild.channels.cache.get<TextChannel>(guild.rulesChannelId);
Expand Down Expand Up @@ -143,8 +161,8 @@ class GuildCreatePacket with Container implements WebsocketPacket {

Future<Map<Snowflake, ModerationRule>?> getAutoModerationRules (Guild guild) async {
Response response = await container.use<DiscordApiHttpService>()
.get(url: "/guilds/${guild.id}/auto-moderation/rules")
.build();
.get(url: "/guilds/${guild.id}/auto-moderation/rules")
.build();

if (response.statusCode == 200) {
dynamic payload = jsonDecode(response.body);
Expand All @@ -160,4 +178,4 @@ class GuildCreatePacket with Container implements WebsocketPacket {

return null;
}
}
}

0 comments on commit e302d53

Please sign in to comment.