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

Panda_Guerrier Pull Request #77

Merged
merged 13 commits into from
Feb 18, 2023
1 change: 1 addition & 0 deletions lib/src/api/channels/partial_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ enum ChannelType {

final int value;
const ChannelType(this.value);
bool equals (dynamic value) => this == value;
PandaGuerrier marked this conversation as resolved.
Show resolved Hide resolved
}

class ChannelWrapper {
Expand Down
25 changes: 25 additions & 0 deletions lib/src/api/channels/text_based_channel.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import 'package:mineral/core/api.dart';
import 'package:mineral/src/api/builders/channel_builder.dart';
import 'package:mineral/src/api/managers/webhook_manager.dart';
import 'package:http/http.dart';
import 'package:mineral_ioc/ioc.dart';
import 'package:mineral/core.dart';
import 'package:mineral_cli/mineral_cli.dart';

class TextBasedChannel extends PartialTextChannel {
final bool _nsfw;
Expand Down Expand Up @@ -31,4 +35,25 @@ class TextBasedChannel extends PartialTextChannel {
Future<void> setNsfw (bool value) async {
await update(ChannelBuilder({ 'nsfw': value }));
}

Future<void> bulkDelete(double number) async {
PandaGuerrier marked this conversation as resolved.
Show resolved Hide resolved
if (number > 200) {
PandaGuerrier marked this conversation as resolved.
Show resolved Hide resolved
return ioc.use<MineralCli>().console.error('The number $number is too high, and exceeds the limit of 200 maximum messages');
PandaGuerrier marked this conversation as resolved.
Show resolved Hide resolved
}

final Map<Snowflake, Message> messagesFetch = await messages.fetch();
List<Snowflake> msg = [];
PandaGuerrier marked this conversation as resolved.
Show resolved Hide resolved

int i = 0;
for (Message message in messagesFetch.values) {
PandaGuerrier marked this conversation as resolved.
Show resolved Hide resolved
if(i <= number - 1) {
msg.add(message.id);
i++;
}
}

await ioc.use<HttpService>().post(url: "/channels/${id}/messages/bulk-delete", payload: {
'messages': msg
});
}
}
11 changes: 11 additions & 0 deletions lib/src/internal/websockets/events/dm_channel_create_event.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'package:mineral/core/api.dart';
PandaGuerrier marked this conversation as resolved.
Show resolved Hide resolved
import 'package:mineral/framework.dart';
import 'package:mineral/src/api/channels/dm_channel.dart';

class DMChannelCreateEvent extends Event {
final DmChannel _channel;

DMChannelCreateEvent(this._channel);

DmChannel get channel => _channel;
}
23 changes: 19 additions & 4 deletions lib/src/internal/websockets/packets/channel_create_packet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,35 @@ import 'package:mineral/src/internal/mixins/container.dart';
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 'package:mineral/src/api/channels/dm_channel.dart';
import 'package:mineral/src/api/channels/news_channel.dart';

class ChannelCreatePacket with Container implements WebsocketPacket {
import 'package:mineral/src/api/channels/stage_channel.dart';
import 'package:mineral/src/internal/websockets/events/dm_channel_create_event.dart';

class ChannelCreatePacket with Container, Console implements WebsocketPacket {
@override
Future<void> handle(WebsocketResponse websocketResponse) async {
EventService eventService = container.use<EventService>();
MineralClient client = container.use<MineralClient>();

dynamic payload = websocketResponse.payload;

final ChannelType? channelType = ChannelType.values.firstWhere((element) => element.value == payload['type']);
PandaGuerrier marked this conversation as resolved.
Show resolved Hide resolved
Guild? guild = client.guilds.cache.get(payload['guild_id']);
GuildChannel channel = ChannelWrapper.create(payload);

guild?.channels.cache.set(channel.id, channel);
final channel = ChannelWrapper.create(payload);

eventService.controller.add(ChannelCreateEvent(channel));
if (channelType == null) {
PandaGuerrier marked this conversation as resolved.
Show resolved Hide resolved
return null;
}

guild?.channels.cache.set(channel.id, channel);

if(channelType.equals(ChannelType.private)) {
PandaGuerrier marked this conversation as resolved.
Show resolved Hide resolved
eventService.controller.add(DMChannelCreateEvent(channel));
} else {
eventService.controller.add(ChannelCreateEvent(channel));
}
}
}