diff --git a/example/main.dart b/example/main.dart index 74be1c8..113afab 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("ODkwMz1g"); 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 a817acf..f23aa0c 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,7 +43,8 @@ class Client extends EventEmitter { late ChannelManager channels; bool ready = false; late User user; - var commands = {}; + late CommandManager commands; + /// Create a new Client. /// [intents] Intents to enable for this connection, it's a multiple of two. @@ -139,7 +141,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 f9444e9..9660b3a 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; @@ -39,6 +41,7 @@ class Sender { final String id; Map headers = {}; dynamic channels; + String? id; Sender(this._token, this.id) { headers = { @@ -47,6 +50,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); @@ -54,7 +61,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); @@ -88,10 +95,19 @@ class Sender { return res; } - Future setCommands(List commands) async { - dynamic res = await http.put(Uri.parse("$apiURL/applications/$id/commands"), headers: headers, body: json.encode(commands)); - if (res.statusCode != 200 || res.statusCode != 201) { - throw Exception("Error ${res.statusCode} receiving the member"); + 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; 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";