Skip to content

Commit

Permalink
Merge pull request #124 from mineral-dart/feat/utils
Browse files Browse the repository at this point in the history
Utils before release
  • Loading branch information
LeadcodeDev authored Jul 24, 2023
2 parents e27af25 + 2eba7b8 commit e2b1448
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 50 deletions.
18 changes: 1 addition & 17 deletions lib/src/api/interactions/button_interaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@ import 'dart:core';
import 'package:mineral/core/api.dart';
import 'package:mineral/framework.dart';
import 'package:mineral/src/api/messages/partial_message.dart';
import 'package:mineral_ioc/ioc.dart';

import 'package:mineral/core.dart';

class ButtonInteraction extends Interaction {
Snowflake _customId;
PartialMessage? _message;
Snowflake _channelId;

ButtonInteraction(
Expand All @@ -21,31 +17,19 @@ class ButtonInteraction extends Interaction {
super.token,
super._userId,
super._guildId,
this._message,
super._message,
this._customId,
this._channelId,
);

/// Get custom id of this
Snowflake get customId => _customId;

/// Get message [PartialMessage] of this
PartialMessage? get message => _message;

/// Get channel [PartialChannel] of this
PartialChannel get channel => guild != null
? guild!.channels.cache.getOrFail<TextBasedChannel>(_channelId)
: throw UnsupportedError('DM channel is not supported');

@override
Future<void> delete () async {
String mid = message?.id ?? "@original";

await ioc.use<DiscordApiHttpService>()
.destroy(url: "/webhooks/$applicationId/$token/messages/$mid")
.build();
}

factory ButtonInteraction.fromPayload(PartialChannel channel, dynamic payload) {
return ButtonInteraction(
payload['id'],
Expand Down
2 changes: 2 additions & 0 deletions lib/src/api/interactions/command_interaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class CommandInteraction extends Interaction with Client {
super._token,
super._userId,
super._guildId,
super._message,
this._identifier,
this._channelId,
this._data,
Expand Down Expand Up @@ -135,6 +136,7 @@ class CommandInteraction extends Interaction with Client {
payload['token'],
payload['member']?['user']?['id'] ?? payload['user']?['id'],
payload['guild_id'],
null,
payload['data']['name'],
payload['channel_id'],
payload['data'],
Expand Down
1 change: 1 addition & 0 deletions lib/src/api/interactions/context_menu_interaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class ContextMenuInteraction extends Interaction {
super._token,
super._userId,
super._guildId,
super._message,
);

/// Get type of this
Expand Down
8 changes: 2 additions & 6 deletions lib/src/api/interactions/context_message_interaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import 'package:mineral/src/api/interactions/context_menu_interaction.dart';

class ContextMessageInteraction extends ContextMenuInteraction {
Snowflake _channelId;
Message _message;

ContextMessageInteraction(
this._channelId,
this._message,
super._type,
super._id,
super._label,
Expand All @@ -18,18 +16,15 @@ class ContextMessageInteraction extends ContextMenuInteraction {
super._token,
super._userId,
super._guildId,
super._message,
);

/// Get message [Message] of this
Message get message => _message;

/// Get channel [TextBasedChannel] of this
TextBasedChannel get channel => guild!.channels.cache.get(_channelId)!;

factory ContextMessageInteraction.from({ required Message message, required dynamic payload }) {
return ContextMessageInteraction(
payload['channel_id'],
message,
payload['type'],
payload['id'],
payload['data']['name'],
Expand All @@ -39,6 +34,7 @@ class ContextMessageInteraction extends ContextMenuInteraction {
payload['token'],
payload['member']?['user']?['id'] ?? payload['user']?['id'],
payload['guild_id'],
message,
);
}
}
2 changes: 2 additions & 0 deletions lib/src/api/interactions/context_user_interaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class ContextUserInteraction extends ContextMenuInteraction {
super._token,
super._user,
super._guild,
super._message,
);

/// Get target [GuildMember] of this
Expand All @@ -39,6 +40,7 @@ class ContextUserInteraction extends ContextMenuInteraction {
payload['token'],
payload['member']?['user']?['id'] ?? payload['user']?['id'],
payload['guild_id'],
null,
);
}
}
4 changes: 3 additions & 1 deletion lib/src/api/interactions/global_command_interaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ class GlobalCommandInteraction extends CommandInteraction {
super.token,
super.userId,
super.guildId,
super.message,
super.identifier,
super.channelId,
super.data,
super.params
super.params,
);

/// Get channel [PartialChannel] of this
Expand Down Expand Up @@ -45,6 +46,7 @@ class GlobalCommandInteraction extends CommandInteraction {
payload['token'],
payload['member']?['user']?['id'] ?? payload['user']?['id'],
payload['guild_id'],
null,
payload['data']['name'],
payload['channel_id'],
payload['data'],
Expand Down
2 changes: 2 additions & 0 deletions lib/src/api/interactions/guild_command_interaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class GuildCommandInteraction extends CommandInteraction {
super.token,
super.userId,
super.guildId,
super.message,
super.identifier,
super.channelId,
super.data,
Expand Down Expand Up @@ -45,6 +46,7 @@ class GuildCommandInteraction extends CommandInteraction {
payload['token'],
payload['member']?['user']?['id'] ?? payload['user']?['id'],
payload['guild_id'],
null,
payload['data']['name'],
payload['channel_id'],
payload['data'],
Expand Down
48 changes: 40 additions & 8 deletions lib/src/api/interactions/interaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:mineral/core/api.dart';
import 'package:mineral/core/builders.dart';
import 'package:mineral/framework.dart';
import 'package:mineral/src/api/messages/message_parser.dart';
import 'package:mineral/src/api/messages/partial_message.dart';
import 'package:mineral_ioc/ioc.dart';

/// ### Interaction Type
Expand Down Expand Up @@ -36,8 +37,9 @@ class Interaction {
String _token;
Snowflake? _userId;
Snowflake? _guildId;
PartialMessage? _message;

Interaction(this._id, this._label, this._applicationId, this._version, this._typeId, this._token, this._userId, this._guildId);
Interaction(this._id, this._label, this._applicationId, this._version, this._typeId, this._token, this._userId, this._guildId, this._message);

/// Get id [Snowflake] of this
Snowflake get id => _id;
Expand All @@ -60,6 +62,9 @@ class Interaction {
/// Get guild [Guild] of this
Guild? get guild => ioc.use<MineralClient>().guilds.cache.get(_guildId);

/// Get message [PartialMessage] of this
PartialMessage? get message => _message;

/// Get user [User] of this
User get user => _guildId != null
? guild!.members.cache.getOrFail(_userId).user
Expand Down Expand Up @@ -112,10 +117,14 @@ class Interaction {
}

/// ### Responds to this by a deferred [Message] (Show a loading state to the user)
Future<Interaction> deferredReply () async {
Future<Interaction> deferredReply ({ bool private = false }) async {
await ioc.use<DiscordApiHttpService>().post(url: "/interactions/$id/$token/callback")
.payload({ 'type': InteractionCallbackType.deferredChannelMessageWithSource.value })
.build();
.payload({
'type': InteractionCallbackType.deferredChannelMessageWithSource.value,
'data': {
'flags': private ? 1 << 6 : null
}
}).build();

return this;
}
Expand All @@ -128,8 +137,9 @@ class Interaction {
/// ```
Future<Interaction> updateReply ({ String? content, List<EmbedBuilder>? embeds, ComponentBuilder? components, List<AttachmentBuilder>? attachments }) async {
dynamic messagePayload = MessageParser(content, embeds, components, attachments, null).toJson();
String mid = message?.id ?? "@original";

await ioc.use<DiscordApiHttpService>().patch(url: "/webhooks/$applicationId/$token/messages/@original")
await ioc.use<DiscordApiHttpService>().patch(url: "/webhooks/$applicationId/$token/messages/$mid")
.files(messagePayload['files'])
.payload(messagePayload['payload'])
.build();
Expand All @@ -148,12 +158,33 @@ class Interaction {
/// });
/// ```
Future<void> delete () async {
String mid = message?.id ?? "@original";

await ioc.use<DiscordApiHttpService>()
.destroy(url: "/webhooks/$applicationId/$token/messages/@original")
.build();
.destroy(url: "/webhooks/$applicationId/$token/messages/$mid")
.build();
}
/// ### Shows no response (and error)
///
/// Example :
/// ```dart
/// await interaction.noReply();
/// ```
Future<void> noReply ({ bool private = false }) async {
await ioc.use<DiscordApiHttpService>().post(url: "/interactions/$id/$token/callback")
.payload({
'type': InteractionCallbackType.deferredUpdateMessage.value,
'data': {
'flags': private ? 1 << 6 : null
}
}).build();
}
factory Interaction.from({ required dynamic payload, required PartialChannel? channel }) {
PartialMessage? message;
if(payload['message'] != null) {
message = (payload['guild_id'] != null ? Message.from(channel: channel as GuildChannel, payload: payload['message']) : DmMessage.from(channel: channel as DmChannel, payload: payload['message'])) as PartialMessage<PartialChannel>;
}

factory Interaction.from({ required dynamic payload }) {
return Interaction(
payload['id'],
null,
Expand All @@ -163,6 +194,7 @@ class Interaction {
payload['token'],
payload['member']?['user']?['id'] ?? payload['user']?['id'],
payload['guild_id'],
message
);
}
}
17 changes: 1 addition & 16 deletions lib/src/api/interactions/menus/select_menu_interaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ import 'dart:core';

import 'package:mineral/core/api.dart';
import 'package:mineral/src/api/messages/partial_message.dart';
import 'package:mineral_ioc/ioc.dart';

import '../../../../core.dart';

class SelectMenuInteraction extends Interaction {
PartialMessage? _message;
Snowflake _customId;
PartialChannel _channel;

Expand All @@ -20,26 +16,15 @@ class SelectMenuInteraction extends Interaction {
super._token,
super._user,
super._guild,
this._message,
super._message,
this._customId,
this._channel,
);

PartialMessage? get message => _message;

Snowflake get customId => _customId;

PartialChannel get channel => _channel;

@override
Future<void> delete () async {
String mid = message?.id ?? "@original";

await ioc.use<DiscordApiHttpService>()
.destroy(url: "/webhooks/$applicationId/$token/messages/$mid")
.build();
}

factory SelectMenuInteraction.from({ required dynamic payload, required PartialChannel channel }) {
return SelectMenuInteraction(
payload['id'],
Expand Down
2 changes: 2 additions & 0 deletions lib/src/api/interactions/modal_interaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class ModalInteraction extends Interaction {
super._token,
super._userId,
super._guildId,
super._message,
this._customId,
this._channelId,
);
Expand Down Expand Up @@ -49,6 +50,7 @@ class ModalInteraction extends Interaction {
payload['token'],
payload['member']?['user']?['id'] ?? payload['user']?['id'],
payload['guild_id'],
null,
payload['data']['custom_id'],
payload['channel_id'],
);
Expand Down
2 changes: 1 addition & 1 deletion lib/src/api/users/user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class User {
/// Returns the username of the user as a [String].
String get username => _username;

String? get globalName => _globalName;
String? get globalName => _globalName ?? _username;

/// Returns the username of the user as a [String].
@deprecated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class InteractionCreatePacket with Container implements WebsocketPacket {

Guild? guild = client.guilds.cache.get(payload['guild_id']);
GuildMember? member = guild?.members.cache.get(payload['member']['user']['id']);
TextBasedChannel? channel = guild?.channels.cache.get(payload['channel_id']);

if (payload['type'] == InteractionType.applicationCommand.value && payload['data']['type'] == ApplicationCommandType.chatInput.value) {
_executeCommandInteraction(payload);
Expand Down Expand Up @@ -58,7 +59,7 @@ class InteractionCreatePacket with Container implements WebsocketPacket {
}

if (member != null) {
final Interaction interaction = Interaction.from(payload: payload);
final Interaction interaction = Interaction.from(payload: payload, channel: channel);
eventService.controller.add(InteractionCreateEvent(interaction));
}
}
Expand Down

0 comments on commit e2b1448

Please sign in to comment.