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

Fix: delete interaction message #123

Merged
merged 5 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions lib/src/api/interactions/button_interaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ 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;
Snowflake? _messageId;
PartialMessage? _message;
Snowflake _channelId;

ButtonInteraction(
Expand All @@ -19,28 +21,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(PartialChannel channel, dynamic payload) {
return ButtonInteraction(
payload['id'],
null,
Expand All @@ -50,7 +56,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
12 changes: 11 additions & 1 deletion lib/src/api/interactions/menus/select_menu_interaction.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
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 '../../../../core.dart';

class SelectMenuInteraction extends Interaction {
PartialMessage? _message;
Snowflake _customId;
Expand All @@ -30,6 +31,15 @@ class SelectMenuInteraction extends Interaction {

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
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class InteractionCreatePacket with Container implements WebsocketPacket {
} catch(_) { }
}

final buttonInteraction = ButtonInteraction.fromPayload(payload);
final buttonInteraction = ButtonInteraction.fromPayload(channel!, payload);
final event = ButtonCreateEvent(buttonInteraction);

eventService.controller.add(event);
Expand Down