From ca9e6741de0b461aa410fa70684e5568df127884 Mon Sep 17 00:00:00 2001 From: KillerBossOrig Date: Sun, 20 Aug 2023 15:16:21 +0200 Subject: [PATCH 1/2] Add interactions manager --- example/main.dart | 8 ++--- lib/src/classes/channel/channel.dart | 2 +- lib/src/classes/commands/command.dart | 15 ++++++++++ lib/src/classes/commands/command_manager.dart | 16 ++++++++++ lib/src/classes/index.dart | 29 +++++++++++++++++++ lib/src/classes/user/user_manager.dart | 1 - lib/src/main.dart | 4 +++ lib/src/requests.dart | 27 ++++++++++++++++- lib/tn_discord.dart | 2 +- 9 files changed, 95 insertions(+), 9 deletions(-) create mode 100644 lib/src/classes/commands/command.dart create mode 100644 lib/src/classes/commands/command_manager.dart create mode 100644 lib/src/classes/index.dart diff --git a/example/main.dart b/example/main.dart index 74be1c8..a06c81d 100644 --- a/example/main.dart +++ b/example/main.dart @@ -1,6 +1,3 @@ -import 'package:tn_discord/src/classes/events.dart'; -import 'package:tn_discord/src/classes/message/message.dart'; -import 'package:tn_discord/src/classes/message/message_sent.dart'; import 'package:tn_discord/tn_discord.dart'; main() async { @@ -13,12 +10,13 @@ main() async { ]) ); - client.login("Your Bot Token"); + client.login("ODkwMjg2NzMzMTE0MDkzNTg4.GQqDv6.QWoa1Sd8S0wz4QjeKNfr6prnQgkMyk8mXv-z1g"); client.on("READY", (data) async { // Let we get a guild name - var a = await client.guilds.fetch("a guild id"); + var a = await client.guilds.fetch("913388008307302410"); print(a.name); + client.commands.create(Command(name: "test", description: "test1")); }); client.on(Events.MessageCreate, (MessageSent message) async { diff --git a/lib/src/classes/channel/channel.dart b/lib/src/classes/channel/channel.dart index 27e9396..dc5f3fc 100644 --- a/lib/src/classes/channel/channel.dart +++ b/lib/src/classes/channel/channel.dart @@ -13,7 +13,7 @@ class Channel { } Future send(Message message) async { - var res = await _sender.send(message, id); + var res = await _sender.sendMessage(message, id); return MessageSent(message, res["author"]["id"], id, res["id"]); } } \ No newline at end of file diff --git a/lib/src/classes/commands/command.dart b/lib/src/classes/commands/command.dart new file mode 100644 index 0000000..14ecf69 --- /dev/null +++ b/lib/src/classes/commands/command.dart @@ -0,0 +1,15 @@ +class Command { + late String name; + late String description; + late int type; + + Command({ required this.name, required this.description, this.type = 1 }); + + exportable () { + return { + "type": type, + "name": name, + "description": description + }; + } +} \ No newline at end of file diff --git a/lib/src/classes/commands/command_manager.dart b/lib/src/classes/commands/command_manager.dart new file mode 100644 index 0000000..31bbafa --- /dev/null +++ b/lib/src/classes/commands/command_manager.dart @@ -0,0 +1,16 @@ +import 'command.dart'; +import '../../requests.dart'; + +class CommandManager { + final Sender _sender; + + CommandManager(this._sender); + + create(Command cmd, { String? guildID }) async { + if (guildID != null) { + await _sender.createGuildCommands(guildID, cmd); + } else { + await _sender.createGlobalCommands(cmd); + } + } +} \ No newline at end of file diff --git a/lib/src/classes/index.dart b/lib/src/classes/index.dart new file mode 100644 index 0000000..07f0265 --- /dev/null +++ b/lib/src/classes/index.dart @@ -0,0 +1,29 @@ +export "channel/channel.dart"; +// // export "channel/channel_manager.dart"; + +export "commands/command.dart"; +// // export "commands/command_manager.dart"; + +export "guild/guild.dart"; +// // export "guild/guild_manager.dart"; +export "guild/unavailable_guild.dart"; + +export "images/guild_icon.dart"; +export "images/guild_splash.dart"; + +export "member/member.dart"; +// // export "member/member_manager.dart"; + +export "message/embed.dart"; +export "message/message.dart"; +export "message/message_sent.dart"; + +export "role/role.dart"; +// // export "role/role_manager.dart"; + +export "user/user.dart"; +// // export "user/user_manager.dart"; + +export "events.dart"; +export "gateway_intents_bits.dart"; +export "interaction.dart"; diff --git a/lib/src/classes/user/user_manager.dart b/lib/src/classes/user/user_manager.dart index cb7ee37..6b989ee 100644 --- a/lib/src/classes/user/user_manager.dart +++ b/lib/src/classes/user/user_manager.dart @@ -4,7 +4,6 @@ import "package:http/http.dart" as http; import '../../../tn_discord.dart'; import '../../collection.dart'; -import './user.dart'; class UserManager { final Collection cache = Collection(); diff --git a/lib/src/main.dart b/lib/src/main.dart index 9c5f6f8..a41f243 100644 --- a/lib/src/main.dart +++ b/lib/src/main.dart @@ -3,6 +3,7 @@ import 'dart:io' if (dart.library.html) 'dart:html'; import "dart:convert"; import "package:events_emitter/events_emitter.dart"; import "package:tn_discord/src/classes/channel/channel_manager.dart"; +import "package:tn_discord/src/classes/commands/command_manager.dart"; import "classes/guild/guild.dart"; import "classes/guild/guild_manager.dart"; @@ -42,6 +43,7 @@ class Client extends EventEmitter { late ChannelManager channels; bool ready = false; late User user; + late CommandManager commands; /// Create a new Client. /// [intents] Intents to enable for this connection, it's a multiple of two. @@ -133,7 +135,9 @@ class Client extends EventEmitter { resumeGatewayURL = event["d"]["resume_gateway_url"]; sessionID = event["d"]["session_id"]; user = User(event["d"]["user"]); + sender.setID(user.id); ready = true; + commands = CommandManager(sender); break; case "GUILD_CREATE": if (guilds.cache.has(event["d"]["id"])) { diff --git a/lib/src/requests.dart b/lib/src/requests.dart index 3aa9269..4d6aa39 100644 --- a/lib/src/requests.dart +++ b/lib/src/requests.dart @@ -1,3 +1,5 @@ +import 'classes/commands/command.dart'; + import 'classes/message/message.dart'; import 'main.dart'; import 'package:http/http.dart' as http; @@ -38,6 +40,7 @@ class Sender { final String? _token; Map headers = {}; dynamic channels; + String? id; Sender(this._token) { headers = { @@ -46,6 +49,10 @@ class Sender { }; } + setID(String id) { + this.id = id; + } + Future fetchGuilds({ bool withCounts = false }) async { final url = Uri.parse("$apiURL/users/@me/guilds?with_counts=$withCounts"); dynamic res = await http.get(url, headers: headers); @@ -53,7 +60,7 @@ class Sender { return res; } - Future send(Message msg, String cid) async { + Future sendMessage(Message msg, String cid) async { final url = Uri.parse("$apiURL/channels/$cid/messages"); dynamic res = await http.post(url, headers: headers, body: json.encode(msg.exportable())); res = json.decode(res.body); @@ -86,6 +93,24 @@ class Sender { res = json.decode(res.body); return res; } + + Future createGlobalCommands(Command body) async { + dynamic res = await http.post(Uri.parse("$apiURL/applications/$id/commands"), headers: headers, body: json.encode(body.exportable())); + if (res.statusCode != 201 && res.statusCode != 200) { + throw Exception("Error ${res.statusCode} setting the global command\nBody: ${json.decode(res.body)}"); + } + res = json.decode(res.body); + return res; + } + + Future createGuildCommands(String id, Command body) async { + dynamic res = await http.post(Uri.parse("$apiURL/applications/${this.id}/guilds/$id/commands"), headers: headers, body: json.encode(body.exportable())); + if (res.statusCode != 201 && res.statusCode != 200) { + throw Exception("Error ${res.statusCode} setting the guild command\nBody: ${json.decode(res.body)}"); + } + res = json.decode(res.body); + return res; + } } Future interactionReply(String id, String token, Map content) async { diff --git a/lib/tn_discord.dart b/lib/tn_discord.dart index 827f62a..b5d9e78 100644 --- a/lib/tn_discord.dart +++ b/lib/tn_discord.dart @@ -2,4 +2,4 @@ library; export "src/webhook.dart"; export "src/main.dart"; -export "src/classes/gateway_intents_bits.dart"; +export "src/classes/index.dart"; From 1a9a07b053a9411cfc84cc17c56e299928281ddf Mon Sep 17 00:00:00 2001 From: KillerBossOrig Date: Sun, 20 Aug 2023 15:17:22 +0200 Subject: [PATCH 2/2] ... --- example/main.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example/main.dart b/example/main.dart index a06c81d..113afab 100644 --- a/example/main.dart +++ b/example/main.dart @@ -10,7 +10,7 @@ main() async { ]) ); - client.login("ODkwMjg2NzMzMTE0MDkzNTg4.GQqDv6.QWoa1Sd8S0wz4QjeKNfr6prnQgkMyk8mXv-z1g"); + client.login("ODkwMz1g"); client.on("READY", (data) async { // Let we get a guild name