diff --git a/bin/nexusbot.dart b/bin/nexusbot.dart index 1c4f878..997bddf 100644 --- a/bin/nexusbot.dart +++ b/bin/nexusbot.dart @@ -37,61 +37,62 @@ void main(List argsRaw) async { final client = await container.read(ClientController.provider.future); client.onTimelineEvent.stream.listen((event) async { - final settings = container.read(SettingsController.provider)!; + try { + final settings = container.read(SettingsController.provider)!; - if (event.room.canonicalAlias != settings.adminRoom) return; - if (event.senderId.startsWith("@${settings.name}:")) return; - switch (event.type) { - case EventTypes.Reaction: - if ((event.content["m.relates_to"] as Map)["key"] != - "✅") { - return; - } - final parentEvent = await client.getOneRoomEvent( - event.roomId!, - event.relationshipEventId!, - ); - if (!parentEvent.senderId.startsWith("@${settings.name}:")) return; - final registration = Registration.fromJson(parentEvent.content); - container - .read(RegistrationController.provider.notifier) - .add(registration); + if (event.room.canonicalAlias != settings.adminRoom) return; + if (event.senderId.startsWith("@${settings.name}:")) return; + switch (event.type) { + case EventTypes.Reaction: + if ((event.content["m.relates_to"] as Map)["key"] != + "✅") { + return; + } + final parentEvent = await client.getOneRoomEvent( + event.roomId!, + event.relationshipEventId!, + ); + if (!parentEvent.senderId.startsWith("@${settings.name}:")) return; + final registration = Registration.fromJson(parentEvent.content); + container + .read(RegistrationController.provider.notifier) + .add(registration); - await event.room.sendTextEvent( - "!admin users create ${registration.username}", - ); - break; - case EventTypes.Message: - if (!event.senderId.startsWith("@${settings.adminName}:")) break; - final results = RegExp( - r"@(?[a-zA-Z0-9._-]+):[^\s]+.*?password:\s+`(?[^`]+)`", - ).firstMatch(event.body); - if (results == null) return; + await event.room.sendTextEvent( + "!admin users create ${registration.username}", + ); + break; + case EventTypes.Message: + if (!event.senderId.startsWith("@${settings.adminName}:")) break; + final results = RegExp( + r"@(?[a-zA-Z0-9._-]+):[^\s]+.*?password:\s+`(?[^`]+)`", + ).firstMatch(event.body); + if (results == null) return; - final registration = container - .read(RegistrationController.provider) - .firstWhere( - (account) => account.username == results.namedGroup("username"), - ); - final password = results.namedGroup("password")!; - final reactionEvent = await event.room.sendReaction( - event.eventId, - "✉️ Sending...", - ); + final registration = container + .read(RegistrationController.provider) + .firstWhere( + (account) => account.username == results.namedGroup("username"), + ); + final password = results.namedGroup("password")!; + final reactionEvent = await event.room.sendReaction( + event.eventId, + "✉️ Sending...", + ); - await container - .read(MailClientController.provider.notifier) - .sendMessage( - plainText: - """Your registration for Federated Nexus has been accepted! Your credentials are: + await container + .read(MailClientController.provider.notifier) + .sendMessage( + plainText: + """Your registration for Federated Nexus has been accepted! Your credentials are: Username: ${registration.username}. Password: $password If you have any questions, check out our documentation: https://federated.nexus/services/matrix/. If you have any issues, reply to this email.""", - markdown: - """# Your registration for Federated Nexus has been accepted! + markdown: + """# Your registration for Federated Nexus has been accepted! ## Your credentials are: - ### Username: ${registration.username}. - ### Password: $password @@ -99,21 +100,24 @@ If you have any issues, reply to this email.""", If you have any questions, check out [our documentation](https://federated.nexus/services/matrix/). If you have any issues, reply to this email.""", - subject: - "Your registration for Federated Nexus has been accepted!", - to: mail.MailAddress(registration.username, registration.email), - ); - await event.room.redactEvent(reactionEvent!); - await event.room.sendReaction(event.eventId, "✉️ Sent!"); - if (settings.inviteTo != null) { - client - .getRoomByAlias(settings.inviteTo!)! - .invite( - RegExp( - r"(?@[a-zA-Z0-9._-]+:[^\s]+)", - ).firstMatch(event.body)!.namedGroup("userid")!, + subject: + "Your registration for Federated Nexus has been accepted!", + to: mail.MailAddress(registration.username, registration.email), ); - } + await event.room.redactEvent(reactionEvent!); + await event.room.sendReaction(event.eventId, "✉️ Sent!"); + if (settings.inviteTo != null) { + client + .getRoomByAlias(settings.inviteTo!)! + .invite( + RegExp( + r"(?@[a-zA-Z0-9._-]+:[^\s]+)", + ).firstMatch(event.body)!.namedGroup("userid")!, + ); + } + } + } catch (error, stackTrace) { + print("$error\n$stackTrace"); } });