Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/guild create #126

Merged
merged 3 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions lib/src/api/guilds/guild.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class Guild {
late TextChannel? rulesChannel;
int? _maxPresences;
int _maxMembers;
VanityInvite? _vanityInvite;
late VanityInvite? vanity;
String? _description;
ImageFormater? _banner;
int _premiumTier;
Expand Down Expand Up @@ -117,7 +117,6 @@ class Guild {
this._rulesChannelId,
this._maxPresences,
this._maxMembers,
this._vanityInvite,
this._description,
this._banner,
this._premiumTier,
Expand Down Expand Up @@ -217,9 +216,6 @@ class Guild {
/// The [Guild]'s max members.
int get maxMembers => _maxMembers;

/// The [Guild]'s vanity url code.
VanityInvite? get vanity => _vanityInvite;

/// The [Guild]'s description.
String? get description => _description;

Expand Down Expand Up @@ -667,7 +663,6 @@ class Guild {
required ModerationRuleManager moderationRuleManager,
required WebhookManager webhookManager,
required GuildScheduledEventService guildScheduledEventService,
required VanityInvite? vanityInvite,
required dynamic payload
}) {
StickerManager stickerManager = StickerManager();
Expand Down Expand Up @@ -706,7 +701,6 @@ class Guild {
payload['rules_channel_id'],
payload['max_presences'],
payload['max_members'],
vanityInvite,
payload['description'],
payload['banner'] != null ? ImageFormater(payload['banner'], 'banners/${payload['id']}') : null,
payload['premium_tier'],
Expand Down
7 changes: 3 additions & 4 deletions lib/src/internal/mixins/mineral_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ extension MineralClientExtension on MineralClient {
ModerationRuleManager moderationManager = ModerationRuleManager(websocketPayload['guild_id']);

WebhookManager webhookManager = WebhookManager(websocketPayload['id'], null);
VanityInvite? vanityInvite = await VanityInvite.sync(websocketPayload['id']);

Guild guild = Guild.from(
emojiManager: emojiManager,
Expand All @@ -103,11 +102,10 @@ extension MineralClientExtension on MineralClient {
moderationRuleManager: moderationManager,
webhookManager: webhookManager,
guildScheduledEventService: guildScheduledManager,
vanityInvite: vanityInvite,
payload: websocketPayload,
);

this.guilds.cache.putIfAbsent(guild.id, () => guild);
guilds.cache.putIfAbsent(guild.id, () => guild);

for (dynamic element in websocketPayload['stickers']) {
Sticker sticker = Sticker.from(element);
Expand All @@ -127,7 +125,7 @@ extension MineralClientExtension on MineralClient {
);

memberManager.cache.putIfAbsent(guildMember.user.id, () => guildMember);
this.users.cache.putIfAbsent(user.id, () => user);
users.cache.putIfAbsent(user.id, () => user);
}

for(dynamic payload in websocketPayload['channels']) {
Expand Down Expand Up @@ -161,6 +159,7 @@ extension MineralClientExtension on MineralClient {
guild.webhooks.guild = guild;
guild.emojis.guild = guild;
guild.scheduledEvents.guild = guild;
guild.vanity = await VanityInvite.sync(websocketPayload['id']);

Map<Snowflake, ModerationRule>? autoModerationRules = await getAutoModerationRules(guild);
if (autoModerationRules != null) {
Expand Down
25 changes: 0 additions & 25 deletions lib/src/internal/websockets/packets/guild_create_packet.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import 'dart:convert';

import 'package:http/http.dart';
import 'package:mineral/core/api.dart';
import 'package:mineral/core/events.dart';
import 'package:mineral/src/internal/mixins/container.dart';
Expand All @@ -9,8 +6,6 @@ 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 '../../../../core.dart';

class GuildCreatePacket with Container implements WebsocketPacket {
@override
Future<void> handle(WebsocketResponse websocketResponse) async {
Expand All @@ -20,24 +15,4 @@ class GuildCreatePacket with Container implements WebsocketPacket {

eventService.controller.add(GuildCreateEvent(guild));
}

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

if (response.statusCode == 200) {
final payload = jsonDecode(response.body);

Map<Snowflake, ModerationRule> rules = {};
for (final element in payload) {
ModerationRule rule = ModerationRule.fromPayload(element);
rules.putIfAbsent(rule.id, () => rule);
}

return rules;
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,4 @@ class GuildRemovePacket with Container implements WebsocketPacket {
eventService.controller.add(GuildDeleteEvent(guild));
client.guilds.cache.remove(guild.id);
}

}
3 changes: 1 addition & 2 deletions lib/src/internal/websockets/packets/guild_update_packet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ class GuildUpdatePacket with Container implements WebsocketPacket {
GuildScheduledEventService guildScheduledEventService = GuildScheduledEventService();
guildScheduledEventService.cache.addAll(before.scheduledEvents.cache);

VanityInvite? vanityInvite = await VanityInvite.sync(websocketResponse.payload['id']);

Guild after = Guild.from(
emojiManager: emojiManager,
Expand All @@ -54,7 +53,6 @@ class GuildUpdatePacket with Container implements WebsocketPacket {
moderationRuleManager: moderationManager,
webhookManager: webhookManager,
payload: websocketResponse.payload,
vanityInvite: vanityInvite,
guildScheduledEventService: guildScheduledEventService
);

Expand All @@ -69,6 +67,7 @@ class GuildUpdatePacket with Container implements WebsocketPacket {
after.rulesChannel = after.channels.cache.get<TextChannel>(after.rulesChannelId);
after.publicUpdatesChannel = after.channels.cache.get<TextChannel>(after.publicUpdatesChannelId);
after.emojis.guild = after;
after.vanity = await VanityInvite.sync(websocketResponse.payload['id']);

eventService.controller.add(GuildUpdateEvent(before, after));
client.guilds.cache.set(after.id, after);
Expand Down