-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from mineral-dart/feat-implement-invitations
feat: Implement invitations
- Loading branch information
Showing
23 changed files
with
366 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
enum ClientPermission { | ||
createInstantInvite(1 << 0), | ||
kickMembers(1 << 1), | ||
banMembers(1 << 2), | ||
administrator(1 << 3), | ||
manageChannels(1 << 4), | ||
manageGuilds(1 << 5), | ||
addReactions(1 << 6), | ||
viewAuditChannel(1 << 7), | ||
prioritySpeaker(1 << 8), | ||
stream(1 << 9), | ||
viewChannel(1 << 10), | ||
sendMessages(1 << 11), | ||
sendTtsMessage(1 << 12), | ||
manageMessages(1 << 13), | ||
embedLinks(1 << 14), | ||
attachFiles(1 << 15), | ||
readMessageHistory(1 << 16), | ||
mentionEveryone(1 << 17), | ||
useExternalEmojis(1 << 18), | ||
viewGuildInsights(1 << 19), | ||
connect(1 << 20), | ||
speak(1 << 21), | ||
muteMembers(1 << 22), | ||
deafenMembers(1 << 23), | ||
moveMembers(1 << 24), | ||
useVad(1 << 25), | ||
changeUsername(1 << 26), | ||
managerUsernames(1 << 27), | ||
manageRoles(1 << 28), | ||
manageWebhooks(1 << 29), | ||
manageEmojisAndStickers(1 << 30), | ||
useApplicationCommand(1 << 31), | ||
requestToSpeak(1 << 32), | ||
manageEvents(1 << 33), | ||
manageThreads(1 << 34), | ||
usePublicThreads(1 << 35), | ||
createPublicThreads(1 << 35), | ||
usePrivateThreads(1 << 36), | ||
createPrivateThreads(1 << 36), | ||
useExternalStickers(1 << 37), | ||
sendMessageInThreads(1 << 38), | ||
startEmbeddedActivities(1 << 39), | ||
moderateMembers(1 << 40); | ||
|
||
final int value; | ||
const ClientPermission(this.value); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
enum ClientScope { | ||
activitiesRead('activities.read'), | ||
activitiesWrite('activities.write'), | ||
applicationBuildsRead('applications.builds.read'), | ||
applicationBuildsUpload('applications.builds.upload'), | ||
applicationsCommands('applications.commands'), | ||
applicationsCommandsUpdate('applications.commands.update'), | ||
applicationsCommandsPermissionsUpdate('applications.commands.permissions.update'), | ||
applicationEntitlements('applications.entitlements'), | ||
applicationsStoreUpdate('applications.store.update'), | ||
bot('bot'), | ||
connections('connections'), | ||
dmChannelsRead('dm_channels.read'), | ||
email('email'), | ||
gdmJoin('gdm.join'), | ||
guilds('guilds'), | ||
guildsJoin('guilds.join'), | ||
guildsMembersRead('guilds.members.read'), | ||
identify('identify'), | ||
messagesRead('messages.read'), | ||
relationships('relationships.read'), | ||
roleConnectionsWrite('role_connections.write'), | ||
rpc('rpc'), | ||
rpcActivitiesWrite('rpc.activities.write'), | ||
rpcNotificationsRead('rpc.notifications.read'), | ||
rpcVoiceRead('rpc.voice.read'), | ||
rpcVoiceWrite('rpc.voice.write'), | ||
voice('voice'), | ||
webhookIncoming('webhook.incoming'); | ||
|
||
final String value; | ||
const ClientScope(this.value); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import 'package:mineral/core.dart'; | ||
import 'package:mineral/core/api.dart'; | ||
import 'package:mineral/framework.dart'; | ||
import 'package:mineral/src/api/invites/invite_target_type.dart'; | ||
import 'package:mineral/src/api/invites/wrapped_inviter.dart'; | ||
import 'package:mineral_ioc/ioc.dart'; | ||
|
||
class Invite { | ||
final int _type; | ||
final int _uses; | ||
final bool _temporary; | ||
final int _maxUses; | ||
final int _maxAge; | ||
final Snowflake? _inviterId; | ||
final Snowflake? _targetUserId; | ||
final Snowflake? _guildId; | ||
final String? _expiresAt; | ||
final String _createdAt; | ||
final String _code; | ||
final Snowflake? _channelId; | ||
final int? _targetType; | ||
|
||
Invite( | ||
this._type, | ||
this._uses, | ||
this._temporary, | ||
this._maxUses, | ||
this._maxAge, | ||
this._inviterId, | ||
this._targetUserId, | ||
this._guildId, | ||
this._expiresAt, | ||
this._createdAt, | ||
this._code, | ||
this._channelId, | ||
this._targetType, | ||
); | ||
|
||
Guild get guild => ioc.use<MineralClient>().guilds.cache.getOrFail(_guildId); | ||
|
||
int get type => _type; | ||
|
||
int get uses => _uses; | ||
|
||
bool get isTemporary => _temporary; | ||
|
||
int get maxUses => _maxUses; | ||
|
||
DateTime get maxAge => DateTime.fromMillisecondsSinceEpoch(_maxAge); | ||
|
||
DateTime? get expiresAt => _expiresAt != null | ||
? DateTime.parse(_expiresAt!) | ||
: null; | ||
|
||
DateTime get createdAt => DateTime.parse(_createdAt); | ||
|
||
String get code => _code; | ||
|
||
GuildChannel? get channel => guild.channels.cache.get(_channelId); | ||
|
||
InviteTargetType get targetType => InviteTargetType.values.firstWhere((element) => element.value == _targetType); | ||
|
||
WrappedInviter? getInviter () => _inviterId != null && _guildId != null | ||
? WrappedInviter(_guildId!, _inviterId!) | ||
: null; | ||
|
||
Future<User>? getTargetUser () => _targetUserId != null | ||
? ioc.use<MineralClient>().users.resolve(_targetUserId!) | ||
: null; | ||
|
||
String get url => '${Constants.discordInviteHost}/$_code'; | ||
|
||
Future<void> delete ({ String? reason }) async { | ||
await ioc.use<DiscordApiHttpService>() | ||
.destroy(url: '/invites/$_code') | ||
.auditLog(reason) | ||
.build(); | ||
} | ||
|
||
@override | ||
String toString () => url; | ||
|
||
factory Invite.from(dynamic payload) => Invite( | ||
payload['type'], | ||
payload['uses'], | ||
payload['temporary'], | ||
payload['max_uses'], | ||
payload['max_age'], | ||
payload['inviter']?['id'], | ||
payload['target_user']?['id'], | ||
payload['guild_id'], | ||
payload['expires_at'], | ||
payload['created_at'], | ||
payload['code'], | ||
payload['channel_id'], | ||
payload['target_type'] | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
enum InviteTargetType { | ||
stream(1), | ||
embeddedApplication(2); | ||
|
||
final int value; | ||
const InviteTargetType(this.value); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import 'package:mineral/core/api.dart'; | ||
import 'package:mineral/framework.dart'; | ||
import 'package:mineral_ioc/ioc.dart'; | ||
|
||
class WrappedInviter { | ||
final Snowflake _guildId; | ||
final Snowflake _userId; | ||
|
||
WrappedInviter(this._guildId, this._userId); | ||
|
||
GuildMember? toMember () => ioc.use<MineralClient>().guilds.cache | ||
.get(_guildId)?.members.cache | ||
.get(_userId); | ||
|
||
Future<User> toUser () async => await ioc.use<MineralClient>().users.resolve(_userId); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:http/http.dart'; | ||
import 'package:mineral/core.dart'; | ||
import 'package:mineral/core/api.dart'; | ||
import 'package:mineral/exception.dart'; | ||
import 'package:mineral/framework.dart'; | ||
import 'package:mineral/src/api/invites/invite.dart'; | ||
import 'package:mineral/src/api/managers/cache_manager.dart'; | ||
import 'package:mineral_ioc/ioc.dart'; | ||
|
||
class GuildInviteManager extends CacheManager<Invite> { | ||
final Snowflake? _guildId; | ||
|
||
GuildInviteManager(this._guildId); | ||
|
||
Future<Map<String, Invite>> sync () async { | ||
Response response = await ioc.use<DiscordApiHttpService>() | ||
.get(url: '/guilds/$_guildId/invites') | ||
.build(); | ||
|
||
for (dynamic element in jsonDecode(response.body)) { | ||
Invite invite = Invite.from(element); | ||
cache.set(invite.code, invite); | ||
} | ||
|
||
return cache; | ||
} | ||
|
||
Future<Invite> resolve (Snowflake id) async { | ||
if(cache.containsKey(id)) { | ||
return cache.getOrFail(id); | ||
} | ||
|
||
final Response response = await ioc.use<DiscordApiHttpService>() | ||
.get(url: '/webhooks/$id') | ||
.build(); | ||
|
||
if(response.statusCode == 200) { | ||
final Invite invite = Invite.from(jsonDecode(response.body)); | ||
|
||
cache.putIfAbsent(invite.code, () => invite); | ||
return invite; | ||
} | ||
|
||
throw ApiException('Unable to fetch invite with code #$id'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.