Skip to content

Commit

Permalink
fix: ask for partial message instead of message id and fix delete method
Browse files Browse the repository at this point in the history
  • Loading branch information
PandaGuerrier committed Jun 27, 2023
1 parent 749a35f commit 1749908
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions lib/src/api/interactions/button_interaction.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import 'dart:core';

import 'package:http/http.dart';
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 '../../../core.dart';

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

ButtonInteraction(
Expand All @@ -19,28 +22,32 @@ class ButtonInteraction extends Interaction {
super.token,
super._userId,
super._guildId,
this._messageId,
this._message,
this._customId,
this._channelId,
);

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

/// Get message id of this
Snowflake? get mid => _messageId;

/// Get message [PartialMessage] of this
PartialMessage? get message => guild != null
? (guild?.channels.cache.get(_channelId) as dynamic)?.messages.cache[_messageId]
: ioc.use<MineralClient>().dmChannels.cache.get(_channelId)?.messages.cache.getOrFail(_messageId);
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');

factory ButtonInteraction.fromPayload(dynamic payload) {
@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(GuildChannel channel, dynamic payload) {
return ButtonInteraction(
payload['id'],
null,
Expand All @@ -50,7 +57,7 @@ class ButtonInteraction extends Interaction {
payload['token'],
payload['member']?['user']?['id'] ?? payload['user']?['id'],
payload['guild_id'],
payload['message']?['id'],
(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>?,
payload['data']['custom_id'],
payload['channel_id'],
);
Expand Down

0 comments on commit 1749908

Please sign in to comment.