Updating interactions #9
9 changed files with 91 additions and 14 deletions
|
@ -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 {
|
||||
|
|
|
@ -13,7 +13,7 @@ class Channel {
|
|||
}
|
||||
|
||||
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"]);
|
||||
}
|
||||
}
|
15
lib/src/classes/commands/command.dart
Normal file
15
lib/src/classes/commands/command.dart
Normal 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
|
||||
};
|
||||
}
|
||||
}
|
16
lib/src/classes/commands/command_manager.dart
Normal file
16
lib/src/classes/commands/command_manager.dart
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
29
lib/src/classes/index.dart
Normal file
29
lib/src/classes/index.dart
Normal 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";
|
|
@ -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();
|
||||
|
|
|
@ -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"])) {
|
||||
|
|
|
@ -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<String, String> 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;
|
||||
|
|
|
@ -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";
|
||||
|
|
Reference in a new issue