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

feat: References #110

Merged
merged 9 commits into from
May 18, 2023
19 changes: 10 additions & 9 deletions lib/src/api/managers/thread_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ import 'dart:convert';
import 'package:http/http.dart';
import 'package:mineral/core.dart';
import 'package:mineral/core/api.dart';
import 'package:mineral/core/builders.dart';
import 'package:mineral/exception.dart';
import 'package:mineral/framework.dart';
import 'package:mineral/src/api/managers/cache_manager.dart';
import 'package:mineral/src/internal/mixins/mineral_client.dart';
import 'package:mineral_ioc/ioc.dart';

class ThreadManager extends CacheManager<ThreadChannel> {
Expand Down Expand Up @@ -35,13 +33,16 @@ class ThreadManager extends CacheManager<ThreadChannel> {
return cache;
}

Future<ThreadChannel?> create<T extends GuildChannel> ({ Snowflake? messageId, String? label }) async {
MineralClient client = ioc.use<MineralClient>();
return await client.createChannel(_guildId, ChannelBuilder({
'name': label,
'auto_archive_duration': '60',
'type': ChannelType.guildPublicThread.value
}));
Future<ThreadChannel?> create ({ required String label, required TextBasedChannel channel, int autoArchiveDuration = 60, bool isPrivate = false }) async {
Response response = await ioc.use<DiscordApiHttpService>().post(url: '/channels/${channel.id}/threads')
.payload({
'name': label,
'auto_archive_duration': autoArchiveDuration,
'type': isPrivate ? ChannelType.guildPrivateThread.value : ChannelType.guildPublicThread.value,
'invitable': isPrivate,
}).build();

return ioc.use<MineralClient>().guilds.cache.getOrFail(_guildId).channels.cache.getOrFail(jsonDecode(response.body)['id']);
}

Future<ThreadChannel> resolve (Snowflake id) async {
Expand Down
3 changes: 2 additions & 1 deletion lib/src/api/messages/message.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/core.dart';
import 'package:mineral/core/api.dart';
import 'package:mineral/core/builders.dart';
import 'package:mineral/framework.dart';
import 'package:mineral/src/api/builders/component_wrapper.dart';
import 'package:mineral/src/api/managers/message_reaction_manager.dart';
import 'package:mineral/src/api/messages/message_attachment.dart';
Expand Down Expand Up @@ -85,7 +86,7 @@ class Message extends PartialMessage<TextBasedChannel> {
})
.build();

return ThreadChannel.fromPayload(jsonDecode(response.body));
return channel.guild.channels.cache.getOrFail(jsonDecode(response.body)['id']);
}

Future<void> pin (Snowflake webhookId) async {
Expand Down
11 changes: 11 additions & 0 deletions lib/src/constants.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:mineral_ioc/ioc.dart';

enum OpCode {
dispatch(0),
heartbeat(1),
Expand Down Expand Up @@ -87,4 +89,13 @@ class Constants {

// Version of API
static const int apiVersion = 10;

// Name of Minéral
static const String name = "Mineral";

// Git of Minéral
static const String githubURL = "https://github.com/mineral-dart";

// Version of Minéral
static const String version = "NO VERSION";
}
3 changes: 2 additions & 1 deletion lib/src/internal/kernel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ class Kernel with Container {
container.bind((_) => _console);
await _environment.load();

final DiscordApiHttpService http = DiscordApiHttpService('https://discord.com/api');
final DiscordApiHttpService http = DiscordApiHttpService('https://discord.com/api/v${Constants.apiVersion}');
http.defineHeader(header: 'Content-Type', value: 'application/json');
http.defineHeader(header: 'User-Agent', value: '${Constants.name} (${Constants.githubURL}, ${Constants.version})');
container.bind((_) => http);

String? token = _environment.get('APP_TOKEN');
Expand Down
6 changes: 3 additions & 3 deletions lib/src/internal/websockets/sharding/shard_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class ShardManager extends MineralService {
/// number is provided, we use [Discord recommendations](https://discord.com/developers/docs/topics/gateway#get-gateway-bot)
Future<void> start ({ int? shardsCount }) async {
http.defineHeader(header: 'Authorization', value: 'Bot $_token');
AuthenticationResponse response = await getBotGateway(Constants.apiVersion);
AuthenticationResponse response = await getBotGateway();

_gatewayURL = response.url;
maxConcurrency = response.maxConcurrency;
Expand Down Expand Up @@ -71,8 +71,8 @@ class ShardManager extends MineralService {
}
}

Future<AuthenticationResponse> getBotGateway(int version) async {
Response response = await http.get(url: '/v$version/gateway/bot').build();
Future<AuthenticationResponse> getBotGateway() async {
Response response = await http.get(url: '/gateway/bot').build();
return AuthenticationResponse.fromResponse(response);
}

Expand Down