Skip to content

Commit

Permalink
feat: Work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
LeadcodeDev committed Jun 16, 2022
1 parent f29d3d2 commit e53d040
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export 'src/api/channels/text_channel.dart' show TextChannel;
export 'src/api/channels/category_channel.dart' show CategoryChannel;

export 'src/api/message.dart' show Message;
export 'src/api/message_embed.dart' show MessageEmbed;
export 'src/api/message_embed.dart' show MessageEmbed, Footer, Image, Author, Field;
export 'src/api/color.dart' show Color;

export 'src/api/emoji.dart' show Emoji;
Expand Down
9 changes: 4 additions & 5 deletions lib/src/api/client/mineral_client.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'dart:convert';

import 'package:http/http.dart';
import 'package:mineral/api.dart';
import 'package:mineral/core.dart';
import 'package:mineral/src/api/managers/guild_manager.dart';
Expand Down Expand Up @@ -65,12 +64,12 @@ class MineralClient {
Future<void> registerGuildCommands ({ required Guild guild, required List<MineralCommand> commands}) async {
Http http = ioc.singleton(Service.http);

print(jsonEncode(commands.map((command) => command.toJson()).toList()));

await http.put(
Response response = await http.put(
url: "/applications/${application.id}/guilds/${guild.id}/commands",
payload: commands.map((command) => command.toJson()).toList()
);

print(response.body);
}

factory MineralClient.from({ required dynamic payload }) {
Expand Down
35 changes: 34 additions & 1 deletion lib/src/api/interactions/command_interaction.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'package:http/http.dart';
import 'package:mineral/api.dart';
import 'package:mineral/core.dart';
import 'package:mineral/src/api/interactions/interaction.dart';

class CommandInteraction extends Interaction {
Expand Down Expand Up @@ -48,9 +50,40 @@ class CommandInteraction extends Interaction {
return data[optionName]['value'];
}

Future<void> reply ({ String? content, List<MessageEmbed>? embeds, List<Row>? components, bool? tts, bool? private }) async {
Http http = ioc.singleton(Service.http);

List<dynamic> embedList = [];
if (embeds != null) {
for (MessageEmbed element in embeds) {
embedList.add(element.toJson());
}
}

List<dynamic> componentList = [];
if (components != null) {
for (Row element in components) {
componentList.add(element.toJson());
}
}

Response response = await http.post(url: "/interactions/$id/$token/callback", payload: {
'type': InteractionCallbackType.channelMessageWithSource.value,
'data': {
'tts': tts ?? false,
'content': content,
'embeds': embeds != null ? embedList : [],
'components': components != null ? componentList : [],
'flags': private != null && private == true ? 1 << 6 : null,
}
});

print(response.body);
}

factory CommandInteraction.from({ required User user, required dynamic payload }) {
return CommandInteraction(
id: payload['data']['id'],
id: payload['id'],
applicationId: payload['application_id'],
type: InteractionType.values.firstWhere((type) => type.value == payload['type']),
identifier: payload['data']['name'],
Expand Down
13 changes: 13 additions & 0 deletions lib/src/api/interactions/interaction.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import 'package:mineral/api.dart';

enum InteractionCallbackType {
pong(1),
channelMessageWithSource(4),
deferredChannelMessageWithSource(5),
deferredUpdateMessage(6),
updateMessage(7),
applicationCommandAutocompleteResult(8),
modal(9);

final int value;
const InteractionCallbackType(this.value);
}

class Interaction {
Snowflake applicationId;
int version;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/api/message_embed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Field {
String value;
bool? inline;

Field({ required this.name, required this.value, required this.inline });
Field({ required this.name, required this.value, this.inline });

Object toJson () => {
'name': name,
Expand Down
8 changes: 8 additions & 0 deletions lib/src/api/user.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:mineral/api.dart';
import 'package:mineral/src/constants.dart';

class User {
Snowflake id;
Expand All @@ -20,6 +21,13 @@ class User {
required this.avatar,
});

String getDisplayAvatarUrl () {
return "${Constants.cdnUrl}/avatars/$id/$avatar";
}

@override
String toString () => "<@$id>";

factory User.from(dynamic payload) {
return User(
id: payload['id'],
Expand Down
1 change: 0 additions & 1 deletion lib/src/internal/entities/command_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ class CommandManager {
'commandClass': classCommand,
});
}

}

if (reflectee is Option) {
Expand Down
6 changes: 5 additions & 1 deletion lib/src/internal/websockets/packets/interaction_create.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:convert';
import 'dart:mirrors';

import 'package:mineral/api.dart';
Expand All @@ -16,6 +17,7 @@ class InteractionCreate implements WebsocketPacket {
MineralClient client = ioc.singleton(Service.client);

dynamic payload = websocketResponse.payload;
print(jsonEncode(payload));

Guild? guild = client.guilds.cache.get(payload['guild_id']);
GuildMember? member = guild?.members.cache.get(payload['member']['user']['id']);
Expand All @@ -38,7 +40,9 @@ class InteractionCreate implements WebsocketPacket {
}
}

walk(payload['data']['options']);
if (payload['data']['options'] != null) {
walk(payload['data']['options']);
}

dynamic handle = manager.handlers[identifier];
reflect(handle['commandClass']).invoke(handle['symbol'], [commandInteraction]);
Expand Down

0 comments on commit e53d040

Please sign in to comment.