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

Voice events #31

Merged
merged 6 commits into from
Jul 10, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
7 changes: 4 additions & 3 deletions lib/src/api/guilds/guild_member.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:http/http.dart';
import 'package:mineral/api.dart';
import 'package:mineral/core.dart';
import 'package:mineral/src/api/guilds/guild_role_manager.dart';
import 'package:mineral/src/api/managers/voice_manager.dart';

class GuildMember {
User user;
Expand All @@ -13,7 +14,7 @@ class GuildMember {
bool isPending;
DateTime? timeoutDuration;
MemberRoleManager roles;
Voice voice;
late VoiceManager voice;
late Guild guild;

GuildMember({
Expand Down Expand Up @@ -97,7 +98,7 @@ class GuildMember {
..roles = roles;
}

factory GuildMember.from({ required user, required GuildRoleManager roles, dynamic member, required Snowflake guildId }) {
factory GuildMember.from({ required user, required GuildRoleManager roles, dynamic member, required Snowflake guildId, required VoiceManager voice }) {
MemberRoleManager memberRoleManager = MemberRoleManager(manager: roles, memberId: user.id);
for (var element in (member['roles'] as List<dynamic>)) {
Role? role = roles.cache.get(element);
Expand All @@ -116,7 +117,7 @@ class GuildMember {
isPending: member['pending'] == true,
timeoutDuration: member['communication_disabled_until'] != null ? DateTime.parse(member['communication_disabled_until']) : null,
roles: memberRoleManager,
voice: Voice.from(payload: member),
voice: voice,
);
}
}
26 changes: 17 additions & 9 deletions lib/src/api/managers/member_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:http/http.dart';
import 'package:mineral/api.dart';
import 'package:mineral/core.dart';
import 'package:mineral/src/api/managers/cache_manager.dart';
import 'package:mineral/src/api/managers/voice_manager.dart';

class MemberManager implements CacheManager<GuildMember> {
@override
Expand All @@ -17,19 +18,26 @@ class MemberManager implements CacheManager<GuildMember> {
@override
Future<Map<Snowflake, GuildMember>> sync () async {
Http http = ioc.singleton(ioc.services.http);
cache.clear();

Response response = await http.get(url: "/guilds/$guildId/members");
dynamic payload = jsonDecode(response.body);
if(response.statusCode == 200) {
dynamic payload = jsonDecode(response.body);
final Map<Snowflake, VoiceManager> voiceStateCache = cache.map((key, value) => MapEntry(key, value.voice));

for(dynamic element in payload) {
GuildMember guildMember = GuildMember.from(
user: User.from(element['user']),
roles: guild.roles,
guildId: guild.id
);
cache.clear();

cache.putIfAbsent(guildMember.user.id, () => guildMember);
for(dynamic element in payload) {
VoiceManager? voiceManager = voiceStateCache.get(element['user']['id']);

GuildMember guildMember = GuildMember.from(
user: User.from(element['user']),
roles: guild.roles,
guildId: guild.id,
voice: voiceManager ?? VoiceManager(isMute: element['mute'], isDeaf: element['deaf'], isSelfMute: false, isSelfDeaf: false, hasVideo: false, hasStream: false, channel: null)
vic256 marked this conversation as resolved.
Show resolved Hide resolved
);

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

return cache;
Expand Down
77 changes: 77 additions & 0 deletions lib/src/api/managers/voice_manager.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import 'package:http/http.dart';
import 'package:mineral/api.dart';
import 'package:mineral/core.dart';

class VoiceManager {
late final GuildMember member;
bool isDeaf;
bool isMute;
bool isSelfMute;
bool isSelfDeaf;
bool hasVideo;
bool? hasStream;
VoiceChannel? channel;

VoiceManager({
required this.isMute,
required this.isDeaf,
required this.isSelfMute,
required this.isSelfDeaf,
required this.hasVideo,
required this.hasStream,
required this.channel
});

Future<void> setMute(bool value) async {
final Http http = ioc.singleton(ioc.services.http);
final Response response = await http.patch(
url: '/guilds/${member.guild.id}/members/${member.user.id}',
payload: {'mute': value}
);

if (response.statusCode == 204 || response.statusCode == 200) {
isMute = value;
}
}

Future<void> setDeaf(bool value) async {
final Http http = ioc.singleton(ioc.services.http);
final Response response = await http.patch(
url: '/guilds/${member.guild.id}/members/${member.user.id}',
payload: {'deaf': value}
);

if (response.statusCode == 204 || response.statusCode == 200) {
isDeaf = value;
}
}

Future<void> move(Snowflake channelId) async {
final Http http = ioc.singleton(ioc.services.http);
final Response response = await http.patch(
url: '/guilds/${member.guild.id}/${member.user.id}',
payload: {'channel_id': channelId}
);

if (response.statusCode == 200) {
final VoiceChannel? channel = member.guild.channels.cache.get(channelId);
if (channel != null) {
this.channel = channel;
}
}
}

factory VoiceManager.from(dynamic payload, VoiceChannel? channel) {
return VoiceManager(
isMute: payload['mute'],
isDeaf: payload['deaf'],
isSelfMute: payload['self_mute'],
isSelfDeaf: payload['self_deaf'],
hasVideo: payload['self_video'],
hasStream: payload['self_stream'],
channel: channel
);
}


}
2 changes: 2 additions & 0 deletions lib/src/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ enum PacketType {

memberJoinRequest('GUILD_JOIN_REQUEST_UPDATE'),

voiceStateUpdate('VOICE_STATE_UPDATE'),

resumed('RESUMED');

final String _value;
Expand Down
15 changes: 14 additions & 1 deletion lib/src/internal/entities/event_manager.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:mirrors';

import 'package:mineral/api.dart';
import 'package:mineral/src/api/managers/voice_manager.dart';
import 'package:mineral/src/internal/entities/store_manager.dart';

class EventManager {
Expand Down Expand Up @@ -92,7 +93,19 @@ enum Events {
commandCreate('create::commandInteraction', { 'interaction': CommandInteraction }),
buttonCreate('create::buttonInteraction', { 'interaction': ButtonInteraction }),
modalCreate('create::modalInteraction', { 'interaction': ModalInteraction }),
selectMenuCreate('create::selectMenuInteraction', { 'interaction': SelectMenuInteraction });
selectMenuCreate('create::selectMenuInteraction', { 'interaction': SelectMenuInteraction }),

voiceStateUpdate('update::voice', { 'before': VoiceManager, 'after': VoiceManager }),
voiceConnect('connect::voice', { 'member': GuildMember, 'before': 'VoiceChannel?', 'after': VoiceChannel }),
voiceDisconnect('disconnect::voice', { 'member': GuildMember, 'channel': VoiceChannel }),
memberMuted('mute::voice', { 'member': GuildMember }),
memberUnMuted('unmute::voice', { 'member': GuildMember }),
memberDeaf('deaf::voice', { 'member': GuildMember }),
memberUnDeaf('undeaf::voice', { 'member': GuildMember }),
memberSelfMuted('self::mute::voice', { 'member': GuildMember }),
memberSelfUnMuted('self::unmute::voice', { 'member': GuildMember }),
memberSelfDeaf('self::deaf::voice', { 'member': GuildMember }),
memberSelfUnDeaf('self::undeaf::voice', { 'member': GuildMember });

final String event;
final Map<String, dynamic> params;
Expand Down
18 changes: 16 additions & 2 deletions lib/src/internal/websockets/packets/guild_create.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:mineral/src/api/managers/emoji_manager.dart';
import 'package:mineral/src/api/managers/guild_scheduled_event_manager.dart';
import 'package:mineral/src/api/managers/member_manager.dart';
import 'package:mineral/src/api/managers/moderation_rule_manager.dart';
import 'package:mineral/src/api/managers/voice_manager.dart';
import 'package:mineral/src/api/managers/webhook_manager.dart';
import 'package:mineral/src/internal/entities/command_manager.dart';
import 'package:mineral/src/internal/entities/event_manager.dart';
Expand All @@ -32,6 +33,13 @@ class GuildCreate implements WebsocketPacket {
Role role = Role.from(roleManager: roleManager, payload: item);
roleManager.cache.putIfAbsent(role.id, () => role);
}

Map<Snowflake, VoiceManager> voices = {};
for(dynamic voiceMember in websocketResponse.payload['voice_states']) {
final VoiceManager voiceManager = VoiceManager.from(voiceMember, null);
voices.putIfAbsent(voiceMember['user_id'], () => voiceManager);
vic256 marked this conversation as resolved.
Show resolved Hide resolved
voices.putIfAbsent(voiceMember['channel_id'], () => voiceManager);
}

MemberManager memberManager = MemberManager(guildId: websocketResponse.payload['id']);
for (dynamic member in websocketResponse.payload['members']) {
Expand All @@ -41,7 +49,10 @@ class GuildCreate implements WebsocketPacket {
roles: roleManager,
user: user,
member: member,
guildId: websocketResponse.payload['id']
guildId: websocketResponse.payload['id'],
voice: voices.containsKey(user.id)
? voices.get(user.id)!
: VoiceManager(isMute: member['mute'], isDeaf: member['deaf'], isSelfMute: false, isSelfDeaf: false, hasVideo: false, hasStream: false, channel: null)
);

memberManager.cache.putIfAbsent(guildMember.user.id, () => guildMember);
Expand Down Expand Up @@ -100,7 +111,6 @@ class GuildCreate implements WebsocketPacket {
guild.members.cache.forEach((Snowflake id, GuildMember member) {
member.guild = guild;
member.voice.member = member;
member.voice.channel = guild.channels.cache.get(member.voice.channelId);
});

// Assign guild channels
Expand All @@ -110,6 +120,10 @@ class GuildCreate implements WebsocketPacket {
channel.guild = guild;
channel.parent = channel.parentId != null ? guild.channels.cache.get<CategoryChannel>(channel.parentId) : null;
channel.webhooks.guild = guild;

if(voices.containsKey(id)) {
voices.get(id)!.channel = channel as VoiceChannel;
}
});

moderationManager.guild = guild;
Expand Down
9 changes: 8 additions & 1 deletion lib/src/internal/websockets/packets/guild_member_add.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:mineral/api.dart';
import 'package:mineral/core.dart';
import 'package:mineral/src/api/managers/voice_manager.dart';
import 'package:mineral/src/internal/entities/event_manager.dart';
import 'package:mineral/src/internal/websockets/websocket_packet.dart';
import 'package:mineral/src/internal/websockets/websocket_response.dart';
Expand All @@ -19,7 +20,13 @@ class GuildMemberAdd implements WebsocketPacket {

if(guild != null) {
User user = User.from(payload['user']);
GuildMember member = GuildMember.from(user: user, roles: guild.roles, member: payload, guildId: guild.id);
GuildMember member = GuildMember.from(
user: user,
roles: guild.roles,
member: payload,
guildId: guild.id,
voice: VoiceManager(isMute: payload['mute'], isDeaf: payload['deaf'], isSelfMute: false, isSelfDeaf: false, hasVideo: false, hasStream: false, channel: null)
);
member.guild = guild;

guild.members.cache.putIfAbsent(member.user.id, () => member);
Expand Down
16 changes: 14 additions & 2 deletions lib/src/internal/websockets/packets/guild_member_update.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:mineral/api.dart';
import 'package:mineral/core.dart';
import 'package:mineral/src/api/managers/voice_manager.dart';
import 'package:mineral/src/internal/entities/event_manager.dart';
import 'package:mineral/src/internal/websockets/websocket_packet.dart';
import 'package:mineral/src/internal/websockets/websocket_response.dart';
Expand All @@ -19,12 +20,23 @@ class GuildMemberUpdate implements WebsocketPacket {
if (guild != null) {
GuildMember? before = guild.members.cache.get(payload['user']['id']);

VoiceManager voice = before != null
? before.voice
: VoiceManager(isMute: payload['mute'], isDeaf: payload['deaf'], isSelfMute: false, isSelfDeaf: false, hasVideo: false, hasStream: false, channel: null);

User user = User.from(payload['user']);
GuildMember after = GuildMember.from(user: user, roles: guild.roles, guildId: guild.id, member: payload);
GuildMember after = GuildMember.from(
user: user,
roles: guild.roles,
member: payload,
guildId: guild.id,
voice: voice
);

after.guild = guild;
after.voice.member = after;
after.voice.channel = guild.channels.cache.get(after.voice.channelId);
//after.voice.member = after;
//after.voice.channel = guild.channels.cache.get(after.voice.channelId);

manager.emit(
event: Events.memberUpdate,
Expand Down
Loading