Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Remove gateway dependency #159

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions lib/src/commands.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ final Logger logger = Logger('Commands');
/// - [addCommand], for adding commands to your bot;
/// - [check], for adding checks to your bot;
/// - [MessageCommand] and [UserCommand], for creating Message and User Commands respectively.
class CommandsPlugin extends NyxxPlugin<NyxxGateway> implements CommandGroup<CommandContext> {
class CommandsPlugin extends NyxxPlugin<NyxxRest> implements CommandGroup<CommandContext> {
/// A function called to determine the prefix for a specific message.
///
/// This function should return a [Pattern] that should match the start of the message content if
Expand Down Expand Up @@ -149,7 +149,7 @@ class CommandsPlugin extends NyxxPlugin<NyxxGateway> implements CommandGroup<Com
/// A list of commands registered by this [CommandsPlugin] to the Discord API.
final List<ApplicationCommand> registeredCommands = [];

final Set<NyxxGateway> _attachedClients = {};
final Set<NyxxRest> _attachedClients = {};

/// Create a new [CommandsPlugin].
CommandsPlugin({
Expand All @@ -167,7 +167,7 @@ class CommandsPlugin extends NyxxPlugin<NyxxGateway> implements CommandGroup<Com
}

@override
Future<void> afterConnect(NyxxGateway client) async {
Future<void> afterConnect(NyxxRest client) async {
_attachedClients.add(client);

client.onMessageComponentInteraction
Expand Down Expand Up @@ -288,12 +288,12 @@ class CommandsPlugin extends NyxxPlugin<NyxxGateway> implements CommandGroup<Com
}

@override
void beforeClose(NyxxGateway client) {
void beforeClose(NyxxRest client) {
registeredCommands.removeWhere((command) => command.manager.client == client);
_attachedClients.remove(client);
}

Future<void> _syncCommands(NyxxGateway client) async {
Future<void> _syncCommands(NyxxRest client) async {
final builders = await _buildCommands();

final commands = await Future.wait(builders.entries.map(
Expand Down
4 changes: 2 additions & 2 deletions lib/src/context/base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ abstract interface class ContextData {
CommandsPlugin get commands;

/// The client that emitted the event triggering this context's creation.
NyxxGateway get client;
NyxxRest get client;
}

/// Data about a context in which a command was executed.
Expand Down Expand Up @@ -406,7 +406,7 @@ class ContextBase implements ContextData {
@override
final CommandsPlugin commands;
@override
final NyxxGateway client;
final NyxxRest client;

ContextBase({
required this.user,
Expand Down
16 changes: 8 additions & 8 deletions lib/src/context/context_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class ContextManager {
member: member,
user: user,
command: command,
client: message.manager.client as NyxxGateway,
client: message.manager.client,
prefix: prefix,
message: message,
rawArguments: contentView.remaining,
Expand Down Expand Up @@ -100,7 +100,7 @@ class ContextManager {
member: member,
user: user,
command: command,
client: interaction.manager.client as NyxxGateway,
client: interaction.manager.client,
interaction: interaction,
rawArguments: rawArguments,
);
Expand All @@ -117,7 +117,7 @@ class ContextManager {
Member? member = interaction.member;
User user = member?.user ?? interaction.user!;

final client = interaction.manager.client as NyxxGateway;
final client = interaction.manager.client;

User targetUser = await client.users[interaction.data.targetId!].get();
Guild? guild = await interaction.guild?.get();
Expand Down Expand Up @@ -153,7 +153,7 @@ class ContextManager {

return MessageContext(
commands: commands,
client: interaction.manager.client as NyxxGateway,
client: interaction.manager.client,
interaction: interaction,
command: command,
channel: channel,
Expand Down Expand Up @@ -194,7 +194,7 @@ class ContextManager {
member: member,
user: user,
command: command,
client: interaction.manager.client as NyxxGateway,
client: interaction.manager.client,
interaction: interaction,
option: focusedOption,
currentValue: focusedOption.value.toString(),
Expand All @@ -216,7 +216,7 @@ class ContextManager {
guild: await interaction.guild?.get(),
channel: await interaction.channel!.get() as TextChannel,
commands: commands,
client: interaction.manager.client as NyxxGateway,
client: interaction.manager.client,
interaction: interaction,
);
}
Expand All @@ -238,7 +238,7 @@ class ContextManager {
guild: await interaction.guild?.get(),
channel: await interaction.channel!.get() as TextChannel,
commands: commands,
client: interaction.manager.client as NyxxGateway,
client: interaction.manager.client,
interaction: interaction,
selected: selected,
);
Expand All @@ -257,7 +257,7 @@ class ContextManager {
guild: await interaction.guild?.get(),
channel: await interaction.channel!.get() as TextChannel,
commands: commands,
client: interaction.manager.client as NyxxGateway,
client: interaction.manager.client,
interaction: interaction,
);
}
Expand Down
88 changes: 45 additions & 43 deletions lib/src/converters/built_in/member.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,59 +18,61 @@ Future<Member?> snowflakeToMember(Snowflake snowflake, ContextData context) asyn
Future<Member?> convertMember(StringView view, ContextData context) async {
String word = view.getQuotedWord();

if (context.guild != null) {
Stream<Member> named = context.client.gateway.listGuildMembers(
context.guild!.id,
query: word,
limit: 100,
);
if (context.guild == null) {
return null;
}

List<Member> usernameExact = [];
List<Member> nickExact = [];
List<Member> named = context.client is NyxxGateway
? await (context.client.gateway as Gateway)
.listGuildMembers(context.guild!.id, query: word, limit: 100)
.toList()
: await context.guild!.members.search(word, limit: 100);

List<Member> usernameCaseInsensitive = [];
List<Member> nickCaseInsensitive = [];
List<Member> usernameExact = [];
List<Member> nickExact = [];

List<Member> usernameStart = [];
List<Member> nickStart = [];
List<Member> usernameCaseInsensitive = [];
List<Member> nickCaseInsensitive = [];

await for (final member in named) {
User user = await context.client.users.get(member.id);
List<Member> usernameStart = [];
List<Member> nickStart = [];

if (user.username == word) {
usernameExact.add(member);
}
if (user.username.toLowerCase() == word.toLowerCase()) {
usernameCaseInsensitive.add(member);
for (final member in named) {
User user = await context.client.users.get(member.id);

if (user.username == word) {
usernameExact.add(member);
}
if (user.username.toLowerCase() == word.toLowerCase()) {
usernameCaseInsensitive.add(member);
}
if (user.username.toLowerCase().startsWith(word.toLowerCase())) {
usernameStart.add(member);
}

if (member.nick != null) {
if (member.nick! == word) {
nickExact.add(member);
}
if (user.username.toLowerCase().startsWith(word.toLowerCase())) {
usernameStart.add(member);
if (member.nick!.toLowerCase() == word.toLowerCase()) {
nickCaseInsensitive.add(member);
}

if (member.nick != null) {
if (member.nick! == word) {
nickExact.add(member);
}
if (member.nick!.toLowerCase() == word.toLowerCase()) {
nickCaseInsensitive.add(member);
}
if (member.nick!.toLowerCase().startsWith(word.toLowerCase())) {
nickStart.add(member);
}
if (member.nick!.toLowerCase().startsWith(word.toLowerCase())) {
nickStart.add(member);
}
}
}

for (final list in [
usernameExact,
nickExact,
usernameCaseInsensitive,
nickCaseInsensitive,
usernameStart,
nickStart
]) {
if (list.length == 1) {
return list.first;
}
for (final list in [
usernameExact,
nickExact,
usernameCaseInsensitive,
nickCaseInsensitive,
usernameStart,
nickStart
]) {
if (list.length == 1) {
return list.first;
}
}
return null;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/event_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ class EventManager {
return;
}

if (message.author.id == await event.gateway.client.users.fetchCurrentUser() &&
if (message.author.id == await event.client.users.fetchCurrentUser() &&
!context.command.resolvedOptions.acceptSelfCommands!) {
return;
}
Expand Down
3 changes: 1 addition & 2 deletions lib/src/util/util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,7 @@ Future<String> Function(MessageCreateEvent) mentionOr(
RegExpMatch? match = _mentionPattern.firstMatch(event.message.content);

if (match != null) {
if (int.parse(match.group(1)!) ==
(await event.gateway.client.users.fetchCurrentUser()).id.value) {
if (int.parse(match.group(1)!) == (await event.client.users.fetchCurrentUser()).id.value) {
return match.group(0)!;
}
}
Expand Down
6 changes: 6 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,9 @@ dev_dependencies:

executables:
nyxx-compile: compile

dependency_overrides:
nyxx:
git:
url: https://github.com/nyxx-discord/nyxx.git
ref: feature/remove-event-depenency-on-gateway
Loading