Add interactions manager

This commit is contained in:
KillerBossOrig 2023-08-20 15:16:21 +02:00
parent af720569bc
commit ca9e6741de
9 changed files with 95 additions and 9 deletions

View file

@ -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'; import 'package:tn_discord/tn_discord.dart';
main() async { main() async {
@ -13,12 +10,13 @@ main() async {
]) ])
); );
client.login("Your Bot Token"); client.login("ODkwMjg2NzMzMTE0MDkzNTg4.GQqDv6.QWoa1Sd8S0wz4QjeKNfr6prnQgkMyk8mXv-z1g");
client.on("READY", (data) async { client.on("READY", (data) async {
// Let we get a guild name // 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); print(a.name);
client.commands.create(Command(name: "test", description: "test1"));
}); });
client.on(Events.MessageCreate, (MessageSent message) async { client.on(Events.MessageCreate, (MessageSent message) async {

View file

@ -13,7 +13,7 @@ class Channel {
} }
Future<MessageSent> send(Message message) async { Future<MessageSent> 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"]); return MessageSent(message, res["author"]["id"], id, res["id"]);
} }
} }

View file

@ -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
};
}
}

View file

@ -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);
}
}
}

View file

@ -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";

View file

@ -4,7 +4,6 @@ import "package:http/http.dart" as http;
import '../../../tn_discord.dart'; import '../../../tn_discord.dart';
import '../../collection.dart'; import '../../collection.dart';
import './user.dart';
class UserManager { class UserManager {
final Collection cache = Collection(); final Collection cache = Collection();

View file

@ -3,6 +3,7 @@ import 'dart:io' if (dart.library.html) 'dart:html';
import "dart:convert"; import "dart:convert";
import "package:events_emitter/events_emitter.dart"; import "package:events_emitter/events_emitter.dart";
import "package:tn_discord/src/classes/channel/channel_manager.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.dart";
import "classes/guild/guild_manager.dart"; import "classes/guild/guild_manager.dart";
@ -42,6 +43,7 @@ class Client extends EventEmitter {
late ChannelManager channels; late ChannelManager channels;
bool ready = false; bool ready = false;
late User user; late User user;
late CommandManager commands;
/// Create a new Client. /// Create a new Client.
/// [intents] Intents to enable for this connection, it's a multiple of two. /// [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"]; resumeGatewayURL = event["d"]["resume_gateway_url"];
sessionID = event["d"]["session_id"]; sessionID = event["d"]["session_id"];
user = User(event["d"]["user"]); user = User(event["d"]["user"]);
sender.setID(user.id);
ready = true; ready = true;
commands = CommandManager(sender);
break; break;
case "GUILD_CREATE": case "GUILD_CREATE":
if (guilds.cache.has(event["d"]["id"])) { if (guilds.cache.has(event["d"]["id"])) {

View file

@ -1,3 +1,5 @@
import 'classes/commands/command.dart';
import 'classes/message/message.dart'; import 'classes/message/message.dart';
import 'main.dart'; import 'main.dart';
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
@ -38,6 +40,7 @@ class Sender {
final String? _token; final String? _token;
Map<String, String> headers = {}; Map<String, String> headers = {};
dynamic channels; dynamic channels;
String? id;
Sender(this._token) { Sender(this._token) {
headers = { headers = {
@ -46,6 +49,10 @@ class Sender {
}; };
} }
setID(String id) {
this.id = id;
}
Future fetchGuilds({ bool withCounts = false }) async { Future fetchGuilds({ bool withCounts = false }) async {
final url = Uri.parse("$apiURL/users/@me/guilds?with_counts=$withCounts"); final url = Uri.parse("$apiURL/users/@me/guilds?with_counts=$withCounts");
dynamic res = await http.get(url, headers: headers); dynamic res = await http.get(url, headers: headers);
@ -53,7 +60,7 @@ class Sender {
return res; return res;
} }
Future send(Message msg, String cid) async { Future sendMessage(Message msg, String cid) async {
final url = Uri.parse("$apiURL/channels/$cid/messages"); final url = Uri.parse("$apiURL/channels/$cid/messages");
dynamic res = await http.post(url, headers: headers, body: json.encode(msg.exportable())); dynamic res = await http.post(url, headers: headers, body: json.encode(msg.exportable()));
res = json.decode(res.body); res = json.decode(res.body);
@ -86,6 +93,24 @@ class Sender {
res = json.decode(res.body); res = json.decode(res.body);
return res; 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<String, dynamic> content) async { Future interactionReply(String id, String token, Map<String, dynamic> content) async {

View file

@ -2,4 +2,4 @@ library;
export "src/webhook.dart"; export "src/webhook.dart";
export "src/main.dart"; export "src/main.dart";
export "src/classes/gateway_intents_bits.dart"; export "src/classes/index.dart";