Skip to content

Commit

Permalink
Merge pull request #26 from nyxx-discord/feature/poop-service-extension
Browse files Browse the repository at this point in the history
feature: Extend pooping service; Add system command
  • Loading branch information
l7ssha authored Jul 2, 2023
2 parents 70a3d7a + 213053c commit 3d5ff6b
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 24 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 3.3.0

- Implement new `/admin perform-nickname-pooping` and `/system clear-cache` command

## 3.2.3

- Fix `/reminder list` command
Expand Down
4 changes: 3 additions & 1 deletion bin/running_on_dart.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:nyxx/nyxx.dart';
import 'package:nyxx_commands/nyxx_commands.dart';
import 'package:running_on_dart/running_on_dart.dart';
import 'package:running_on_dart/src/commands/system.dart';

void main() async {
// Create nyxx client and nyxx_commands plugin
Expand All @@ -23,7 +24,8 @@ void main() async {
..addCommand(admin)
..addCommand(settings)
..addCommand(github)
..addCommand(music);
..addCommand(music)
..addCommand(system);

// Add our error handler
commands.onCommandError.listen(commandErrorHandler);
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.prod.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: '3.9'
services:
running_on_dart:
image: ghcr.io/nyxx-discord/running_on_dart:3.2.3
image: ghcr.io/nyxx-discord/running_on_dart:3.3.0
container_name: running_on_dart
env_file:
- .env
Expand Down
1 change: 0 additions & 1 deletion lib/src/checks.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:nyxx/nyxx.dart';
import 'package:nyxx_commands/nyxx_commands.dart';
import 'package:running_on_dart/src/settings.dart';

Expand Down
49 changes: 42 additions & 7 deletions lib/src/commands/admin.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import 'package:nyxx/nyxx.dart';
import 'package:nyxx_commands/nyxx_commands.dart';
import 'package:running_on_dart/src/checks.dart';
import 'package:running_on_dart/src/exception.dart';
import 'package:running_on_dart/src/services/poop_name.dart';

ChatGroup admin = ChatGroup(
'admin',
'Administrative commands',
checks: [
administratorCheck,
],
options: CommandsOptions(
hideOriginalResponse: true,
),
children: [
ChatCommand(
'cleanup',
Expand Down Expand Up @@ -60,6 +54,47 @@ ChatGroup admin = ChatGroup(
await context.respond(MessageBuilder.content('Successfully deleted messages!'));
}
}),
checks: [PermissionsCheck(PermissionsConstants.manageMessages)],
options: CommandsOptions(
hideOriginalResponse: true,
),
),
ChatCommand(
"perform-nickname-pooping",
"Perform pooping of usernames in current guild",
id('perform-nickname-pooping', (IChatContext context, [bool dryRun = true, int batchSize = 100]) async {
var nickNamesToRemove = <String>[];
for(final disallowedChar in poopCharacters) {
await for(final member in context.guild!.searchMembersGateway(disallowedChar, limit: batchSize)) {
final nick = await PoopNameService.instance.poopUser(member, dryRun: dryRun);
if (nick != null) {
nickNamesToRemove.add(nick);
}
}
}

final outPutMessageHeader = "Pooping nicknames" + (dryRun ? "[DRY RUN]" : "");
var nickString = nickNamesToRemove.where((element) => element.isNotEmpty).join(",");
if (nickString.length > 1950) {
nickString = nickString.substring(0, 1950) + " ...";
} else if (nickString.isEmpty) {
nickString = "-/-";
}

var outputMessage = """
$outPutMessageHeader:\n
```
$nickString
```
""";

await context.respond(MessageBuilder.content(outputMessage));
}),
checks: [
GuildCheck.all(),
PermissionsCheck(PermissionsConstants.manageNicknames),
],
options: CommandOptions(autoAcknowledgeInteractions: true)
)
],
);
9 changes: 4 additions & 5 deletions lib/src/commands/info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import 'package:nyxx_commands/nyxx_commands.dart';
import 'package:nyxx_interactions/nyxx_interactions.dart';
import 'package:running_on_dart/running_on_dart.dart';
import 'package:running_on_dart/src/util.dart';
import 'package:time_ago_provider/time_ago_provider.dart';

