Compare commits
7 commits
Author | SHA1 | Date | |
---|---|---|---|
|
fca1b7792e | ||
|
a70b976313 | ||
1a9a07b053 | |||
ca9e6741de | |||
16eaf7dfc5 | |||
98a87005b0 | |||
af720569bc |
17 changed files with 200 additions and 82 deletions
10
CHANGELOG.md
10
CHANGELOG.md
|
@ -1,3 +1,13 @@
|
|||
# 1.3.0
|
||||
- Renamed `calculateIntents` to `intentsCalculator`
|
||||
- Add set slashcommands
|
||||
- Add GuildDiscoverySplash
|
||||
- Adding Docs in Guild Class
|
||||
- Fix `guild_icon`
|
||||
- Fix `guild_splash`
|
||||
- Removed `base_image`
|
||||
- Removed `notUpdatedGuild` from UnavailableGuild
|
||||
|
||||
# 1.2.0
|
||||
- Add `client`
|
||||
- Add Event Emitter/Listener (`client.on`)
|
||||
|
|
10
README.md
10
README.md
|
@ -2,8 +2,18 @@
|
|||
[![Pub](https://img.shields.io/pub/v/tn_discord?color=red&logo=dart)](https://github.com/ThunderNetworkRaD/discord-dart)
|
||||
|
||||
⚠️ We don't have tested on Flutter & on Web.
|
||||
|
||||
⚠️ This package is work in progress.
|
||||
|
||||
## Package Versions
|
||||
How we use package versions?
|
||||
a.b.c-d
|
||||
|
||||
- a: Big Relase, Rewrites, etc
|
||||
- b: Small Relase, Small Features, like add a class
|
||||
- c: Bug Fixes, Template Fixes, Documentation Updates
|
||||
- d: Beta or Relase Number
|
||||
|
||||
## Credits
|
||||
We took inspiration from [discord.js](https://github.com/discordjs/discord.js) and [Grapes-discord.grapes](https://github.com/BlackdestinyXX/Grapes-discord.grapes).
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,3 +1,4 @@
|
|||
import '../images/guild_discovery_splash.dart';
|
||||
import '../images/guild_icon.dart';
|
||||
|
||||
import '../../requests.dart';
|
||||
|
@ -17,23 +18,28 @@ class Guild {
|
|||
MemberManager? members;
|
||||
RoleManager? roles;
|
||||
|
||||
// Param guild create | fetch | fetch-servers
|
||||
/// The guild's id <br>
|
||||
/// ✅ GuildCreate, GuildUpdate, GuildDelete, FetchOneGuild, FetchAllGuild
|
||||
/// The guild's id
|
||||
late String id;
|
||||
/// Whether the guild is available to access. If it is not available, it indicates a server outage
|
||||
/// ✅ GuildCreate, GuildDelete
|
||||
/// ❎ FetchOneGuild, FetchAllGuild, GuildUpdate
|
||||
late bool unavailable;
|
||||
late String name; // x | x | x
|
||||
late GuildIcon? icon; // x | x | x
|
||||
late String? iconHash; // x | x |
|
||||
String? splashHash; // x | x |
|
||||
GuildSplash? splash;
|
||||
String? discoverySplash; // x | x |
|
||||
bool? appIsOwner; // | | x
|
||||
Future<Member>? owner; // | |
|
||||
String? ownerId; // x | x |
|
||||
bool unavailable = false;
|
||||
/// The name of this guild
|
||||
late String name;
|
||||
/// The icon hash of this guild
|
||||
late String? iconHash;
|
||||
/// The icon of this guild
|
||||
late GuildIcon? icon;
|
||||
/// The hash of the guild invite splash image
|
||||
late String? splashHash;
|
||||
/// The guild invite splash image of this guild
|
||||
late GuildSplash? splash;
|
||||
/// The hash of the guild discovery splash image
|
||||
late String? discoverySplashHash;
|
||||
/// The guild discovery splash image of this guild
|
||||
late GuildDiscoverySplash? discoverySplash;
|
||||
/// The owner of this guild
|
||||
late Future<Member>? owner;
|
||||
/// The owner id of this guild
|
||||
String? ownerId;
|
||||
String? permissions; // | | x
|
||||
String? afkChannelId; // x | x |
|
||||
int? afkTimeout; // x | x |
|
||||
|
@ -66,6 +72,7 @@ class Guild {
|
|||
String? joinedAt; // x | |
|
||||
bool? large; // x | |
|
||||
int? memberCount; // x | |
|
||||
// Param guild create | fetch | fetch-servers
|
||||
|
||||
Guild(this._sender, Map data) {
|
||||
id = data["id"];
|
||||
|
@ -93,27 +100,32 @@ class Guild {
|
|||
roles = RoleManager([]);
|
||||
}
|
||||
|
||||
unavailable = false;
|
||||
name = data["name"];
|
||||
if (data["icon"] != null) {
|
||||
icon = GuildIcon(data["icon"], id);
|
||||
|
||||
iconHash = data["icon_hash"];
|
||||
if (iconHash != null) {
|
||||
icon = GuildIcon(iconHash!, id);
|
||||
} else {
|
||||
icon = null;
|
||||
}
|
||||
iconHash = data["icon_hash"];
|
||||
|
||||
splashHash = data["splash"];
|
||||
if (splashHash != null) {
|
||||
splash = GuildSplash(splashHash.toString(), id);
|
||||
splash = GuildSplash(splashHash!, id);
|
||||
} else {
|
||||
splash = null;
|
||||
}
|
||||
if (data["discovery_splash"] != null) {
|
||||
discoverySplash = data["discovery_splash"];
|
||||
|
||||
discoverySplashHash = data["discovery_splash"];
|
||||
if (discoverySplashHash != null) {
|
||||
discoverySplash = GuildDiscoverySplash(discoverySplashHash!, id);
|
||||
} else {
|
||||
discoverySplash = null;
|
||||
}
|
||||
appIsOwner = data["owner"];
|
||||
|
||||
ownerId = data["owner_id"];
|
||||
if (ownerId != null) {
|
||||
owner = members?.fetch(ownerId.toString());
|
||||
owner = members!.fetch(ownerId.toString());
|
||||
} else {
|
||||
owner = null;
|
||||
}
|
||||
|
|
|
@ -1,11 +1,9 @@
|
|||
import "guild.dart";
|
||||
|
||||
/// A guild involved in a server outage.
|
||||
class UnavailableGuild {
|
||||
/// The guild's id
|
||||
late String id;
|
||||
late bool unavailable;
|
||||
late Guild? notUpdatedGuild;
|
||||
/// Whether the guild is available to access. If it is not available, it indicates a server outage
|
||||
bool unavailable = true;
|
||||
|
||||
UnavailableGuild(this.id, { this.notUpdatedGuild }) {
|
||||
unavailable = true;
|
||||
}
|
||||
UnavailableGuild(this.id);
|
||||
}
|
|
@ -1,4 +0,0 @@
|
|||
class BaseImage {
|
||||
late String hash;
|
||||
BaseImage(this.hash);
|
||||
}
|
10
lib/src/classes/images/guild_discovery_splash.dart
Normal file
10
lib/src/classes/images/guild_discovery_splash.dart
Normal file
|
@ -0,0 +1,10 @@
|
|||
class GuildDiscoverySplash {
|
||||
late String id;
|
||||
late String hash;
|
||||
|
||||
GuildDiscoverySplash(this.hash, this.id);
|
||||
|
||||
String url({String? extension = "jpeg"}) {
|
||||
return "https://cdn.discordapp.com/discovery-splashes/$id/$hash.$extension";
|
||||
}
|
||||
}
|
|
@ -1,11 +1,8 @@
|
|||
import 'base_image.dart';
|
||||
|
||||
class GuildIcon extends BaseImage {
|
||||
class GuildIcon {
|
||||
late String id;
|
||||
late String hash;
|
||||
|
||||
GuildIcon(String hash, this.id) : super(hash) {
|
||||
this.hash = hash;
|
||||
}
|
||||
GuildIcon(this.hash, this.id);
|
||||
|
||||
String url({String? extension = "jpeg"}) {
|
||||
return "https://cdn.discordapp.com/icons/$id/$hash.$extension";
|
||||
|
|
|
@ -1,11 +1,8 @@
|
|||
import 'base_image.dart';
|
||||
|
||||
class GuildSplash extends BaseImage {
|
||||
class GuildSplash {
|
||||
late String id;
|
||||
late String hash;
|
||||
|
||||
GuildSplash(String hash, this.id) : super(hash) {
|
||||
this.hash = hash;
|
||||
}
|
||||
GuildSplash(this.hash, this.id);
|
||||
|
||||
String url({String? extension = "jpeg"}) {
|
||||
return "https://cdn.discordapp.com/splashes/$id/$hash.$extension";
|
||||
|
|
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";
|
||||
|
@ -20,7 +21,7 @@ final apiURL = "https://discord.com/api/v$version";
|
|||
/// This function calculate the intent number required from the gateway.
|
||||
/// [intents] is a list of multiples of two. You can use GatewayIntentBits class.
|
||||
/// Return a number.
|
||||
int calculateIntents(List<int> intents) {
|
||||
int intentsCalculator(List<int> intents) {
|
||||
int intentsNumber = 0;
|
||||
|
||||
for (var element in intents) {
|
||||
|
@ -42,6 +43,8 @@ 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.
|
||||
|
@ -94,23 +97,10 @@ class Client extends EventEmitter {
|
|||
});
|
||||
}
|
||||
|
||||
Sender sender = Sender(token);
|
||||
var i = await sender.fetchGuilds(withCounts: true);
|
||||
late Sender sender;
|
||||
late int n;
|
||||
|
||||
List<Guild> gg = [];
|
||||
|
||||
for (dynamic g in i) {
|
||||
gg.add(Guild(sender, g));
|
||||
}
|
||||
|
||||
channels = ChannelManager(sender, [], main: true);
|
||||
|
||||
sender.channels = channels;
|
||||
guilds = GuildManager(sender, gg);
|
||||
|
||||
int n = i.length;
|
||||
|
||||
ws.listen((event) {
|
||||
ws.listen((event) async {
|
||||
event = json.decode(event);
|
||||
|
||||
switch (event["op"]) {
|
||||
|
@ -130,17 +120,34 @@ class Client extends EventEmitter {
|
|||
|
||||
switch (eventName) {
|
||||
case "READY":
|
||||
sender = Sender(token, event["d"]["user"]["id"]);
|
||||
var i = await sender.fetchGuilds(withCounts: true);
|
||||
|
||||
List<Guild> gg = [];
|
||||
|
||||
for (dynamic g in i) {
|
||||
gg.add(Guild(sender, g));
|
||||
}
|
||||
|
||||
channels = ChannelManager(sender, [], main: true);
|
||||
|
||||
sender.channels = channels;
|
||||
guilds = GuildManager(sender, gg);
|
||||
|
||||
n = i.length;
|
||||
|
||||
commands["set"] = sender.setCommands;
|
||||
|
||||
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"])) {
|
||||
Guild oldGuild = guilds.cache.get(event["d"]["id"]);
|
||||
if (oldGuild.appIsOwner != null) {
|
||||
event['d']["owner"] = oldGuild.appIsOwner;
|
||||
}
|
||||
if (oldGuild.permissions != null) {
|
||||
event['d']["permissions"] = oldGuild.permissions;
|
||||
}
|
||||
|
@ -156,13 +163,11 @@ class Client extends EventEmitter {
|
|||
}
|
||||
break;
|
||||
case "GUILD_DELETE":
|
||||
dynamic guild;
|
||||
if (guilds.cache.has(event["d"]["id"])) {
|
||||
guild = guilds.cache.get(event["d"]["id"]);
|
||||
guilds.cache.set(event["d"]["id"], UnavailableGuild(event["d"]["id"], notUpdatedGuild: guild));
|
||||
} else {
|
||||
guild = event["d"];
|
||||
guilds.cache.delete(event["d"]["id"]);
|
||||
guilds.cache.set(event["d"]["id"], UnavailableGuild(event["d"]["id"],));
|
||||
}
|
||||
var guild = event["d"];
|
||||
emit("GUILD_DELETE", guild);
|
||||
break;
|
||||
case "INTERACTION_CREATE":
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
import 'classes/commands/command.dart';
|
||||
|
||||
import 'classes/message/message.dart';
|
||||
import 'main.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
@ -36,16 +38,22 @@ Future<String> requestWebSocketURL() async {
|
|||
|
||||
class Sender {
|
||||
final String? _token;
|
||||
final String id;
|
||||
Map<String, String> headers = {};
|
||||
dynamic channels;
|
||||
String? id;
|
||||
|
||||
Sender(this._token) {
|
||||
Sender(this._token, this.id) {
|
||||
headers = {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": "Bot $_token",
|
||||
};
|
||||
}
|
||||
|
||||
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 +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);
|
||||
|
@ -86,6 +94,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<String, dynamic> content) async {
|
||||
|
|
|
@ -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