String getCurrentMemoryString() {
final current = (ProcessInfo.currentRss / 1024 / 1024).toStringAsFixed(2);
Expand Down Expand Up @@ -37,19 +36,19 @@ ChatCommand info = ChatCommand(
..addField(name: 'Cached channels', content: context.client.channels.length, inline: true)
..addField(
name: 'Cached voice states',
content: context.client.guilds.values.map((g) => g.voiceStates.length).reduce((value, element) => value + element),
content: context.client.guilds.values.map((g) => g.voiceStates.length).fold<num>(0, (value, element) => value + element),
inline: true,
)
..addField(name: 'Shard count', content: (context.client as INyxxWebsocket).shards, inline: true)
..addField(
name: 'Cached messages',
content: context.client.channels.values.whereType<ITextChannel>().map((c) => c.messageCache.length).reduce((value, element) => value + element),
content: context.client.channels.values.whereType<ITextChannel>().map((c) => c.messageCache.length).fold<num>(0, (value, element) => value + element),
inline: true,
)
..addField(name: 'Memory usage (current/RSS)', content: getCurrentMemoryString(), inline: true)
..addField(name: 'Uptime', content: formatFull(context.client.startTime))
..addField(name: 'Uptime', content: TimeStampStyle.relativeTime.format(context.client.startTime))
..addField(
name: 'Last documentation cache update', content: DocsService.instance.lastUpdate == null ? 'never' : formatFull(DocsService.instance.lastUpdate!));
name: 'Last documentation cache update', content: DocsService.instance.lastUpdate == null ? 'never' : TimeStampStyle.relativeTime.format(DocsService.instance.lastUpdate!));

await context.respond(ComponentMessageBuilder()
..embeds = [embed]
Expand Down
26 changes: 26 additions & 0 deletions lib/src/commands/system.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'package:nyxx/nyxx.dart';
import 'package:nyxx_commands/nyxx_commands.dart';
import 'package:running_on_dart/running_on_dart.dart';
import 'package:running_on_dart/src/checks.dart';

ChatGroup system = ChatGroup(
'system',
'Commands designed for ROD administrators',
children: [
ChatCommand(
"clear-cache",
"Clear bot cache",
id("clear-cache", (IChatContext context) async {
context.client.channels.clear();
context.client.users.clear();

await context.respond(MessageBuilder.content("Cache cleared successfully!"));
})
)
],
checks: [
administratorCheck,
GuildCheck.id(adminGuildId)
],
options: CommandOptions(hideOriginalResponse: true)
);
22 changes: 17 additions & 5 deletions lib/src/services/poop_name.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import 'package:nyxx/nyxx.dart';
import 'package:running_on_dart/running_on_dart.dart';
import 'package:running_on_dart/src/models/guild_settings.dart';

const _poopEmoji = "💩";
final _poopRegexp = RegExp(r"[!#@^%&-*\.+']");
const poopEmoji = "💩";
final poopCharacters = ['!', '#', '@', '^', '%', '&', '-', '*', '.' '+', '\''];
final poopRegexp = RegExp("[${poopCharacters.join()}]");

class PoopNameService {
static PoopNameService get instance => _instance ?? (throw Exception('PoopNameService must be initialised with PoopNameService.init'));
Expand All @@ -27,11 +28,22 @@ class PoopNameService {
return;
}

poopUser(member);
}

/// Returns null if user should be pooped. Returns pooped user username otherwise
Future<String?> poopUser(IMember member, {bool dryRun = false}) async {
final memberUser = await member.user.getOrDownload();
if ((member.nickname ?? memberUser.username).startsWith(_poopRegexp)) {
_logger.fine("Changing ${member.id} (${member.nickname ?? memberUser.username})'s nickname to poop emoji");
final memberName = member.nickname ?? memberUser.username;
if (!memberName.startsWith(poopRegexp)) {
return null;
}

await member.edit(builder: MemberBuilder()..nick = _poopEmoji);
if(!dryRun) {
_logger.fine("Changing ${member.id} ($memberName) nickname to poop emoji");
await member.edit(builder: MemberBuilder()..nick = poopEmoji);
}

return memberName;
}
}
6 changes: 4 additions & 2 deletions lib/src/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'dart:io';

import 'package:nyxx/nyxx.dart';

String get version => '3.2.3';
String get version => '3.3.0';

/// Get a [String] from an environment variable, throwing an exception if it is not set.
///
Expand All @@ -25,6 +25,8 @@ final bool intentFeaturesEnabled = getEnvBool('ROD_INTENT_FEATURES_ENABLE');
/// The prefix to use for text commands for this instance.
final String prefix = getEnv('ROD_PREFIX');

final Snowflake adminGuildId = Snowflake(getEnv('ROD_ADMIN_GUILD'));

/// The IDs of the users that are allowed to use administrator commands
final List<Snowflake> adminIds = getEnv('ROD_ADMIN_IDS').split(RegExp(r'\s+')).map(Snowflake.new).toList();

Expand Down Expand Up @@ -88,7 +90,7 @@ bool useSSL = getEnvBool('LAVALINK_USE_SSL', false);
final int _baseIntents = GatewayIntents.directMessages | GatewayIntents.guilds | GatewayIntents.guildVoiceState;

/// Privileged intents that can be enabled to add additional features to Running on Dart.
final int _privilegedIntents = _baseIntents | GatewayIntents.guildMessages | GatewayIntents.guildMembers;
final int _privilegedIntents = _baseIntents | GatewayIntents.guildMessages | GatewayIntents.guildMembers | GatewayIntents.messageContent;

/// The intents to use for this instance.
final int intents = intentFeaturesEnabled ? _privilegedIntents : _baseIntents;
3 changes: 1 addition & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: running_on_dart
version: 3.2.3
version: 3.3.0
description: Discord Bot for nyxx development
homepage: https://github.com/nyxx-discord/running_on_dart
repository: https://github.com/nyxx-discord/running_on_dart
Expand Down Expand Up @@ -30,7 +30,6 @@ dependencies:
prometheus_client_shelf: ^1.0.0
shelf: ^1.3.0
shelf_router: ^1.1.2
time_ago_provider: ^4.1.2

dev_dependencies:
lints: ^1.0.1

0 comments on commit 3d5ff6b

Please sign in to comment.